Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
simple arithmetic expression parser
from pyparsing import Word, alphas, oneOf, operatorPrecedence, opAssoc
operand = Word(alphas)
signop = oneOf('+ -')
multop = oneOf('* /')
plusop = oneOf('+ -')
ARITH_EXPR = operatorPrecedence( operand,
[(signop, 1, opAssoc.RIGHT),
(multop, 2, opAssoc.LEFT),
(plusop, 2, opAssoc.LEFT),]
if __name__ == "__main__":
print ARITH_EXPR.parseString(raw_input())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment