Chapter 9
Debugging

9.1 Introduction

When I need to debug a grammar I often add print statments to visualize the parser activity. Now with TPG 3 it is possible to print such information automatically.

9.2 Verbose parsers

Normal parsers inherit from tpg.Parser. If you need a more verbose parser you can use tpg.VerboseParser instead. This parser prints information about the current token each time the lexer is called. The debugging information has currently two level of details.

Level 0
displays no information.
Level 1
displays tokens only when the current token matches the expected token.
Level 2
displays tokens if the current token matches or not the expected token.

The level is defined by the attribute verbose. Its default value is 1.




Figure 9.1: Verbose parser example
class Test(tpg.VerboseParser):  
    r"""  
 
    START -> 'x' 'y' 'z' ;  
 
    """  
 
    verbose = 2



The information displayed by verbose parsers has the following format:

[eat counter][stack depth]callers: (line,row) <current token> == <expected token>

eatcounter
is the number of calls of the lexer.
stackdepth
is the depth of the Python stack since the axiom.
callers
is the list of non terminal symbols called before the current symbol.
(line,row)
is the position of the current token in the input string.
==
means the current token matches the expected token (level 1 or 2).
! =
means the current token doesn’t match the expected token (level 2).