Skip to content

Instantly share code, notes, and snippets.

@SEJeff
Forked from fatiherikli/gist:4122058
Created March 18, 2013 03:31
Show Gist options
  • Save SEJeff/5184863 to your computer and use it in GitHub Desktop.
Save SEJeff/5184863 to your computer and use it in GitHub Desktop.
from pyparsing import *
stack = {
"my_number": 4,
"my_list": []
}
script = \
"""
push 5 to my_list.
push 4 to my_list.
add 4 to my_number.
remove 5 from my_list.
"""
sentence = Word("push" + "remove" + "add") \
+ Word(nums) \
+ Word("to" + "from") \
+ Word(alphas+"_") \
+ Literal(".")
rule = OneOrMore(Group(sentence))
for action, value, _, destination, _ in rule.parseString(script):
if action == "push":
stack[destination].append(value)
elif action == "remove":
stack[destination].remove(value)
elif action == "add":
stack[destination] += int(value)
print stack # {'my_list': ['4'], 'my_number': 8}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment