Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs444
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Xun Yang
cs444
Commits
214edb06
Commit
214edb06
authored
5 years ago
by
Nicholas Robinson
Browse files
Options
Downloads
Patches
Plain Diff
cleaning & parser grammer input
parent
1745db6a
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
Parsing.py
+35
-33
35 additions, 33 deletions
Parsing.py
cfgrules
+0
-4
0 additions, 4 deletions
cfgrules
lr1trans
+0
-15
0 additions, 15 deletions
lr1trans
utils.py
+0
-21
0 additions, 21 deletions
utils.py
with
35 additions
and
73 deletions
Parsing.py
+
35
−
33
View file @
214edb06
...
...
@@ -15,20 +15,18 @@ class Node():
self
.
name
=
name
self
.
lex
=
lex
# A State has a list of rules, and a
list of accepting token
s
#
accepting: string[]
# rules:
string[]
# A State has a list of rules, and a
num indicating which state it i
s
#
num: int
# rules: string[]
class
State
():
def
__init__
(
self
):
self
.
accepting
=
[]
self
.
rules
=
[]
self
.
num
=
-
1
self
.
rules
=
[]
# addRule simply adds the string rule to rules, and adds the
# accepting token to accepting
# addRule simply adds the string rule to rules
# rule: string
def
addRule
(
self
,
rule
):
acceptingToken
=
rule
[
1
]
self
.
accepting
.
append
(
acceptingToken
)
def
addRule
(
self
,
i
,
rule
):
self
.
num
=
i
self
.
rules
.
append
(
rule
)
# go to next state depending on what states you've already visited
...
...
@@ -50,7 +48,8 @@ class State():
break
if
not
ruleToUse
:
raise
ValueError
(
'
No rule found
'
)
raise
ValueError
(
'
No rule found for next=({}) in self.rules
'
.
format
(
next
),
self
.
rules
,
statesVisited
,
rulesToOutput
)
# what type of rule is it? shift or reduce?
if
(
ruleToUse
[
2
]
==
'
shift
'
):
...
...
@@ -80,12 +79,10 @@ class State():
raise
ValueError
(
'
rule neither shift nor reduce.
'
)
def
output
(
self
):
print
(
'
accepting: [
'
),
for
i
in
self
.
accepting
:
print
(
i
+
'
,
'
),
print
(
'
]
\n
rules: [
'
),
for
i
in
self
.
rules
:
print
(
i
+
'
,
'
),
print
(
'
state
'
+
self
.
num
)
print
(
'
rules: [
'
),
for
r
in
self
.
rules
:
print
(
r
+
'
,
'
),
print
(
'
]
'
)
##################### Globals ###############################
...
...
@@ -94,7 +91,7 @@ class State():
cfgrules
=
[]
# String[][]
lr1trans
=
[]
# String[][]
theStack
=
[]
# Node[]
states
=
[
State
()
for
_
in
range
(
15
)
]
# State[]
states
=
[]
# State[]
##################### Main Functions ##########################################
...
...
@@ -161,28 +158,33 @@ def setUpGrammar():
cfgrules
=
[]
lr1trans
=
[]
theStack
=
[]
states
=
[
State
()
for
_
in
range
(
15
)]
# TODO: is there an easier way to convert this?
# '0 BOF shift 1\n1 procedures shift 2'
# => [['0','BOF','shift','1'],['1','procedures','shift','2']]
states
=
[]
# one big file
with
open
(
'
cfg/trans.txt
'
,
'
r
'
)
as
f
:
lines
=
f
.
read
().
splitlines
()
i
=
int
(
lines
[
0
])
+
1
# terminals
i
+=
int
(
lines
[
i
])
+
1
# non terminals
i
+=
1
# starting non terminal
# cfg rules
with
open
(
'
cfgrules
'
,
'
r
'
)
as
f
:
cfgrules
=
f
.
read
().
splitlines
()
for
i
,
t
in
enumerate
(
cfgrules
):
cfgrules
[
i
]
=
splitString
(
t
)
for
r
in
range
(
int
(
lines
[
i
])):
cfgrules
.
append
(
lines
[
i
+
r
+
1
].
split
())
# num of states
i
+=
int
(
lines
[
i
])
+
1
states
=
[
State
()
for
_
in
range
(
int
(
lines
[
i
]))]
# transitions
with
open
(
'
lr1trans
'
,
'
r
'
)
as
f
:
lr1trans
=
f
.
read
().
splitlines
()
for
i
,
t
in
enumerate
(
lr1trans
):
lr1trans
[
i
]
=
splitString
(
t
)
i
+=
1
for
t
in
range
(
int
(
lines
[
i
])):
lr1trans
.
append
(
lines
[
i
+
t
+
1
].
split
())
# states
for
t
in
lr1trans
:
transState
=
int
(
t
[
0
])
states
[
transState
].
addRule
(
t
)
states
[
transState
].
addRule
(
transState
,
t
)
# main parse function
# tokens: Token[]
...
...
This diff is collapsed.
Click to expand it.
cfgrules
deleted
100644 → 0
+
0
−
4
View file @
1745db6a
start BOF procedures EOF
procedures CLASS ID LBRACK dcl RBRACK
dcl INT ID ASSIGN ZERO SEMICO
dcl
\ No newline at end of file
This diff is collapsed.
Click to expand it.
lr1trans
deleted
100644 → 0
+
0
−
15
View file @
1745db6a
0 BOF shift 1
1 procedures shift 2
1 CLASS shift 5
2 EOF shift 15
5 ID shift 6
6 LBRACK shift 7
7 INT shift 8
7 dcl shift 13
8 ID shift 9
9 ASSIGN shift 10
10 ZERO shift 11
11 SEMICO shift 12
12 RBRACK reduce 2
13 RBRACK shift 14
14 EOF reduce 1
\ No newline at end of file
This diff is collapsed.
Click to expand it.
utils.py
+
0
−
21
View file @
214edb06
...
...
@@ -2,27 +2,6 @@ import string
# ------- Utility functions
# split string into array of strings
# e.g. '0 BOF shift 80' => ['0', 'BOF', 'shift', '80']
# s: string
def
splitString
(
s
):
words
=
[]
isword
=
0
for
c
in
s
:
if
c
in
"
\r\n\t
"
:
# whitespace
isword
=
0
elif
not
isword
:
words
=
words
+
[
c
]
isword
=
1
else
:
words
[
-
1
]
=
words
[
-
1
]
+
c
return
words
# count number of words in a string
# s: string
def
countWords
(
s
):
return
len
(
splitString
(
s
))
# a: stack
def
printStack
(
a
):
print
(
'
[
'
),
...
...
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