Created
September 2, 2012 17:37
-
-
Save graingert/3602044 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pyparsing import Forward, Word, nums, delimitedList, Optional, StringStart, StringEnd, Group, Literal | |
list = Forward() | |
expr = Word(nums)("multiplier") + Group(Literal('(').suppress() + list + Literal(')').suppress())("list") | |
list << delimitedList(Group(expr) | Word(nums)("rawnum")) | |
prog = StringStart() + list + StringEnd() | |
print list.parseString("1,2,3") | |
print prog.parseString("1,2,3") | |
info = prog.parseString("5(1,2,3(1,2))") | |
print info | |
def handle(matches): | |
str = "" | |
for match in matches: | |
if isinstance(match,basestring): | |
str+= match | |
else: | |
mult = match["multiplier"] | |
list = match["list"] | |
str+= (handle(list) * int(mult)) | |
return str | |
print handle(info) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment