def createTreeHelper(): nextToken = stack.pop() newNode = None if nextToken == "(": newNode = createTreeHelper() elif scanner.isConstant(nextToken): newNode = Node(nextToken) elif scanner.isOperator(nextToken): newNode = Node(nextToken) leftOperand = stack.pop() if leftOperand == "(": newNode.leftChild = createTreeHelper() elif scanner.isConstant(leftOperand): newNode.leftChild = Node(leftOperand) else: raise CreateParseTreeException(stack) if scanner.getArgCount(nextToken) == 2: rightOperand = stack.pop() if rightOperand == "(": newNode.rightChild = createTreeHelper() elif scanner.isConstant(rightOperand): newNode.rightChild = Node(rightOperand) else: raise CreateParseTreeException(stack) else: raise CreateParseTreeException(stack) lastToken = stack.pop() if lastToken != ")": raise CreateParseTreeException(stack) return newNode
def printTreeTraversalHelper(currentNode): if currentNode is None: return None leftType = printTreeTraversalHelper(currentNode.leftChild) rightType = printTreeTraversalHelper(currentNode.rightChild) if scanner.isConstant(currentNode.data): if scanner.isReal(currentNode.data): print currentNode.data + "e " elif scanner.isString(currentNode.data): print "s\" " + currentNode.data[1:] else: print currentNode.data return (scanner.getToken(currentNode.data)).Type elif scanner.isOperator(currentNode.data): numberOperands = scanner.getArgCount(currentNode.data) if numberOperands == 1 and rightType is None: # if operand is Real add a f in front of the operator if leftType == "Real" and rightType is None: print "f" + currentNode.data #if there is an integer using a real operation then convert the integer to real if leftType == "Integer" and scanner.isRealOperator(currentNode.data): print currentNode.data print "s>f" #is boolean type child and operator is boolean if leftType == "Boolean" and scanner.isBooleanOperator(currentNode.data): print itblToGforth(currentNode.data) #is integer type child and is integer operator if leftType == "Integer" and not scanner.isBooleanOperator(currentNode.data): print currentNode.data else: raise ParseTreeException(currentNode) return leftType elif numberOperands == 2: #children are same value no conversion needed if leftType == rightType: # children are real and real operator if leftType == "Real" and scanner.isRealOperator(currentNode.data): print "f" + itblToGforth(currentNode.data) + " " # children are string and a + elif leftType == "String": if currentNode.data == "+": print "s" + itblToGforth(currentNode.data) + " " else: raise ParseTreeException(currentNode) # children are boolean and boolean operator elif leftType == "Boolean" and scanner.isBooleanOperator(currentNode.data): print itblToGforth(currentNode.data) # children are integers and integer operator elif leftType == "Integer" and not scanner.isBooleanOperator(currentNode.data): if currentNode.data == "^": print "s>f" print "s>f" print "f" + itblToGforth(currentNode.data) leftType = "Real" else: print itblToGforth(currentNode.data) # bad parse else: raise ParseTreeException(currentNode) return leftType # type conversion needed to float elif (((leftType == "Integer" and rightType == "Real") or (leftType == "Real" and rightType == "Integer")) and not scanner.isBooleanOperator(currentNode.data)): print "s>f" if leftType == "Integer": print "fswap" print "f" + itblToGforth(currentNode.data) return "Real" else: raise ParseTreeException(currentNode) else: return None