Skip to content
Snippets Groups Projects
Commit 83d04f26 authored by Esteban Prince, Liam (UG - Computer Science)'s avatar Esteban Prince, Liam (UG - Computer Science)
Browse files

Upload New File

parent 23581bf8
No related branches found
No related tags found
No related merge requests found
from graphviz import Digraph
# Parser: parses the stream of tokens generated by the lexer and creates an abstract syntax tree
# Each token will become a node in the graph
# Nodes are represented with the Node type, each instance of which contains a "children" array which holds more Nodes
class Node:
def __init__(self, name):
self.name = name
self.children = []
def add(self, node):
self.children.append(node)
# Ingredient ::=
def ingredient():
node = Node('ingredient')
# Amount ::= Digit+ ("-" | "/" | "to")? Digit*
def amount():
node = Node('digit')
if nextToken().type == 'digit':
return node
# Digit ::= ("0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")+
def digit():
node = Node('digit')
def unit():
node = Node('unit')
def example():
text = "10 to 20 minutes"
dot = Digraph(comment='Abstract Syntax Tree')
dot.node('a', 'amount')
dot.node('d', 'digit')
dot.edge('a', 'd')
dot.node('t', '10')
dot.edge('d', 't')
dot.node('to')
dot.node('digit')
dot.node('unit')
dot.render('ast.gv', view=True)
def draw():
dot = Digraph(comment='Abstract Syntax Tree')
dot.render('ast.gv', view=True)
example()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment