TPG

Christophe Delord <[email protected]> Tue, 28 May 2002 19:06:13 +0200
Newsgroups gmane.comp.python.parser
Message-ID <[email protected]>
Hello,

I wrote a parser generator for Python. I would like to present it in this SIG.

TPG (Toy Parser Generator) generates recursive descendant parsers. It is not an LL(k) generator. It's more like Prolog DGC parsers. For every choice points, the first alternative that match is choosen. But once a choice has been made it can be undone (it's not a complete backtracking as in Prolog). So contrary to other parsers, grammar rule order is important.

The lexical scanner uses Python regular expressions. The input is split into a list of tokens. Tokens return the matched text by default but can be given a function to work with this text (to convert a string to an integer for example).

The syntactic parser is a set of grammar rules. You can use classical operators in grammar rules (A|B, A*, A+, A?, where A is a symbol or a compound expression). Each symbol can have attributes (inherited or generated) and return a value. Attributes and return values are Python objects of any type.
The parser is translated into a Python class and each rule into a method of this class.

It is also possible to add code in the parser and in the grammar rules. This code may interfere with the parser (ie you can decide to leave a branch and backtrack).

TPG can be used as a module. That means that grammars can be in a string and generated dynamically. TPG can also generate autonomous parsers by including a runtime.

To have a further description of TPG you can read http://christophe.delord.free.fr/en/tpg/index.html

You may also want to read some examples at http://christophe.delord.free.fr/en/tpg/examples.html



Best regards,
Christophe Delord.