I am writing a small parser with Menhir + Ocamllex, and I have two requirements that I cannot meet at the same time.
- I would like to continue parsing after the error (to report more errors).
- I would like to print the token that caused the error.
I can only do 1) easily using the error token. I can also do only 2) easily using the approach suggested for this question . However, I do not know how easy it is to achieve both.
The error handling method right now looks something like this:
pair: | left = prodA SEPARATOR right = prodA { (* happy case *) } | error SEPARATOR right = prodA { print_error_report $startpos; (* would like to continue after the first error, just in case there is a second error, so I report both *) }
One thing that will help me is to access lexbuf itself so that I can get the token directly. That would mean instead of $startpos passing something like $lexbuf . But as far as I can tell, there is no official way to access lexbuf. Solution 1 works only at the caller level in the parser, where the caller itself passes the parser lexbuf t othe, but not in semantic actions.
Does anyone know if it is really available? or perhaps a workaround?
source share