Beispiel #1
0
Datei: pp2.py Projekt: pial08/PP2
def createExprTree():
    global exprTree
    global exprTreeHead
    global lastTreeHead
    global prevOperator
    global currentTreeHead
    global assgnHead
    currentTreeHead = ""
    prevOperator = ""
    exprTreeHead = ""
    lastTreeHead = ""
    assgnHead = ""
    exprTree = Tree()
    return True
Beispiel #2
0
def initExprTree():
    global exprTree
    exprTree = Tree()
    global exprTreeRoot
    exprTreeRoot = ""
    global lastTreeRoot
    lastTreeRoot = ""
    global prevOperator
    prevOperator = ""
    global currentTreeRoot
    currentTreeRoot = ""
    global assignTreeRoot
    assignTreeRoot = ""
    return True
Beispiel #3
0
Datei: pp2.py Projekt: pial08/PP2
def Expr():
    global unary

    printBool("inside Expr")
    global exprTreeHead
    global lastTreeHead
    printBool(tok)
    global assgnHead
    global exprTree
    global prevOperator
    global currentTreeHead
    prevParent = parentNode
    #changed today
    """if tok.value == const.READINT:
        return True
    
    elif tok.value == const.READLINE:
        return True
    """

    if tok.value == const.LPAREN:
        printBool("((((((( leftParen found")
        #new code added and next token removed from if
        if (updateTok() and Expr()) and (
                printBool("*****bool print token... " + str(tok.value))
                and tok.value == const.RPAREN):
            printBool("returning true.....")
            updateTok()
            if tok.value in const.operatorList:
                printBool("operator list found in lparen")
                return updateTok() and Expr()
            else:
                return True

    elif tok.value == const.MINUS:
        tree.create_node("  " + str(tok.lineno) + "$" + prefix +
                         "ArithmeticExpr:",
                         createParent("ArithmeticExpr"),
                         parent=parentNode)
        tree.create_node("  " + str(tok.lineno) + "$Operator: " +
                         str(tok.value),
                         createParent("Operator"),
                         parent=createParent("ArithmeticExpr"))
        setParent(prevParent)
        unary = True
        assgnHead = createParent("ArithmeticExpr")
        return (updateTok() and Expr())

    elif tok.value == const.NOT:
        tree.create_node("  " + str(tok.lineno) + "$" + prefix +
                         "LogicalExpr:",
                         createParent("LogicalExpr"),
                         parent=parentNode)
        tree.create_node("  " + str(tok.lineno) + "$Operator: " +
                         str(tok.value),
                         createParent("Operator"),
                         parent=createParent("LogicalExpr"))
        setParent(prevParent)
        unary = True
        assgnHead = createParent("LogicalExpr")
        return updateTok() and Expr()

    elif tok.type == const.IDENT:
        identifier = tok.value
        printBool("var " + tok.value + "found")
        #print("token position", tok[20])
        updateTok()

        if tok.value == const.EQUAL:
            exprType = "AssignExpr"
            tree.create_node("  " + str(tok.lineno) + "$" + prefix + exprType +
                             ":",
                             createParent(exprType),
                             parent=parentNode)
            resetPrefix()
            assgnHead = createParent(exprType)
            tree.create_node("  " + str(tok.lineno) + "$FieldAccess:",
                             createParent("FieldAccess"),
                             parent=assgnHead)
            tree.create_node("  " + str(tok.lineno) + "$Identifier: " +
                             identifier,
                             createParent("Identifier"),
                             parent=createParent("FieldAccess"))
            tree.create_node("  " + str(tok.lineno) + "$Operator: " +
                             str(tok.value),
                             createParent("Operator"),
                             parent=assgnHead)
        elif tok.value in const.arithmaticOperator:
            exprType = "ArithmeticExpr"
        elif tok.value in const.relationalOperator:
            exprType = "RelationalExpr"
        elif tok.value in const.equalityOperator:
            exprType = "EqualityExpr"
        elif tok.value in const.logicalOperator:
            exprType = "LogicalExpr"

        if assgnHead == "":
            assgnHead = prevParent

        printBool("checkpoint ...2 ")
        if tok.value == const.LPAREN:
            tree.create_node("  " + str(tok.lineno) + "$" + prefix + "Call:",
                             createParent("Call"),
                             parent=parentNode)
            tree.create_node("  " + str(tok.lineno) + "$Identifier: " +
                             identifier,
                             createParent("Identifier"),
                             parent=createParent("Call"))

            setParent(createParent("Call"))
            printBool("checkpoint ... 3")
            updateTok()
            return Actuals() and setParent(prevParent)

        elif (tok.value == const.EQUAL) or (tok.value in const.operatorList):
            printBool("equal found")
            if tok.value in const.operatorList:
                currentOperator = str(tok.value)
                if prevOperator == "":

                    exprTreeHead = createParent(exprType)
                    exprTree.create_node(
                        "  " + str(tok.lineno) + "$" + exprType + ":",
                        exprTreeHead)
                    exprTree.create_node("  " + str(tok.lineno) +
                                         "$FieldAccess:",
                                         createParent("FieldAccess"),
                                         parent=exprTreeHead)
                    exprTree.create_node("  " + str(tok.lineno) +
                                         "$Identifier: " + identifier,
                                         createParent("Identifier"),
                                         parent=createParent("FieldAccess"))
                    exprTree.create_node("  " + str(tok.lineno) +
                                         "$Operator: " + str(tok.value),
                                         createParent("Operator"),
                                         parent=exprTreeHead)
                    prevOperator = currentOperator
                    lastTreeHead = exprTreeHead

                elif const.precedenceList[
                        prevOperator] >= const.precedenceList[currentOperator]:

                    if currentTreeHead != "":
                        exprTree.create_node("  " + str(tok.lineno) +
                                             "$FieldAccess:",
                                             createParent("FieldAccess"),
                                             parent=currentTreeHead)

                        exprTree.create_node(
                            "  " + str(tok.lineno) + "$Identifier: " +
                            identifier,
                            createParent("Identifier"),
                            parent=createParent("FieldAccess"))
                        currentTreeHead = ""

                    else:
                        exprTree.create_node("  " + str(tok.lineno) +
                                             "$FieldAccess:",
                                             createParent("FieldAccess"),
                                             parent=exprTreeHead)

                        exprTree.create_node(
                            "  " + str(tok.lineno) + "$Identifier: " +
                            identifier,
                            createParent("Identifier"),
                            parent=createParent("FieldAccess"))

                    tempTree = Tree()
                    tempTreeHead = createParent(exprType)
                    tempTree.create_node(
                        "  " + str(tok.lineno) + "$" + exprType + ":",
                        tempTreeHead)
                    tempTree.paste(tempTreeHead, exprTree)
                    tempTree.create_node("  " + str(tok.lineno) +
                                         "$Operator: " + str(tok.value),
                                         createParent("Operator"),
                                         parent=tempTreeHead)
                    exprTree = tempTree
                    exprTreeHead = tempTreeHead
                    prevOperator = currentOperator
                    lastTreeHead = tempTreeHead

                elif const.precedenceList[prevOperator] < const.precedenceList[
                        currentOperator]:
                    tempTree = Tree()
                    tempTreeHead = createParent(exprType)
                    tempTree.create_node(
                        "  " + str(tok.lineno) + "$" + exprType + ":",
                        tempTreeHead)
                    tempTree.create_node("  " + str(tok.lineno) +
                                         "$FieldAccess:",
                                         createParent("FieldAccess"),
                                         parent=tempTreeHead)
                    tempTree.create_node("  " + str(tok.lineno) +
                                         "$Identifier: " + identifier,
                                         createParent("Identifier"),
                                         parent=createParent("FieldAccess"))
                    tempTree.create_node("  " + str(tok.lineno) +
                                         "$Operator: " + str(tok.value),
                                         createParent("Operator"),
                                         parent=tempTreeHead)
                    exprTree.paste(exprTreeHead, tempTree)
                    prevOperator = currentOperator
                    lastTreeHead = tempTreeHead
                    currentTreeHead = tempTreeHead

            return updateTok() and Expr()
        else:
            if exprTree:

                exprTree.create_node("  " + str(tok.lineno) + "$FieldAccess:",
                                     createParent("FieldAccess"),
                                     parent=lastTreeHead)
                exprTree.create_node("  " + str(tok.lineno) + "$Identifier: " +
                                     identifier,
                                     createParent("Identifier"),
                                     parent=createParent("FieldAccess"))
                tag = exprTree.__getitem__(exprTree.root).tag
                tag = tag.split("$")[0] + "$" + prefix + tag.split("$")[1]
                exprTree.update_node(exprTree.root, tag)
                tree.paste(assgnHead, exprTree)
                createExprTree()
            else:
                if unary == False:
                    tree.create_node("  " + str(tok.lineno) + "$" + prefix +
                                     "FieldAccess:",
                                     createParent("FieldAccess"),
                                     parent=assgnHead)
                else:
                    tree.create_node("  " + str(tok.lineno) + "$" +
                                     "FieldAccess:",
                                     createParent("FieldAccess"),
                                     parent=assgnHead)
                tree.create_node("  " + str(tok.lineno) + "$Identifier: " +
                                 identifier,
                                 createParent("Identifier"),
                                 parent=createParent("FieldAccess"))

                unary = False

            setParent(prevParent)
            return True

        #elif tok.value in const.operatorList:
        #    return updateTok() and Expr()

    elif tok.type in const.constantList:
        printBool("constant found")
        constantVal = str(tok.value)

        if tok.type == const.DOUBLECONSTANT:
            temp = str(tok.value).split(".")
            if temp[1] == "0" and len(temp[1]) == 1:
                constantVal = temp[0]

        constants = str(tok.type).split("_")
        if str(tok.type) == const.STRINGCONSTANT:
            con = "$" + constants[1] + ": "
        else:
            con = "$" + constants[1] + ": "
        par = constants[1]
        updateTok()

        if tok.value in const.arithmaticOperator:
            exprType = "ArithmeticExpr"
        elif tok.value in const.relationalOperator:
            exprType = "RelationalExpr"
        elif tok.value in const.equalityOperator:
            exprType = "EqualityExpr"
        elif tok.value in const.logicalOperator:
            exprType = "LogicalExpr"

        #exprType = "ArithmeticExpr"
        if assgnHead == "":
            assgnHead = prevParent

        if tok.value in const.operatorList:
            printBool("operator found after constant")
            currentOperator = str(tok.value)
            if prevOperator == "":

                exprTreeHead = createParent(exprType)
                exprTree.create_node(
                    "  " + str(tok.lineno) + "$" + exprType + ":",
                    exprTreeHead)
                exprTree.create_node("  " + str(tok.lineno) + con +
                                     constantVal,
                                     createParent(par),
                                     parent=exprTreeHead)
                exprTree.create_node("  " + str(tok.lineno) + "$Operator: " +
                                     str(tok.value),
                                     createParent("Operator"),
                                     parent=exprTreeHead)
                prevOperator = currentOperator
                lastTreeHead = exprTreeHead

            elif const.precedenceList[prevOperator] >= const.precedenceList[
                    currentOperator]:

                if currentTreeHead != "":

                    exprTree.create_node("  " + str(tok.lineno) + con +
                                         constantVal,
                                         createParent(par),
                                         parent=currentTreeHead)
                    currentTreeHead = ""

                else:

                    exprTree.create_node("  " + str(tok.lineno) + con +
                                         constantVal,
                                         createParent(par),
                                         parent=exprTreeHead)

                tempTree = Tree()
                tempTreeHead = createParent(exprType)
                tempTree.create_node(
                    "  " + str(tok.lineno) + "$" + exprType + ":",
                    tempTreeHead)
                tempTree.paste(tempTreeHead, exprTree)
                tempTree.create_node("  " + str(tok.lineno) + "$Operator: " +
                                     str(tok.value),
                                     createParent("Operator"),
                                     parent=tempTreeHead)
                exprTree = tempTree
                exprTreeHead = tempTreeHead
                prevOperator = currentOperator
                lastTreeHead = tempTreeHead

            elif const.precedenceList[prevOperator] < const.precedenceList[
                    currentOperator]:
                tempTree = Tree()
                tempTreeHead = createParent(exprType)
                tempTree.create_node(
                    "  " + str(tok.lineno) + "$" + exprType + ":",
                    tempTreeHead)
                tempTree.create_node("  " + str(tok.lineno) + con +
                                     constantVal,
                                     createParent(par),
                                     parent=tempTreeHead)
                tempTree.create_node("  " + str(tok.lineno) + "$Operator: " +
                                     str(tok.value),
                                     createParent("Operator"),
                                     parent=tempTreeHead)
                exprTree.paste(exprTreeHead, tempTree)
                prevOperator = currentOperator
                lastTreeHead = tempTreeHead
                currentTreeHead = tempTreeHead

            return updateTok() and Expr()
            #return True
        elif tok.value == '.':
            updateTok()
            reportError(tok)
            return False

        else:
            #changed from return true to return Expr()
            if exprTree:
                exprTree.create_node("  " + str(tok.lineno) + con +
                                     constantVal,
                                     createParent(par),
                                     parent=lastTreeHead)
                tag = exprTree.__getitem__(exprTree.root).tag
                tag = tag.split("$")[0] + "$" + prefix + tag.split("$")[1]
                exprTree.update_node(exprTree.root, tag)
                tree.paste(assgnHead, exprTree)
                createExprTree()

            else:
                if unary == False:
                    tree.create_node("  " + str(tok.lineno) + "$" + prefix +
                                     constants[1] + ": " + constantVal,
                                     createParent(par),
                                     parent=assgnHead)
                else:
                    tree.create_node("  " + str(tok.lineno) + "$" +
                                     constants[1] + ": " + constantVal,
                                     createParent(par),
                                     parent=assgnHead)

                unary = False

            setParent(prevParent)
            return True

    #elif tok.value == const.RPAREN:
    #    printBool("this part executed ***********************", tok)
    #    return True
    #ReadInteger ( )
    elif tok.value == const.READINT or tok.value == const.READLINE:
        printBool("readline or readint found............................")
        if assgnHead == "":
            assgnHead = prevParent
        tree.create_node("  " + str(tok.lineno) + "$" + str(tok.value) +
                         "Expr:",
                         createParent(str(tok.value)),
                         parent=assgnHead)
        if (updateTok() and tok.value == const.LPAREN) and (
                updateTok() and tok.value == const.RPAREN):
            updateTok()
            return True
        else:
            reportError(tok)
            return False
    else:
        printBool("some error...")
        reportError(tok)
        return False
Beispiel #4
0
Datei: pp2.py Projekt: pial08/PP2
import sys
import const
import lexanalysis
from treelib.treelib import Node, Tree

boolPrint = True

tree = Tree()
tree.create_node("   $Program:", "Program")  # root node
prevOperator = ""
parentNode = "Program"
prefix = ""

tok = lexanalysis.getNextToken()


def resetPrefix():
    global prefix
    prefix = ""
    return True


def setPrefix(st):
    global prefix
    prefix = st
    return True


def findCol(tok):
    return lexanalysis.find_column(lexanalysis.contents, tok)
Beispiel #5
0
def expr():
    printDebug("------expr " + str(t))
    global exprTreeRoot
    global lastTreeRoot
    global assignTreeRoot
    global exprTree
    global prevOperator
    global currentTreeRoot
    prevPar = parent
    global unary

    if t.value == tokens.T_Minus:
        astree.create_node("  " + str(t.lineno) + "$" + prefix + "ArithmeticExpr:", find_node_id(t, "ArithmeticExpr"), parent=prevPar)
        astree.create_node("  " + str(t.lineno) + "$Operator: " + str(t.value), find_node_id(t, "Operator"), parent=find_node_id(t, "ArithmeticExpr"))
        update_parent(prevPar)
        unary = True
        assignTreeRoot = find_node_id(t, "ArithmeticExpr")
        return next_token() and expr()
    
    elif t.value == tokens.T_LP:
        printDebug("LeftParen found from expr")
        if next_token() and expr() and t.value == tokens.T_RP:
            next_token()
            if t.value in tokens.op_list:
                printDebug("op_list found inside LeftParen expr")
                return next_token() and expr()
            else:
                printDebug("true from expr LeftParen cond")
                return True

    elif t.value == tokens.T_Not:
        astree.create_node("  " + str(t.lineno) + "$" + prefix + "LogicalExpr:", find_node_id(t, "LogicalExpr"), parent=prevPar)
        astree.create_node("  " + str(t.lineno) + "$Operator: " + str(t.value), find_node_id(t, "Operator"), parent=find_node_id(t, "LogicalExpr"))
        update_parent(prevPar)
        unary = True
        assignTreeRoot = find_node_id(t, "LogicalExpr")
        return next_token() and expr()

    elif t.type == tokens.T_Identifier:
        ident = t.value
        next_token()

        if(t.value == tokens.T_Equal):
            exprType = "AssignExpr"
            astree.create_node("  " + str(t.lineno) + "$" + prefix + exprType + ":", find_node_id(t, exprType), parent=prevPar)
            clear_prefix()
            assignTreeRoot = find_node_id(t, exprType)
            astree.create_node("  " + str(t.lineno) + "$" + "FieldAccess" + ":", find_node_id(t, "FieldAccess"), parent=assignTreeRoot)
            astree.create_node("  " + str(t.lineno) + "$" + "Identifier" + ": " + ident, find_node_id(t, "Identifier"), parent=find_node_id(t, "FieldAccess"))
            astree.create_node("  " + str(t.lineno) + "$" + "Operator" + ": " + str(t.value), find_node_id(t, "Operator"), parent=assignTreeRoot)
        elif t.value in tokens.arithmatic_op:
            exprType = "ArithmeticExpr"
        elif t.value in tokens.relational_op:
            exprType = "RelationalExpr"
        elif t.value in tokens.equality_op:
            exprType = "EqualityExpr"
        elif t.value in tokens.logical_op:
            exprType = "LogicalExpr"
        
        if assignTreeRoot == "":
            assignTreeRoot = prevPar

        if t.value == tokens.T_LP:
            astree.create_node("  " + str(t.lineno) + "$" + prefix + "Call:", find_node_id(t, "Call"), parent= prevPar)
            astree.create_node("  " + str(t.lineno) + "$" + "Identifier" + ": " + ident, find_node_id(t, "Identifier"), parent=find_node_id(t, "Call"))
            update_parent(find_node_id(t, "Call"))
            next_token()
            return actuals() and update_parent(prevPar)

        elif (t.value == tokens.T_Equal) or (t.value in tokens.op_list):

            if t.value in tokens.op_list:
                currOperator = str(t.value)
                if prevOperator == "":
                    exprTreeRoot = find_node_id(t, exprType)
                    exprTree.create_node("  " + str(t.lineno) + "$" + exprType + ":", exprTreeRoot)
                    exprTree.create_node("  " + str(t.lineno) + "$" + "FieldAccess" + ":", find_node_id(t, "FieldAccess"), parent=exprTreeRoot)
                    exprTree.create_node("  " + str(t.lineno) + "$" + "Identifier" + ": " + ident, find_node_id(t, "Identifier"), parent=find_node_id(t, "FieldAccess"))
                    exprTree.create_node("  " + str(t.lineno) + "$" + "Operator" + ": " + currOperator, find_node_id(t, "Operator"), parent=exprTreeRoot)
                    prevOperator = currOperator
                    lastTreeRoot = exprTreeRoot

                elif tokens.precedence_list[prevOperator] >= tokens.precedence_list[currOperator]:

                    if currentTreeRoot != "":
                        exprTree.create_node("  " + str(t.lineno) + "$FieldAccess:", find_node_id(t, "FieldAccess"), parent=currentTreeRoot)
                        exprTree.create_node("  " + str(t.lineno) + "$Identifier: " + ident, find_node_id(t, "Identifier"), parent=find_node_id(t, "FieldAccess"))
                        currentTreeRoot = ""
                    
                    else:
                        exprTree.create_node("  " + str(t.lineno) + "$FieldAccess:", find_node_id(t, "FieldAccess"), parent=exprTreeRoot)
                        exprTree.create_node("  " + str(t.lineno) + "$Identifier: " + ident, find_node_id(t, "Identifier"), parent=find_node_id(t, "FieldAccess"))
                    
                    tempTree=Tree()
                    tempTreeRoot = find_node_id(t, exprType)
                    tempTree.create_node("  " + str(t.lineno) + "$" + exprType + ":", tempTreeRoot)
                    tempTree.paste(tempTreeRoot, exprTree)
                    tempTree.create_node("  " + str(t.lineno) + "$Operator: " + currOperator, find_node_id(t, "Operator"), parent=tempTreeRoot)            
                    exprTree = tempTree
                    exprTreeRoot = tempTreeRoot
                    prevOperator = currOperator
                    lastTreeRoot = tempTreeRoot

                elif tokens.precedence_list[prevOperator] < tokens.precedence_list[currOperator]:
                    tempTree=Tree()
                    tempTreeRoot = find_node_id(t, exprType)
                    tempTree.create_node("  " + str(t.lineno) + "$" + exprType + ":", tempTreeRoot)
                    tempTree.create_node("  " + str(t.lineno) + "$FieldAccess:", find_node_id(t, "FieldAccess"), parent=tempTreeRoot)
                    tempTree.create_node("  " + str(t.lineno) + "$Identifier: " + ident, find_node_id(t, "Identifier"), parent=find_node_id(t, "FieldAccess"))
                    tempTree.create_node("  " + str(t.lineno) + "$Operator: " + currOperator, find_node_id(t, "Operator"), parent=tempTreeRoot)            
                    exprTree.paste(exprTreeRoot, tempTree)
                    prevOperator = currOperator
                    lastTreeRoot = tempTreeRoot
                    currentTreeRoot = tempTreeRoot
                
            next_token()
            return expr()
        else:
            if exprTree:
                exprTree.create_node("  " + str(t.lineno) + "$" + "FieldAccess" + ":", find_node_id(t, "FieldAccess"), parent=lastTreeRoot)
                exprTree.create_node("  " + str(t.lineno) + "$" + "Identifier" + ": " + ident, find_node_id(t, "Identifier"), parent=find_node_id(t, "FieldAccess"))
                label = exprTree.__getitem__(exprTree.root).tag
                label = label.split("$")[0] + "$" + prefix + label.split("$")[1]
                exprTree.update_node(exprTree.root, label)
                astree.paste(assignTreeRoot, exprTree)
                initExprTree()
            else:
                if unary == False:
                    astree.create_node("  " + str(t.lineno) + "$" + prefix + "FieldAccess" + ":", find_node_id(t, "FieldAccess"), parent=assignTreeRoot)
                else:
                    astree.create_node("  " + str(t.lineno) + "$" + "FieldAccess" + ":", find_node_id(t, "FieldAccess"), parent=assignTreeRoot)
                unary = False
                astree.create_node("  " + str(t.lineno) + "$" + "Identifier" + ": " + ident, find_node_id(t, "Identifier"), parent=find_node_id(t, "FieldAccess"))
            return True and update_parent(prevPar)
    
    elif t.type in tokens.const_list:
        
        constantVal = str(t.value)
        # done for doublecontants only hex and int needed to add
        if t.type == tokens.T_DoubleConstant:
            if(constantVal[-2:] == '.0'):
                constantVal = constantVal[:-2]

        # need update for constant without args
        constant = str(t.type).split("_")[1]

        """node_id = find_node_id(t, constant)
        node_label = "  " + str(t.lineno) + "." + "(args) " + constant + ": " + constantVal
        astree.create_node( node_label, node_id, parent=parent)"""
        
        next_token()
        
        if t.value in tokens.arithmatic_op:
            exprType = "ArithmeticExpr"
        elif t.value in tokens.relational_op:
            exprType = "RelationalExpr"
        elif t.value in tokens.equality_op:
            exprType = "EqualityExpr"
        elif t.value in tokens.logical_op:
            exprType = "LogicalExpr"
        if assignTreeRoot == "":
            assignTreeRoot = prevPar

        if (t.value in tokens.op_list):
            currOperator = str(t.value)
            if prevOperator == "":
                exprTreeRoot = find_node_id(t, exprType)
                exprTree.create_node("  " + str(t.lineno) + "$" + exprType + ":", exprTreeRoot)
                exprTree.create_node("  " + str(t.lineno) + "$" + constant + ": " +  constantVal, find_node_id(t, constant), parent=exprTreeRoot)
                exprTree.create_node("  " + str(t.lineno) + "$" + "Operator" + ": " + currOperator, find_node_id(t, "Operator"), parent=exprTreeRoot)
                prevOperator = currOperator
                lastTreeRoot = exprTreeRoot

            elif tokens.precedence_list[prevOperator] >= tokens.precedence_list[currOperator]:

                if currentTreeRoot != "":
                    exprTree.create_node("  " + str(t.lineno) + "$" + constant + ": " +  constantVal, find_node_id(t, constant), parent=currentTreeRoot)
                    currentTreeRoot = ""
                
                else:
                    exprTree.create_node("  " + str(t.lineno) + "$" + constant + ": " +  constantVal, find_node_id(t, constant), parent=exprTreeRoot)
                
                tempTree=Tree()
                tempTreeRoot = find_node_id(t, exprType)
                tempTree.create_node("  " + str(t.lineno) + "$" + exprType + ":", tempTreeRoot)
                tempTree.paste(tempTreeRoot, exprTree)
                tempTree.create_node("  " + str(t.lineno) + "$Operator: " + currOperator, find_node_id(t, "Operator"), parent=tempTreeRoot)            
                exprTree = tempTree
                exprTreeRoot = tempTreeRoot
                prevOperator = currOperator
                lastTreeRoot = tempTreeRoot

            elif tokens.precedence_list[prevOperator] < tokens.precedence_list[currOperator]:
                tempTree=Tree()
                tempTreeRoot = find_node_id(t, exprType)
                tempTree.create_node("  " + str(t.lineno) + "$" + exprType + ":", tempTreeRoot)
                tempTree.create_node("  " + str(t.lineno) + "$" + constant + ": " +  constantVal, find_node_id(t, constant), parent=tempTreeRoot)
                tempTree.create_node("  " + str(t.lineno) + "$Operator: " + currOperator, find_node_id(t, "Operator"), parent=tempTreeRoot)            
                exprTree.paste(exprTreeRoot, tempTree)
                prevOperator = currOperator
                lastTreeRoot = tempTreeRoot
                currentTreeRoot = tempTreeRoot

            return next_token() and expr()
        elif t.value == ".":
            next_token()
            handleError(t)
            return False
        else:
            printDebug("ture from expr Constant" + str(t.value))

            if exprTree:
                exprTree.create_node("  " + str(t.lineno) + "$" + constant + ": " +  constantVal, find_node_id(t, constant), parent=lastTreeRoot)
                label = exprTree.__getitem__(exprTree.root).tag
                label = label.split("$")[0] + "$" + prefix + label.split("$")[1]
                exprTree.update_node(exprTree.root, label)
                astree.paste(assignTreeRoot, exprTree)
                initExprTree()
            else:
                if unary == False:
                    astree.create_node("  " + str(t.lineno) + "$" + prefix + constant + ": " +  constantVal, find_node_id(t, constant), parent=assignTreeRoot)
                else:
                    astree.create_node("  " + str(t.lineno) + "$" + constant + ": " +  constantVal, find_node_id(t, constant), parent=assignTreeRoot)
                unary = False
            return True and update_parent(prevPar)
    
    elif t.value == tokens.T_ReadInteger or t.value == tokens.T_ReadLine:
        if assignTreeRoot == "":
            assignTreeRoot = prevPar
        printDebug("ReadInteger or ReadLine found")
        exprType = str(t.value) + "Expr"
        astree.create_node("  " + str(t.lineno) + "$" + exprType + ":", find_node_id(t, exprType), parent=assignTreeRoot)

        if (next_token() and t.value == tokens.T_LP) and (next_token() and t.value == tokens.T_RP):
            next_token()
            return True
        else:
            handleError(t)
            return False

    else:
        printDebug("exit expr false " + str(t.value))
        handleError(t)
        return False
Beispiel #6
0
"""
@author: iqrah
"""

import sys
import tokens
import phase_1
from treelib.treelib import Node, Tree

astree = Tree()
astree.create_node("   $Program:", "Program") # abstract syntax tree root
parent = "Program"
prefix = ""

errorFound = False

def printDebug(str):
    #print(str)
    return True

def printFunction(str):
    global errorFound 
    if not errorFound:
        print(str)
    return True

t = phase_1.get_next_token()
printDebug(t)

def next_token():
    global t