Created
December 6, 2015 07:24
-
-
Save kn9ts/fe26d472497e8f1e3e89 to your computer and use it in GitHub Desktop.
An example of defining a programming language grammer
This file contains hidden or 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
# Expanding Exp | |
# This is very, very difficult. | |
grammar = [ | |
("exp", ["exp", "+", "exp"]), | |
("exp", ["exp", "-", "exp"]), | |
("exp", ["(", "exp", ")"]), | |
("exp", ["num"]), | |
] | |
def expand(tokens, grammar): | |
for pos in range(len(tokens)): | |
for rule in grammar: | |
if tokens[pos] == rule[0]: | |
yield tokens[0:pos] + rule[1] + tokens[pos+1:] | |
# hmmmm | |
depth = 2 | |
utterances = [["exp"]] | |
for x in range(depth): | |
for sentence in utterances: | |
utterances = utterances + [ i for i in expand(sentence, grammar)] | |
for sentence in utterances: | |
print sentence | |
# ['exp'] | |
# ['exp', '+', 'exp'] | |
# ['exp', '-', 'exp'] | |
# ['(', 'exp', ')'] | |
# ['num'] |
fuck i am not understanding shit.. need to read more and more to understand what is going on and why we are doing shit
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@kn9ts looks very interesting