Skip to content

Instantly share code, notes, and snippets.

@kelvingakuo
Last active November 19, 2021 11:30
Show Gist options
  • Save kelvingakuo/624e8b1dc3ddd4d85b0b0497b3b6ee04 to your computer and use it in GitHub Desktop.
Save kelvingakuo/624e8b1dc3ddd4d85b0b0497b3b6ee04 to your computer and use it in GitHub Desktop.
def show_tree(tree):
""" (Level order) Traverse and print parse tree
"""
queue = []
queue.append(tree)
while(len(queue) != 0):
items = len(queue)
while(items > 0):
node = queue[0]
queue.pop(0)
kids = len(node.children)
if(kids == 0):
print(node.value, end = ' ')
else:
print(f"<{node.value}>", end = ' ')
for i in range(kids):
queue.append(node.children[i])
items = items - 1
print()
# Query - SELECT col_a, col_b FROM table_name;
# Output (manually spaced for clarity)
# <Query>
# SELECT <columns> FROM <name> <terminal>
# <name> <punctuation> <name> table_name ;
# col_a , col_b
# Query - SELECT col_a, col_b FROM table_name WHERE col_d = col_e AND col_f > 20;
# Output (manually spaced)
# <Query>
# SELECT <columns> FROM <name> <condition_list>
# <name> <punctuation> <name> table_name <condition> <comparator> <condition>
# col_a , col_b <name> <operator> <term> AND <name> <operator> <term>
# col_d = <name> col_f > <integer>
# col_e 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment