Skip to content
Snippets Groups Projects
Commit 214edb06 authored by Nicholas Robinson's avatar Nicholas Robinson
Browse files

cleaning & parser grammer input

parent 1745db6a
No related branches found
No related tags found
No related merge requests found
......@@ -15,20 +15,18 @@ class Node():
self.name = name
self.lex = lex
# A State has a list of rules, and a list of accepting tokens
# accepting: string[]
# rules: string[]
# A State has a list of rules, and a num indicating which state it is
# 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(']\nrules: ['),
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[]
......
start BOF procedures EOF
procedures CLASS ID LBRACK dcl RBRACK
dcl INT ID ASSIGN ZERO SEMICO
dcl
\ No newline at end of file
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
......@@ -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('['),
......
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