Your grammar rules look like arithmetic infix notation. Pyparsing (a Python parsing add-on module) has a built-in method for building parser expressions for this kind of notation, called operatorPrecedence
. Here is how a pyparsing parser would parse your two examples:
from pyparsing import operatorPrecedence, opAssoc, ParseException
expr = operatorPrecedence("<id>",
[
('*', 2, opAssoc.LEFT),
('+', 2, opAssoc.LEFT),
])
tests = """\
<id> + <id> * <id>
<id> * <id> <id>""".splitlines()
for t in tests:
print t
try:
print expr.parseString(t, parseAll=True).asList()
except ParseException,pe:
print "parsing failed"
print
Prints:
<id> + <id> * <id>
[['<id>', '+', ['<id>', '*', '<id>']]]
<id> * <id> <id>
parsing failed
Hope this helps you in your studies.