예제 #1
0
        public override string ToString()
        {
            CommonAST walker = (CommonAST)NodeASTSubTree.getFirstChild();

            string Text = m_Label + walker.getText();

            walker = (CommonAST)walker.getNextSibling();

            while (walker.Type != BoolParserTokenTypes.ASSIGN)
            {
                Text  += "," + walker.getText();
                walker = (CommonAST)walker.getNextSibling();
            }
            Text  += ":=";
            walker = (CommonAST)walker.getNextSibling();
            while (walker != null)
            {
                Text += HelperFunctions.ExprToString(walker);
                if (walker.getNextSibling() != null)
                {
                    Text += ",";
                }
                walker = (CommonAST)walker.getNextSibling();
            }
            Text += ';';

            return(Text);
        }
예제 #2
0
        private List <SemanticError> CheckExpression(CommonAST EXPR, List <SemanticError> Errors,
                                                     List <string> LocalVariables, string ProcOf)
        {
            CommonAST walker = (CommonAST)EXPR.getFirstChild();

            while (walker != null)
            {
                switch (walker.Type)
                {
                case (BoolParserTokenTypes.ID):
                    if (!(LocalVariables.Contains(walker.getText())) &&
                        !(GlobalVariables.Contains(walker.getText())))
                    {
                        Errors.Add(new SemanticError(BoolSemanticErrorTypes.VAR_UNDECLARED,
                                                     walker.getText(), ProcOf));
                    }
                    break;

                case (BoolParserTokenTypes.EXPR):
                    Errors = CheckExpression(walker, Errors, LocalVariables, ProcOf);
                    break;
                }

                walker = (CommonAST)walker.getNextSibling();
            }

            return(Errors);
        }
예제 #3
0
        public static string DeclToString(CommonAST Decl)
        {
            Debug.Assert(Decl != null);

            string output = "decl ";

            Decl = (CommonAST)Decl.getFirstChild();
            bool first = true;

            while (Decl != null)
            {
                if (!first)
                {
                    output = output + ", ";
                }
                output = output + Decl.getText();

                Decl  = (CommonAST)Decl.getNextSibling();
                first = false;
            }

            output = output + ";";

            return(output);
        }
예제 #4
0
        //This Procedure assumes Procedure is a PROC AST node
        private List <SemanticError> CheckProcedure(CommonAST Procedure, List <SemanticError> Errors)
        {
            List <string> LocalVariables = new List <string>();

            CommonAST walker = Procedure;

            walker = (CommonAST)walker.getFirstChild();
            string ProcedureName = walker.getText();

            walker = (CommonAST)walker.getFirstChild();

            #region Variables checking
            while (walker.Type == BoolParserTokenTypes.ID)
            {
                string variable = walker.ToString();
                if (LocalVariables.Contains(variable))
                {
                    Errors.Add(new SemanticError(BoolSemanticErrorTypes.VAR_NAME_CLASH, variable, ProcedureName));
                }
                else
                {
                    LocalVariables.Add(variable);
                }

                walker = (CommonAST)walker.getNextSibling();
            }

            while ((walker.Type == BoolParserTokenTypes.LITERAL_decl))
            {
                CommonAST subWalker = (CommonAST)walker.getFirstChild();

                while (subWalker != null)
                {
                    string variable = subWalker.ToString();
                    if (LocalVariables.Contains(variable))
                    {
                        Errors.Add(new SemanticError(BoolSemanticErrorTypes.VAR_NAME_CLASH, variable, ProcedureName));
                    }
                    else
                    {
                        LocalVariables.Add(variable);
                    }

                    subWalker = (CommonAST)subWalker.getNextSibling();
                }

                walker = (CommonAST)walker.getNextSibling();
            }
            #endregion


            Errors = CheckSSEQ(walker, Errors, LocalVariables, ProcedureName);

            return(Errors);
        }
예제 #5
0
 public static string DeciderToString(CommonAST Decider)
 {
     Decider = (CommonAST)Decider.getFirstChild();
     if (Decider.Type == BoolParserTokenTypes.EXPR)
     {
         return(HelperFunctions.ExprToString(Decider));
     }
     else
     {
         return(Decider.getText());
     }
 }
