#!/usr/bin/python

import pyparsing as PP
PP.ParserElement.enablePackrat()

# Simple test grammar:
Ident = PP.Word(PP.alphas)
One   = PP.Group("One" + Ident)
Two   = PP.Group("Two" + One)
Three = PP.Group("Three" + Two)
Cnfsr = PP.Group(Ident + Ident + Two)
Statement = PP.OneOrMore(Cnfsr | One | Two | Three) + PP.StringEnd()

# Valid string:
Valid = "Three Two One Zero One Monkey Two One Banana"
print list(Statement.parseString(Valid))

# Invalid string:
Invalid = "Three Two Four Zero One Monkey Two One Banana"
try:
    print list(Statement.parseString(Invalid))
except PP.ParseException:
    print "Parse made it to character %d" % PP.LastParseLoc