TPG is a package which main function is to define a class which particular metaclass converts a doc string into a parser. You only need to import TPG and use these five objects:
The grammar must be in the doc string of the class (see figure 4.1).
|
Then you can use the new generated parser. The parser is simply a Python class (see figure 4.2).
|
The tpg script reads a Python script and replaces TPG grammars (in doc string) by Python code. To produce a Python script (*.py) from a script containing grammars (*.pyg) you can use tpg as follow:
tpg [-v|-vv] grammar.pyg [-o parser.py]
|
tpg accepts some options on the command line:
Notice that .pyg files are valid Python scripts. So you can choose the run .pyg file (slower startup but easier for debugging purpose) or turn them into a .py file (faster startup but needs a ”precompilation” stage). You can also write .py scripts containing grammars to be used as Python scripts.
In fact I only use the tpg script to convert tpg.pyg into tpg.py because TPG needs obviously to be a pure Python script (it can not translate itself at runtime). Then in most cases it is very convenient to directly write grammars in Python scripts.