By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,661 Members | 779 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,661 IT Pros & Developers. It's quick & easy.

Parser of parentheses in arithmetical expressions

P: 1
from pyparsing import *
cislo = Word(nums)
levaZavorka = Suppress('(')
pravaZavorka = Suppress(')')

vyraz = Forward()
vyraz << Or( [cislo,
Group(levaZavorka + vyraz + "+" + vyraz + pravaZavorka),
Group(levaZavorka + vyraz + "-" + vyraz + pravaZavorka),
Group(levaZavorka + vyraz + "*" + vyraz + pravaZavorka)] )

celyVyraz = vyraz + StringEnd()

so we need to change vyraz

it must work like that

print(celyVyraz.parseString('3'))
print(celyVyraz.parseString('(3+5)'))
print(celyVyraz.parseString('((3+5)-2)'))
print(celyVyraz.parseString('((3+5)-(2*4))'))
print(celyVyraz.parseString('((3+5)+4))'))
print(celyVyraz.parseString('(((3+5)-4)-2))'))
print(celyVyraz.parseString('3+5-2*4'))

Thank you!
Mar 31 '17 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.