Related articles |
---|
ocamlyacc: where's the state? ajo@nospam.andrew.cmu.edu (Arthur J. O'Dwyer) (2004-10-09) |
From: | "Arthur J. O'Dwyer" <ajo@nospam.andrew.cmu.edu> |
Newsgroups: | comp.compilers |
Date: | 9 Oct 2004 22:54:35 -0400 |
Organization: | Carnegie Mellon, Pittsburgh, PA |
Keywords: | yacc, question |
Posted-Date: | 09 Oct 2004 22:54:35 EDT |
I am using ocamllex and ocamlyacc to generate the front end of a
compiler for a school project. I have found that it's very hard to
produce good error messages using ocamlyacc, especially since the use
of the 'error' production is still undocumented. (I wonder if anyone
is still maintaining ocamlyacc? Apparently there have been lots of
independent patches that haven't been absorbed into the official tree
yet.)
I have come across the Merr 'yyerror' generator in my search for a
better way. http://unicon.sourceforge.net/merr/ But it is targeted
specifically at C-based parser generators, and is written in a bizarre
language, so it's not directly useful to me.
However, I believe the general approach would be very useful. I
want to write a function like this:
let yyerror = (
if (yystate = 42 && yyinput = LBRACE)
printf "Unexpected left brace"
else if (yystate = 16 && yyinput = VAR)
printf "Extraneous \"var\" in struct definition"
else
printf "Syntax error: state %d, input symbol %s\n" yystate
(token2string yyinput)
)
Unfortunately, I don't see where in ocamlyacc I can get the values
'yystate' and 'yyinput' from. 'yystate' being the current parser state,
as described in the generated 'foo.output' file, and 'yyinput' being the
next token on the input stream.
Can anyone help me here? How do I find out the current parser state
internally? I'm willing to hack the generated 'foo.ml' by hand, but
not willing to hack the source of ocamlyacc itself.
Thanks,
-Arthur
Return to the
comp.compilers page.
Search the
comp.compilers archives again.