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

2.4k views Asked by At

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.

2

There are 2 answers

0
salspaugh On BEST ANSWER

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.

1
Dan Svoboda On

If you're only interested in the stack state at the point where p_error gets triggered, but don't wish to log all the other debug info, this does a nice job:

def p_error(p):

    # get formatted representation of stack
    stack_state_str = ' '.join([symbol.type for symbol in parser.symstack][1:])

    print('Syntax error in input! Parser State:{} {} . {}'
          .format(parser.state,
                  stack_state_str,
                  p))

The parser you create with parser = yacc.yacc() must be in scope for this solution to work.