pyparsing and 'keywords'

Berteun Damman berteun at NO_SPAMdds.nl
Tue Dec 14 20:05:31 CET 2004


On Tue, 14 Dec 2004 18:39:19 GMT, Paul McGuire
<ptmcg at austin.rr._bogus_.com> wrote:
>> If I try however to parse the String "if test; testagain; fi;", it does
>> not work, because the fi is interpreted as an expr, not as the end of
>> the if statement, and of course, adding another fi doesn't solve this
>> either.
> The simplest way I can think of for this grammar off the top of my head is
> to use a parse action to reject keywords.

Thank you for your quick and good solution, that did the trick indeed. 

But I'm wondering, isn't it possible to have some sort of lexing phase
which already identifies keywords as such? Or to provide a table with
keywords, which pyparsing is able to automatically recognize? 

So you would be able to do IF = Keyword("if"), just as a Literal now is
created, but now the parser knows this word shouldn't be interpreted any
other way than as a keyword. Or would that be a bad idea?

Berteun



More information about the Python-list mailing list