Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #73271

parsley parsing question, how to make a variable grammar

Date 2014-06-13 17:05 -0400
From "Eric S. Johansson" <esj@harvee.org>
Subject parsley parsing question, how to make a variable grammar
Newsgroups comp.lang.python
Message-ID <mailman.11060.1402695110.18130.python-list@python.org> (permalink)

Show all headers | View raw


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)
"""

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

parsley parsing question, how to make a variable grammar "Eric S. Johansson" <esj@harvee.org> - 2014-06-13 17:05 -0400

csiph-web