I'm using fslex/fsyacc utilities for my F# Lexer and Parser. If input text has incorrect syntax it is necessary to know place where it happens.
It is possible to determine incorrect lexeme (token) in Lexer and throw an exception if it was used incorrect symbol or word:
rule token = parse
...
| integer { INT (Int32.Parse(lexeme lexbuf)) }
| "*=" { failwith "Incorrect symbol" }
| eof { EOF }
The question is related more to Parser (fsyacc) - if input text has correct tokens and was sucessfuly tokenized by Lexer, but error happened during parsing (for example, incorrect tokens order or some absent token in the rule)
I know if catch an exception, this give position (line and column), where parsing failed:
try
Parser.start Lexer.token lexbuf
with e ->
let pos = lexbuf.EndPos
let line = pos.Line
let column = pos.Column
let message = e.Message // "parse error"
...
But is it possible (if yes - how to do it?) to determine also AST class, for which parsing failed.
For example is it possible to write something similar to following in my parser.fsy file:
Expression1:
| INT { Int $1 }
...
| _ { failwith "Error with parsing in Expression1"}