Skip to content

Instantly share code, notes, and snippets.

Embed
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())
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.