Pyparsing: Grammar Suggestion

Paul McGuire ptmcg at austin.rr._bogus_.com
Wed May 17 14:57:11 EDT 2006


"Khoa Nguyen" <khoa.coffee at gmail.com> wrote in message
news:mailman.5825.1147888678.27775.python-list at python.org...
<snip>
> Thanks for your reply. This looks promising, but I have a few more
questions:
> 1. If f(i) is non-terminal (e.g f(i) is another grammar expression),
> how would I adapt your idea to a more generic way?
> 2. The field delimiter is not always ',' in my case. So I guess I'll
> have to use delimtedList instead?
>
> Thanks again,
> Khoa

Khoa,

Yes, it sounds like delimitedList is the way to go.  Note that the delimiter
for delimitedList can also be an expression, it does not have to be a
literal string, or terminal.  Also, do not bother using delimitedList if
your delimiter is just whitespace - pyparsing breaks up tokens on whitespace
by default, that is:

OneOrMore(Word(alphas))

will correctly parse "This is a sentence made up of alphabetic words",
giving:

['This', 'is', 'a', 'sentence', 'made', 'up', 'of', 'alphabetic', 'words']

without having to represent the delimiting whitespace in the grammar.

-- Paul





More information about the Python-list mailing list