예제 #6
0
        private formattedCodeView getFormattedProcedure(CommonAST procedure, formattedCodeView formattedCode)
        {
            CommonAST walker = (CommonAST)procedure.getFirstChild();

            string procedureHead = string.Empty;

            procedureHead += walker.getText() + "(";
            walker         = (CommonAST)walker.getFirstChild();

            bool first = true;

            while (walker.Type == BoolParserTokenTypes.ID)
            {
                if (!first)
                {
                    procedureHead += ", ";
                }
                procedureHead += walker.getText();

                walker = (CommonAST)walker.getNextSibling();
                first  = false;
            }
            procedureHead += ")";

            formattedCode.addLine(procedureHead);
            formattedCode.addLine("begin");

            while (walker.Type == BoolParserTokenTypes.LITERAL_decl)
            {
                formattedCode.addLine("  " + HelperFunctions.DeclToString(walker));

                walker = (CommonAST)walker.getNextSibling();
            }

            formattedCode = getFormattedSSEQ(walker, formattedCode, "  ");

            formattedCode.addLine("end");

            return(formattedCode);
        }
예제 #7
0
        private int DrawAST(CommonAST AST, ref Graph g)
        {
            if (AST != null)
            {
                int thisNodeIndex = ASTforGLEEIndex++;

                ASTorderedByGraphIndex.Add(AST);

                Node thisNode = (Node)g.AddNode(thisNodeIndex.ToString());
                thisNode.Attr.Label = AST.getText();

                CommonAST ChildAST = (CommonAST)AST.getFirstChild();

                int previousChildIndex = -1;

                for (int i = 0; i < AST.getNumberOfChildren(); i++)
                {
                    int childIndex = DrawAST(ChildAST, ref g);

                    if (childIndex != -1)
                    {
                        g.AddEdge(thisNodeIndex.ToString(), childIndex.ToString());

                        /*                        if (previousChildIndex != -1)
                         *                      {
                         *                          Edge SiblingEdge = (Edge)g.AddEdge(previousChildIndex.ToString(),
                         *                                                              childIndex.ToString());
                         *                          SiblingEdge.Attr.Styles = new Style[] { Microsoft.Glee.Drawing.Style.Dashed };
                         *                      }
                         */
                    }

                    previousChildIndex = childIndex;

                    ChildAST = (CommonAST)ChildAST.getNextSibling();
                }

                return(thisNodeIndex);
            }
            else
            {
                return(-1);
            }
        }
예제 #8
0
        public override string ToString()
        {
            CommonAST walker = (CommonAST)NodeASTSubTree.getFirstChild();
            string    Text   = m_Label + walker.getText() + "(";

            walker = (CommonAST)walker.getFirstChild();
            while (walker != null)
            {
                Text += HelperFunctions.ExprToString(walker);
                if (walker.getNextSibling() != null)
                {
                    Text += ",";
                }
                walker = (CommonAST)walker.getNextSibling();
            }
            Text += ");";

            return(Text);
        }
예제 #9
0
        public static string ExprToString(CommonAST Expression)
        {
            Debug.Assert(Expression != null);

            string output = String.Empty;

            Expression = (CommonAST)Expression.getFirstChild();
            while (Expression != null)
            {
                if (Expression.Type != BoolParserTokenTypes.EXPR)
                {
                    output += Expression.getText();
                }
                else
                {
                    output += ExprToString(Expression);
                }

                Expression = (CommonAST)Expression.getNextSibling();
            }

            return(output);
        }
예제 #10
0
        public static Bdd ExprToBdd(CommonAST Expr, BddManager Manager,
                                    Dictionary <string, int> LocalVarNameToId, Dictionary <string, int> GlobalVarNameToId)
        {
            Debug.Assert(Expr.Type == BoolParserTokenTypes.EXPR);

            Bdd ExprBdd = null;

            CommonAST walker = (CommonAST)Expr.getFirstChild();

            switch (walker.Type)
            {
            case BoolParserTokenTypes.EXPR:
                ExprBdd = ExprToBdd(walker, Manager, LocalVarNameToId, GlobalVarNameToId);
                break;

            case BoolParserTokenTypes.EMARK:
                walker = (CommonAST)walker.getNextSibling();

                Bdd tempBdd = ExprToBdd(walker, Manager, LocalVarNameToId, GlobalVarNameToId);
                ExprBdd = Manager.LogicalNot(tempBdd);

                tempBdd.FreeBdd();
                break;

            case BoolParserTokenTypes.ID:
                int VariableID;
                if (!LocalVarNameToId.TryGetValue(walker.getText(), out VariableID))
                {
                    if (!GlobalVarNameToId.TryGetValue(walker.getText(), out VariableID))
                    {
                        Debug.Assert(false);    //Varijabla mora biti ili medju lokalnim ili medju globalnim
                    }
                }
                ExprBdd = Manager.GetBddVariableWithID(VariableID);
                break;

            case BoolParserTokenTypes.CONST:
                if (walker.getText() == "1")
                {
                    ExprBdd = Manager.CreateBddOne();
                }
                else
                {
                    ExprBdd = Manager.CreateBddZero();
                }
                break;

            default:
                Debug.Assert(false);
                break;
            }

            if ((walker.getNextSibling() != null) &&
                ((walker = (CommonAST)walker.getNextSibling()).Type == BoolParserTokenTypes.BINOP))
            {
                Bdd firstOperand = ExprBdd;

                string Operation = walker.getText();
                walker = (CommonAST)walker.getNextSibling();

                Bdd secondOperand = ExprToBdd(walker, Manager, LocalVarNameToId, GlobalVarNameToId);

                switch (Operation)
                {
                case ("|"):
                    ExprBdd = Manager.LogicalOr(firstOperand, secondOperand);
                    break;

                case ("&"):
                    ExprBdd = Manager.LogicalAnd(firstOperand, secondOperand);
                    break;

                case ("^"):
                    ExprBdd = Manager.LogicalXor(firstOperand, secondOperand);
                    break;

                case ("="):
                    ExprBdd = Manager.LogicalXnor(firstOperand, secondOperand);
                    break;

                case ("!="):
                    ExprBdd = Manager.LogicalXor(firstOperand, secondOperand);
                    break;

                case ("=>"):
                    ExprBdd = Manager.LogicalImplies(firstOperand, secondOperand);
                    break;

                default:
                    Debug.Assert(false);
                    break;
                }

                firstOperand.FreeBdd();
                secondOperand.FreeBdd();
            }

            return(ExprBdd);
        }
