aPTK - A Parse Toolkit¶
aPTK is a Parse Toolkit. It is useful to write documented grammars similar to BNF grammar language.
Typically you would use it like this:
from aptk import *
class AdditionGrammar(Grammar):
'''This is the grammar of a simple addition.
<addition> := <operand> <.ws> "+" <.ws> <operand>
<ws> := \s*
<operand> := \d+
'''
class AdditionActions(ParseActions):
def make_operand(self, p, lexem):
return int(str(lexem))
def make_addition(self, p, lexem):
return lexem[0].ast + lexem[1].ast
tree = parse("5 + 4", AdditionGrammar)
result = ast("5 + 4", AdditionGrammar, AdditionActions)
The most interesting on the grammars derived from BaseGrammar
is that they are compiled at compile-time of your python module. This is
possible due to some python voodoo with metaclasses in grammar
.