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

parser lastrule & node children

- condensed getting the last rule, no more need for copying the stack
- children are now right way round, not reversed
parent e5f0fda4
No related branches found
No related tags found
No related merge requests found
......@@ -65,9 +65,8 @@ class State():
newChildren = [] # Node[]
while (toPop != 0):
statesVisited.pop()
top = theStack.pop()
newChildren.append(Node(top.children, top.name, top.lex))
newChildren.insert(0, Node(top.children, top.name, top.lex))
toPop -= 1
......@@ -127,27 +126,16 @@ def checkSequence(tokens):
getNext = states[curState].trans(statesVisited, tokens[i].name, rulesToOutput)
copyStack = theStack
if copyStack:
lastRule = copyStack[-1].name
theRule = ''
copyStack.pop()
while copyStack:
lastRule = copyStack[-1].name + ' ' + lastRule
copyStack.pop()
for r in cfgrules:
if (' '.join(r)).find(lastRule) != -1:
theRule = r
break
if theRule != '':
rulesToOutput.append(' '.join(theRule))
else:
print('last rule not found')
lastRule = True
for index, token in enumerate(theStack):
if token.name != cfgrules[0][index+1]:
lastRule = False
break
if lastRule:
rulesToOutput.append(' '.join(cfgrules[0]))
else:
raise ValueError('last rule not found')
# printNodeStack(theStack)
return rulesToOutput
# set up grammar from files and set up states
......@@ -200,4 +188,5 @@ def parse(tokens):
except ValueError as err:
return (None, err)
# printNodeStack(theStack)
return (result, "success")
import string
##################### Weeding ##########################################
# node: Node[]
def oneConstructor(node, insideClass):
success = False
if not insideClass:
if node.name == 'classDcl':
for c in node.children:
success = oneConstructor(c, True)
if success:
return True
elif insideClass:
if node.name == 'constructorDcl':
return True
# tree: Node[]
def weed(tree):
# Every class must contain at least one explicit constructor
if oneConstructor(tree, False) == False:
print('a class does not have an explicit constructor')
......@@ -13,14 +13,14 @@ def printStack(a):
def printNode(a):
print(a.name),
if (a.lex != ''):
print(' ' + a.lex),
print(a.lex),
for i in reversed(a.children):
for i in a.children:
print(i.name),
if a.children:
print('\n<children>')
for i in reversed(a.children):
for i in a.children:
printNode(i)
print('</children>')
else:
......
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