예제 #11
0
        private void BuildSuccesors()
        {
            foreach (CFGNode node in ListOfCfgNodes)
            {
                CommonAST nodeAST = node.GetAST();
                CFGNode   succNode;

                #region Succesors for Statements
                if (node is CFGNodeStatement)
                {
                    CFGNodeStatement stmtNode = (CFGNodeStatement)node;
                    switch (nodeAST.Type)
                    {
                    case BoolParserTokenTypes.LITERAL_goto:
                        nodeAST = (CommonAST)nodeAST.getFirstChild();
                        if (LabelToStatement.TryGetValue(nodeAST.getText(), out succNode))
                        {
                            stmtNode.AddSuccesor(succNode);
                        }
                        break;

                    case BoolParserTokenTypes.ASSIGNMENT:
                    case BoolParserTokenTypes.LITERAL_skip:
                    case BoolParserTokenTypes.LITERAL_print:
                        stmtNode.AddSuccesor(stmtNode.Next);
                        break;

                    case BoolParserTokenTypes.LITERAL_return:
                        stmtNode.AddSuccesor(stmtNode.ProcOf);
                        break;

                    case BoolParserTokenTypes.LITERAL_if:
                        nodeAST = (CommonAST)nodeAST.getFirstChild();
                        nodeAST = (CommonAST)nodeAST.getNextSibling();
                        if (AstToCfgMapping.TryGetValue(nodeAST.getFirstChild() as CommonAST, out succNode))
                        {
                            (stmtNode as CFGNodeStmtIf).TrueSuccesor = succNode;
                        }
                        nodeAST = (CommonAST)nodeAST.getNextSibling();
                        if (AstToCfgMapping.TryGetValue(nodeAST.getFirstChild() as CommonAST, out succNode))
                        {
                            (stmtNode as CFGNodeStmtIf).FalseSuccesor = succNode;
                        }
                        break;

                    case BoolParserTokenTypes.LITERAL_while:
                        nodeAST = (CommonAST)nodeAST.getFirstChild();
                        nodeAST = (CommonAST)nodeAST.getNextSibling();
                        nodeAST = (CommonAST)nodeAST.getFirstChild();
                        if (AstToCfgMapping.TryGetValue(nodeAST, out succNode))
                        {
                            (stmtNode as CFGNodeStmtWhile).TrueSuccesor = succNode;
                        }
                        (stmtNode as CFGNodeStmtWhile).FalseSuccesor = stmtNode.Next;
                        break;

                    case BoolParserTokenTypes.LITERAL_assert:
                        (stmtNode as CFGNodeStmtAssert).FalseSuccesor = ErrorNode;
                        (stmtNode as CFGNodeStmtAssert).TrueSuccesor  = stmtNode.Next;
                        break;

                    case BoolParserTokenTypes.PROCCALL:
                        nodeAST = (CommonAST)nodeAST.getFirstChild();
                        CFGNodeProcedure procNode;
                        if (ProcNameToNode.TryGetValue(nodeAST.getText(), out procNode))
                        {
                            stmtNode.AddSuccesor(procNode.FirstStmtOf);
                        }

                        //Exit points of procedures need to be filled when processing all procedure calls
                        procNode.AddSuccesor(stmtNode.Next);
                        break;

                    default:
                        System.Diagnostics.Debug.Assert(false);
                        break;
                    }
                }
                #endregion
                //Succesors for Procedure Exits are built when processing Procedure calls
            }
        }
예제 #12
0
        private void ExtractCfgNodes(CommonAST SubAst, CommonAST ParentAST, CFGNodeProcedure ProcOf)
        {
            while (SubAst != null)
            {
                ASTParentByASTNode.Add(SubAst, ParentAST);

                #region AST Nodes of type Statement
                if (SubAst.Type == BoolParserTokenTypes.STMT ||
                    SubAst.Type == BoolParserTokenTypes.LSTMT)
                {
                    if (!AstToCfgMapping.ContainsKey(SubAst))
                    {
                        CFGNodeStatement NewStatementNode;
                        if (SubAst.Type == BoolParserTokenTypes.STMT)
                        {
                            NewStatementNode = CFGStatementNodeFactory.Make((CommonAST)SubAst.getFirstChild());
                        }
                        else
                        {
                            NewStatementNode = CFGStatementNodeFactory.Make(
                                (CommonAST)SubAst.getFirstChild().getNextSibling());
                            NewStatementNode.setLabel(SubAst.getFirstChild().getText());
                            LabelToStatement.Add(SubAst.getFirstChild().getText(), NewStatementNode);
                        }
                        NewStatementNode.ProcOf = ProcOf;
                        ListOfCfgNodes.Add(NewStatementNode);
                        AstToCfgMapping.Add(SubAst, NewStatementNode);
                    }
                }

                #endregion
                #region AST Nodes of type Procedure
                if (SubAst.Type == BoolParserTokenTypes.PROC)
                {
                    if (!AstToCfgMapping.ContainsKey(SubAst))
                    {
                        CFGNodeProcedure NewStatementNode =
                            new CFGNodeProcedure((CommonAST)SubAst.getFirstChild());
                        ListOfCfgNodes.Add(NewStatementNode);
                        AstToCfgMapping.Add(SubAst, NewStatementNode);

                        ProcNameToNode.Add(SubAst.getFirstChild().getText(), NewStatementNode);

                        ProcOf = NewStatementNode;

                        //Inserting the function parameters into the procedure local variables list
                        CommonAST walker = (CommonAST)SubAst.getFirstChild().getFirstChild();
                        while (walker.Type == BoolParserTokenTypes.ID)
                        {
                            int varID = ProcOf.LocalVariables.AddVar(walker.getText(), m_BddManager, m_BddVariableToName);
                            ProcOf.FormalParameters.AddVar(walker.getText(), varID);
                            walker = (CommonAST)walker.getNextSibling();
                        }
                    }
                }

                #endregion
                #region AST Nodes of type Variable Declaration
                if (SubAst.Type == BoolParserTokenTypes.LITERAL_decl)
                {
                    CommonAST walker = (CommonAST)SubAst.getFirstChild();

                    while (walker != null)
                    {
                        if (ProcOf != null)
                        {
                            ProcOf.LocalVariables.AddVar(walker.getText(), m_BddManager, m_BddVariableToName);
                        }
                        else
                        {
                            m_GlobalVariables.AddVar(walker.getText(), m_BddManager, m_BddVariableToName);
                        }

                        walker = (CommonAST)walker.getNextSibling();
                    }
                }
                #endregion

                ExtractCfgNodes((CommonAST)SubAst.getFirstChild(), SubAst, ProcOf);

                SubAst = (CommonAST)SubAst.getNextSibling();
            }
        }
예제 #13
0
        public static Bdd buildProcCallTransfer(CommonAST procCall, CFGNodeProcedure procedureCalled,
                                                BddManager Manager, Dictionary <string, int> LocalVarToId, Dictionary <string, int> GlobalVarToId)
        {
            Bdd transfer = Manager.CreateBddOne();

            CommonAST walkerExpr   = procCall.getFirstChild().getFirstChild() as CommonAST;
            CommonAST walkerFormal = procedureCalled.GetAST().getFirstChild() as CommonAST;

            #region Build Bdd for FormalParameters = Call Expressions
            while (walkerExpr != null)
            {
                Bdd expr = HelperFunctions.ExprToBdd(walkerExpr, Manager, LocalVarToId, GlobalVarToId);
                int varID;
                if (!procedureCalled.FormalParameters.VariablesToId.TryGetValue(walkerFormal.getText(), out varID))
                {
                    System.Diagnostics.Debug.Assert(false);
                }

                Bdd formal = Manager.GetBddVariableWithID(varID + 2);

                Bdd tempBdd = Manager.LogicalXnor(formal, expr);

                Bdd tempBdd2 = transfer;
                transfer = Manager.LogicalAnd(transfer, tempBdd);

                expr.FreeBdd();
                formal.FreeBdd();
                tempBdd.FreeBdd();
                tempBdd2.FreeBdd();

                walkerExpr   = walkerExpr.getNextSibling() as CommonAST;
                walkerFormal = walkerFormal.getNextSibling() as CommonAST;
            }
            #endregion

            #region And with an identity over Global variables and true Local variables(non Formals)

            /*    int variableCount = procedureCalled.LocalVariables.VariablesToId.Values.Count -
             *                      procedureCalled.FormalParameters.VariablesToId.Values.Count;
             *  int[] variableIDs = new int[variableCount];
             *  int index = 0;
             *  Bdd identity;
             *  foreach (int varID in procedureCalled.LocalVariables.VariablesToId.Values)
             *  {
             *      if (!(procedureCalled.FormalParameters.VariablesToId.ContainsValue(varID)))
             *      {
             *          variableIDs[index++] = varID;
             *      }
             *  }
             *
             *  Bdd identNewLocals = HelperFunctions.BuildIdentityOverVariableIDs(Manager, variableIDs, variableCount);*/
            Bdd identGlobals = HelperFunctions.BuildIdentityTransfer(Manager, null, GlobalVarToId);

            /*    if (identNewLocals != null && identGlobals != null)
             *  {
             *      identity = Manager.LogicalAnd(identGlobals, identNewLocals);
             *      identNewLocals.FreeBdd();
             *      identGlobals.FreeBdd();
             *  }
             *  else if (identNewLocals != null)
             *  {
             *      identity = identNewLocals;
             *  }
             *  else
             *  {
             *      identity = identGlobals;
             *  }*/

            Bdd identity = identGlobals;

            Bdd temp = transfer;

            transfer = Manager.LogicalAnd(transfer, identity);

            //temp.FreeBdd();

            #endregion

            return(transfer);
        }
