From 577991d248a2098146538f93c8fffede0acf59d0 Mon Sep 17 00:00:00 2001 From: Nicholas Robinson <nwrobins@edu.uwaterloo.ca> Date: Sun, 9 Feb 2020 23:04:24 -0500 Subject: [PATCH] parser lastrule & node children - condensed getting the last rule, no more need for copying the stack - children are now right way round, not reversed --- Parsing.py | 33 +++++++++++---------------------- Weeding.py | 23 +++++++++++++++++++++++ utils.py | 6 +++--- 3 files changed, 37 insertions(+), 25 deletions(-) create mode 100644 Weeding.py diff --git a/Parsing.py b/Parsing.py index 645d767..cb9c8be 100644 --- a/Parsing.py +++ b/Parsing.py @@ -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") diff --git a/Weeding.py b/Weeding.py new file mode 100644 index 0000000..b918cac --- /dev/null +++ b/Weeding.py @@ -0,0 +1,23 @@ +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') diff --git a/utils.py b/utils.py index 51a2a3a..623e5dd 100644 --- a/utils.py +++ b/utils.py @@ -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: -- GitLab