Jason S Jason S - 9 months ago 48
Python Question

debugging ply in p_error() to access parser state/stack

Is there a way to access the parser state/stack in p_error()?

All I know is that I can look at the offending token.

Answer Source

You can pass debug=1 to parse when you call it and it will output the parser stack.

Here is the function definition for that, for convenience:

def parse(self,input=None,lexer=None,debug=0,tracking=0,tokenfunc=None):

You can send the debugging output to a file too, if you set it up when you call yacc. Here is that function definition, for convenience:

def yacc(method='LALR', debug=yaccdebug, module=None, tabmodule=tab_module, start=None, check_recursion=1, optimize=0, write_tables=1, debugfile=debug_file,outputdir='', debuglog=None, errorlog = None, picklefile=None):

You may find it useful to checkout the yacc and parse methods in yacc.py to see how this works.