예제 #14
0
 virtual public string ToString()
 {
     return(NodeASTSubTree.getText());
 }
예제 #15
0
        public static Bdd BuildAssignmentTransfer(CommonAST assignAST, BddManager Manager,
                                                  Dictionary <string, int> LocalVarToId, Dictionary <string, int> GlobalVarToId)
        {
            CommonAST walkerVariable  = assignAST.getFirstChild() as CommonAST;
            CommonAST walkerExpresion = assignAST.getFirstChild() as CommonAST;

            while (walkerExpresion.Type == BoolParserTokenTypes.ID)
            {
                walkerExpresion = walkerExpresion.getNextSibling() as CommonAST;
            }
            walkerExpresion = walkerExpresion.getNextSibling() as CommonAST;

            List <int> assignedVariables = new List <int>();

            Bdd  transfer          = null;
            bool bddTransferIsnull = true;

            while (walkerVariable.Type == BoolParserTokenTypes.ID)
            {
                Bdd tempBdd, varBdd, exprBdd;
                int VariableID;

                if (!LocalVarToId.TryGetValue(walkerVariable.getText(), out VariableID))
                {
                    if (!GlobalVarToId.TryGetValue(walkerVariable.getText(), out VariableID))
                    {
                        Debug.Assert(false);//Varijabla mora biti ili medju lokalnim ili medju globalnim
                    }
                }
                assignedVariables.Add(VariableID);

                varBdd  = Manager.GetBddVariableWithID(VariableID + 2);
                exprBdd = HelperFunctions.ExprToBdd(walkerExpresion, Manager, LocalVarToId, GlobalVarToId);

                tempBdd = Manager.LogicalXnor(varBdd, exprBdd);

                varBdd.FreeBdd();
                exprBdd.FreeBdd();

                if (bddTransferIsnull)
                {
                    transfer          = tempBdd;
                    bddTransferIsnull = false;
                }
                else
                {
                    varBdd = transfer;

                    transfer = Manager.LogicalAnd(transfer, tempBdd);

                    varBdd.FreeBdd();
                    tempBdd.FreeBdd();
                }

                walkerVariable  = walkerVariable.getNextSibling() as CommonAST;
                walkerExpresion = walkerExpresion.getNextSibling() as CommonAST;
            }

            #region add identitiy transfer for the rest of the variables
            //Now we add the identity over variables that were not assigned to
            int[] localVariablesIDs = new int[LocalVarToId.Values.Count];
            LocalVarToId.Values.CopyTo(localVariablesIDs, 0);
            int[] globalVariablesIDs = new int[GlobalVarToId.Values.Count];
            GlobalVarToId.Values.CopyTo(globalVariablesIDs, 0);

            int[] unassignedVariables =
                new int[LocalVarToId.Values.Count + GlobalVarToId.Values.Count - assignedVariables.Count];
            int unAssIndex = 0;

            for (int index = 0; index < LocalVarToId.Values.Count; index++)
            {
                if (!(assignedVariables.Contains(localVariablesIDs[index])))
                {
                    unassignedVariables[unAssIndex++] = localVariablesIDs[index];
                }
            }
            for (int index = 0; index < GlobalVarToId.Values.Count; index++)
            {
                if (!(assignedVariables.Contains(globalVariablesIDs[index])))
                {
                    unassignedVariables[unAssIndex++] = globalVariablesIDs[index];
                }
            }

            Bdd identitiy = HelperFunctions.BuildIdentityOverVariableIDs(Manager, unassignedVariables);

            Bdd tempBdd2 = transfer;
            transfer = Manager.LogicalAnd(transfer, identitiy);

            identitiy.FreeBdd();
            tempBdd2.FreeBdd();

            #endregion

            return(transfer);
        }