blaxyzblaxyz
unread,Apr 28, 2008, 7:51:20 AM4/28/08Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to ply-hack
When running the calc.py in examples/calc of the ply package, I enter
"3*(3)" (without the quotes) and get "9" as answer, which is correct.
When I enter "3*(*)" I get: "Syntax error at '*'", which is exactly
what the p_error function is supposed to do.
I then modify calc.py. I change "yacc.yacc()" to
"yacc.yacc(optimize=1)". I run the program again. I once again enter
"3*(*)". This time however, I get "yacc: Syntax error at line 1,
token=*" on my screen. It turns out that yacc no longer has an error
function defined and therefore falls back to printing something to
stderr itself. The reason for this is that since the parsetab.py was
loaded there is no longer a p_error present (internally, in yacc,
self.errorfunc = None).
In my application I encountered the same problem. I have a p_error
function that raises a custom parsing exception. However, since I use
optimize=1, yacc no longer has the error function and thus prints a
message to stderr and continues! This means that None is returned as
parse result and the rest of my code crashes, since it cannot handle
None as parse result. (Note that it should not have to handle None, as
parse errors should be handled by p_error and the code DOES handle the
custom parsing exceptions).
So, to conclude, using optimize=1 (using the imported parsetab.py),
yacc loses the p_error function and this changes the outcome of
parsing!
It may also be interesting to know that the same does not apply to
lex, for there the t_error function is preserved, even when using
lextab.py.
Finally, I wonder whether all of this is wanted behavior or not. In my
opinion it shouldn't be...