Last active
October 31, 2016 11:23
-
-
Save somebody1234/b9aa5d77ca55fe7cec4997b089de7fcb 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
Enter numeric array: [1,2,[],[3,4,5]] | |
Array | |
├1 | |
├2 | |
├Array | |
└Array | |
├3 | |
├4 | |
└5 |
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 token import Token | |
grammars = { | |
Token.Program: [ | |
[Token.Array] | |
], | |
Token.Array: [ | |
["[", Token.Elements, "]"], | |
["[]"] | |
], | |
Token.Elements: [ | |
[Token.Number, ",", Token.Elements], | |
[Token.Array, ",", Token.Elements], | |
[Token.Number], | |
[Token.Array] | |
] | |
} |
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 grammars import grammars | |
from processor import processor | |
from printtree import PrintTree | |
from token import Token | |
def Parse( | |
code, | |
index=0, | |
grammar=Token.Program | |
): | |
original_index = index | |
lexeme_index = 0 | |
for lexeme in grammars[grammar]: | |
success = True | |
index = original_index | |
tokens = [] | |
for token in lexeme: | |
if isinstance(token, Token): | |
if token == Token.Number: | |
if index == len(code): | |
success = False | |
break | |
old_index = index | |
character = code[index] | |
result = 0 | |
while character >= "0" and character <= "9": | |
index += 1 | |
if index == len(code): | |
character = "" | |
else: | |
character = code[index] | |
if old_index == index: | |
success = False | |
break | |
tokens += processor[token][0]([int(code[old_index:index])]) | |
else: | |
result = Parse(code, index, token) | |
if not result: | |
success = False | |
break | |
tokens += [result[0]] | |
index = result[1] | |
elif isinstance(token, str): | |
old_index = index | |
index += len(token) | |
if code[old_index:index] == token: | |
tokens += [token] | |
else: | |
success = False | |
break | |
else: | |
print( | |
"ParseError: Unexpected token %s found in grammar." % | |
repr(token) | |
) | |
if success: | |
return (processor[grammar][lexeme_index](tokens), index) | |
lexeme_index += 1 | |
return False | |
if __name__ == "__main__": | |
parsed = Parse(input("Enter numeric array: ")) | |
if not parsed: | |
print("Could not parse") | |
else: | |
print("Array") | |
PrintTree(parsed[0]) |
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
import re | |
def PrintTree(tree, padding=""): | |
padding = re.sub(r"└$", r" ", re.sub(r"├$", r"│", padding)) | |
new_padding = padding + "├" | |
if len(tree) > 1: | |
for item in tree[1:-1]: | |
if isinstance(item, list): | |
print(new_padding + item[0]) | |
PrintTree(item, new_padding) | |
else: | |
if isinstance(item, str): | |
print(new_padding + repr(item)[1:-1]) | |
else: | |
print(new_padding + str(item)) | |
new_padding = padding + "└" | |
item = tree[-1] | |
if isinstance(item, list): | |
print(new_padding + item[0]) | |
PrintTree(item, new_padding) | |
else: | |
if isinstance(item, str): | |
print(new_padding + repr(item)[1:-1]) | |
else: | |
print(new_padding + str(item)) |
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 token import Token | |
processor = { | |
Token.Program: [ | |
lambda result: result[0] | |
], | |
Token.Array: [ | |
lambda result: ["Array"] + result[1], | |
lambda result: ["Array"] | |
], | |
Token.Elements: [ | |
lambda result: [result[0]] + result[2], | |
lambda result: [result[0]] + result[2], | |
lambda result: [result[0]], | |
lambda result: [result[0]] | |
], | |
Token.Number: [ | |
lambda result: [str(result[0])] | |
] | |
} |
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 enum import Enum | |
class Token(Enum): | |
Program = 1 | |
Array = 2 | |
Elements = 3 | |
Number = 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment