Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
F
Final Year Project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Esteban Prince, Liam (UG - Computer Science)
Final Year Project
Commits
83d04f26
Commit
83d04f26
authored
5 years ago
by
Esteban Prince, Liam (UG - Computer Science)
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
23581bf8
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
parser.py
+48
-0
48 additions, 0 deletions
parser.py
with
48 additions
and
0 deletions
parser.py
0 → 100644
+
48
−
0
View file @
83d04f26
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
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment