parsley parsing question, how to make a variable grammar

Eric S. Johansson esj at harvee.org
Fri Jun 13 17:05:32 EDT 2014


In my quest for making speech friendly applications, I've developed a 
very simple domain specific language/notation that works well. I'm using 
parsley which is a great tool for writing parsers especially simple ones 
like the one I need. However, I've come across a problem that I don't 
know how to solve.

Below is my grammar. The problem is the last element which aggregates 
all individual grammar elements. In my domain specific notation, it's 
possible for a user to create extensions and I'm trying to figure out 
how to make the grammar accommodate those extensions.

The first Issue is adding more statements. I can handle that easy enough 
by splitting the existing grammar into a prefix, statements, and 
postfix. I could append more statements to the end of the statement 
section. When I'm done, I can then join all of the pieces together into 
a single grammar.

The second issue is adding more elements between parentheses of bot so 
the additional statements can be included in the grammar. Seems to me, I 
should be able to create a Python expression which returns the OR list 
and it is interpreted as part of the grammar. Failing that, I'm just 
going to do a %s expansion when I create the grammar.

I appreciate any insight before I go too far off track.
--- eric

TF_grammar = r"""
kwToken = (letter|digit|'_')+
uses_statement = 'uses' ws kwToken:kwT ':' <anything*>:roL -> do_uses 
("".join(kwT), "".join(roL))
returns_statement = 'returns' ws kwToken:kwT -> do_returns("".join(kwT))
template_statement = 'template' ws kwToken:kwT -> do_template("".join(kwT))
remembers_statement = 'remembers' ws kwToken:kwT -> 
do_remembers("".join(kwT))
everything_else = <anything*>:roL '\n'{0,1} -> do_everything_else 
("".join(roL))
bot = (uses_statement | returns_statement | template_statement | 
everything_else)
"""



More information about the Python-list mailing list