diff --git a/Parsing.py b/Parsing.py index 645d767c9e343b4b10be704f22d55f5a650c2f12..cb9c8be9f0ad854a33eece291941cf7161aadc05 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 0000000000000000000000000000000000000000..b918cac3e9c56995d4890991effb9e6a9ad44cc2 --- /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 51a2a3ad1e2411dfa4ad8c645ddec1b4f15a8ea8..623e5dd09b075abbe2e0c4c717148e5abcf48498 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: