Then you can use the new generated parser. The parser is now simply a Python class (see
figure 4.3).
Figure 4.3:
Parser usage example
test = "bar"
my_parser = Foo()
x = my_parser(test) # Uses the START symbol
print x
x = my_parser.parse('Bar', test) # Uses the Bar symbol
print x
4.2Command line usage
The tpg script is just a wrapper for the package. It reads a grammar in a file and write the generated
code in a Python script. To produce a Python script from a grammar you can use tpg as
follow:
tpg [-v|-vv] grammar.g [-o parser.py]
tpg accepts some options on the command line:
-v
turns tpg into a verbose mode (it displays parser names).
-vv
turns tpg into a more verbose mode (it displays parser names and simplified rules).
-o file.py
tells tpg to generate the parser in file.py. The default output file is grammar.py if -o
option is not provided and grammar.g is the name of the grammar.