Package td_parser
[hide private]
[frames] | no frames]

Package td_parser

source code

Code and classes for top down recursive descent parser. Currently just a recognizer is actually implemented.
>>> from td_parser import *
>>> demo(['John likes Mary'])
This will recognize 'John likes Mary' with the first default grammar defined in the demo function at the bottom of the module file.
>>> g = Grammar('s')
defines a grammar g with start symbol 's'
>>> g.add_production('s', ['np','vp'])
adds the rule s -> np vp.
>>> p = Parser(g)
creates a parser instance p with grammar g.
>>> p.trace()
>>> p.recognize_string('The boy likes the girl')

Turn verbose trace output on for p and recognize the string 'The boy likes the girl.'

For more examples of all of the above see the demo function in td_parser.td_parser.
>>> strings = ['John likes Mary', 'The boy likes the girl', 'Eat beans']
>>> demo(strings)

Run the demo with these 3 strings.

td_parser.td_parser includes a class for a top down recursive descent parser, a class for context-free grammars, and parser states. There are two grammars defined in the demo function, declared as globals g1 and g2.

Submodules [hide private]