"Berteun Damman" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hello, > > I'm having some problems with pyparsing, I could not find how to tell > it to view certain words as keywords, i.e. not as a possible variable > name (in an elegant way), > for example, I have this little grammar: > > terminator = Literal(";") > expr = Word(alphas) > body = Forward(); > ifstat = "if" + body + "fi" > stat = expr | ifstat > body << OneOrMore(stat + terminator) > program = body > > I.e. some program which contains statements separated by semicolons. A > statement is either an if [....] fi statement or simply a word. > > 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. > > How to fix this? > > Thank you, > > Berteun > Berteun -
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. keywords = [ "if", "fi", "else", "return" ] def rejectKeywords(string,loc,tokens): if tokens[0] in keywords: raise ParseException(string,loc,"found keyword %s" % tokens[0]) expr.setParseAction( rejectKeywords ) I took a different tack in the idl parser that is included in the pyparsing examples directory, but it is more extensive. -- Paul -- http://mail.python.org/mailman/listinfo/python-list