I'm using ocamlyacc and ocamllex. I have an error production in my grammar that signals a custom exception. So far, I can get it to report the error position:
| error { raise (Parse_failure (string_of_position (symbol_start_pos ()))) }
But, I also want to know which token was read. There must be a way---anyone know?
Thanks.
Tokens are generated by lexer, hence you can use the current lexer token when error occurs :
Lexing.lexeme lexbuf
is what you need. Other parts are not necessary but useful.ruleTail
will concat all remaining tokens into string for the user to easily locate error position.lexbuf.Lexing.lex_curr_p
should be updated in the lexer to contain correct positions. (source)