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)The best way to debug your
ocamlyacc
parser is to set theOCAMLRUNPARAM
param to include the characterp
- this will make the parser print all the states that it goes through, and each shift / reduce it performs.If you are using bash, you can do this with the following command:
I think that, similar to yacc, the tokens are stored in variables corresponding to the symbols in your grammar rule. Here since there is one symbol (error), you may be able to simply output $1 using
printf
, etc.Edit: responding to comment.
Why do you use an error terminal? I'm reading an ocamlyacc tutorial that says a special error-handling routine is called when a parse error happens. Like so:
Well, looks like you only get "syntax error" with that function though. Stay tuned for more info.