Dython

(TreeMeta for Python)


treemeta.py, a recursive descent method implementation for Python that combines regular expression with syntax tree state processing. Input is analyzed to produce a tree representation which is then traversed to produce output. All operations, input interpretation and tree assembly, tree traversal and output generation are specified and controlled from a single source file. The TreeMeta syntax used provides abstractions for parse (tree construction), unparse (tree traversal) and production (output generation) operation. The goal of which is to make recursive descent techniques as easy to use as regular expressions.


As its own method library

#!/usr/bin/python

import treemeta

s = r"""
class rule[treemeta.lib] :
  GOAL = TEXT *                       # define function GOAL
  TEXT = .LABEL $( ', .LABEL :DO[2] )
  DO [-,-] => *1 *2
"""

c = treemeta.compile(s)               # compile rule set
exec c                                # create rule class
r = rule()                            # create rule instance

i = 'first, second, third'
o = r.GOAL(i)                         # call function from class


As a language extension
#!/usr/bin/dython # local rule(s) def'd as class

class rule[treemeta.lib] :            # define rule as class with TreeMeta
  GOAL = TEXT *
  TEXT = .SR $( ', .SR :DO[2] )
  DO [-,-] => *1 *2

r = rule()                            # create rule instance

i = 'first, second, third'
o = r.GOAL(i)                         # call function from class

#!/usr/bin/dython # global rule(s) def'd inline

#!/usr/bin/dython

[ GOAL = TEXT *                       # TreeMeta specification bewteen []'s
  TEXT = .LABEL $( ', .LABEL :DO[2] ) # to create function GOAL()
  DO [-,-] => *1 *2 ]

i = 'first, second, third'
o = GOAL(i)                           # rule based processing function call