Exemplo n.º 1
0
        public static TDNODE Function_Part()
        {
            TDNODE node = new TDNODE();

            if (i < LT.Count)
            {
                node.token = new Token("function_part", Token_Class.other);

                //if (LT[i].lex == '('.ToString())
                // {
                node.children.Add(match(Token_Class.LeftBracket));
                if (i < LT.Count && LT[i].token_type != Token_Class.RightBracket)
                {
                    node.children.Add(Expression());
                }
                //  }
                while (LT[i].token_type != Token_Class.RightBracket)
                {
                    node.children.Add(match(Token_Class.comma));
                    node.children.Add(Expression());
                }
                //if (LT[i].lex == ')'.ToString())
                node.children.Add(match(Token_Class.RightBracket));
            }
            return(node);
        }
Exemplo n.º 2
0
        public static TDNODE Declaration_Statement()
        {
            TDNODE node = new TDNODE();

            if (i < LT.Count)
            {
                node.token = new Token("Declaration_Statement", Token_Class.other);

                node.children.Add(Datatype());
                node.children.Add(Identifiers());
                if (i < LT.Count && (LT[i].token_type != Token_Class.comma && LT[i].token_type != Token_Class.semicolon))
                {
                    node.children.Add(match(Token_Class.AssigmentOp));
                    node.children.Add(Expression());
                }
                while (i < LT.Count && LT[i].token_type != Token_Class.semicolon)
                {
                    node.children.Add(match(Token_Class.comma));
                    node.children.Add(Identifiers());
                    if (i < LT.Count && (LT[i].token_type != Token_Class.comma && LT[i].token_type != Token_Class.semicolon))
                    {
                        node.children.Add(match(Token_Class.AssigmentOp));
                        node.children.Add(Expression());
                    }
                }

                //if (LT[i].lex == ';'.ToString())
                node.children.Add(match(Token_Class.semicolon));
            }
            return(node);
        }
Exemplo n.º 3
0
        public static TDNODE Factor()
        {
            TDNODE node = new TDNODE();

            if (i < LT.Count)
            {
                node.token = new Token("factor", Token_Class.other);
                if (i < LT.Count)
                {
                    if (LT[i].token_type == Token_Class.Identifier && i + 1 < LT.Count && LT[i + 1].token_type == Token_Class.LeftBracket)
                    {
                        node.children.Add(Function_Call());
                    }
                    else if (LT[i].token_type == Token_Class.Identifier)
                    {
                        node.children.Add(Identifiers());
                    }
                    else if (LT[i].token_type == Token_Class.String)
                    {
                        node.children.Add(String());
                    }
                    else if (LT[i].token_type == Token_Class.constant)
                    {
                        node.children.Add(Number());
                    }
                    else if (LT[i].lex == '('.ToString())
                    {
                        node.children.Add(match(Token_Class.LeftBracket));
                        node.children.Add(Expression());
                        node.children.Add(match(Token_Class.RightBracket));
                    }
                }
            }
            return(node);
        }
Exemplo n.º 4
0
        public static TDNODE Statements()
        {
            TDNODE nd = new TDNODE();

            if (i < LT.Count)
            {
                nd.token = new Token("Statements", Token_Class.other);
                bool flag = false;
                if (LT[i].token_type == Token_Class.Return)
                {
                    int j = i + 1;
                    while (!flag && j < LT.Count && LT[j].token_type != Token_Class.RightCurlyBracket && LT[j].token_type != Token_Class.LeftCurlyBracket)
                    {
                        if (LT[j].token_type == Token_Class.Return)
                        {
                            flag = true;
                        }
                        j++;
                    }
                }
                if (i < LT.Count && (LT[i].token_type == Token_Class.Identifier || LT[i].token_type == Token_Class.dataType || LT[i].token_type == Token_Class.reservedKeyword && LT[i].lex == "write" || LT[i].token_type == Token_Class.reservedKeyword && LT[i].lex == "read" || (flag && LT[i].token_type == Token_Class.Return) || LT[i].token_type == Token_Class.IF || LT[i].token_type == Token_Class.repeat || LT[i].token_type == Token_Class.comment))
                {
                    nd.children.Add(Statement());
                    nd.children.Add(Statements());
                }
                else
                {
                    TDNODE child = new TDNODE();
                    child.token = new Token(" ", Token_Class.Epsilon);
                    nd.children.Add(child);
                }
            }
            return(nd);
        }
Exemplo n.º 5
0
        //19
        public static TDNODE Condition_Operator()
        {
            TDNODE condition_op = new TDNODE();

            if (i < LT.Count)
            {
                condition_op.token = new Token("Condition_Operator", Token_Class.other);
                if (LT[i].token_type == Token_Class.LessThanOp)
                {
                    condition_op.children.Add(match(Token_Class.LessThanOp));
                }
                else if (LT[i].token_type == Token_Class.GreaterThanOp)
                {
                    condition_op.children.Add(match(Token_Class.GreaterThanOp));
                }
                else if (LT[i].token_type == Token_Class.IsEqualOp)
                {
                    condition_op.children.Add(match(Token_Class.IsEqualOp));
                }
                else if (LT[i].token_type == Token_Class.NotEqualOp)
                {
                    condition_op.children.Add(match(Token_Class.NotEqualOp));
                }
            }
            return(condition_op);
        }
Exemplo n.º 6
0
        public static TDNODE MulOp()
        {
            TDNODE node = new TDNODE();

            if (i < LT.Count)
            {
                node.token = new Token("MulOp", Token_Class.MulOp);
                node.children.Add(match(Token_Class.MulOp));
            }
            return(node);
        }
Exemplo n.º 7
0
        //use this function to print the parse tree in TreeView Toolbox
        public static TreeNode PrintParseTree(TDNODE root)
        {
            TreeNode tree     = new TreeNode("Parse Tree");
            TreeNode treeRoot = PrintTree(root);

            if (treeRoot != null)
            {
                tree.Nodes.Add(treeRoot);
            }
            return(tree);
        }
Exemplo n.º 8
0
        //23
        public static TDNODE AndOp()
        {
            TDNODE and_op = new TDNODE();

            if (i < LT.Count)
            {
                and_op.token = new Token("AndOp", Token_Class.And_Operator);
                and_op.children.Add(match(Token_Class.And_Operator));
            }
            return(and_op);
        }
Exemplo n.º 9
0
        //29
        public static TDNODE FunctionName()
        {
            TDNODE nd = new TDNODE();

            if (i < LT.Count)
            {
                nd.token = new Token("FunctionName", Token_Class.other);
                nd.children.Add(Identifiers());
            }
            return(nd);
        }
Exemplo n.º 10
0
        //24
        public static TDNODE OrOp()
        {
            TDNODE or_op = new TDNODE();

            if (i < LT.Count)
            {
                or_op.token = new Token("OrOp", Token_Class.Or_Operator);
                or_op.children.Add(match(Token_Class.Or_Operator));
            }
            return(or_op);
        }
Exemplo n.º 11
0
        public static TDNODE Number()
        {
            TDNODE node = new TDNODE();

            if (i < LT.Count)
            {
                node.token = new Token("Number", Token_Class.other);
                node.children.Add(match(Token_Class.constant));
            }
            return(node);
        }
Exemplo n.º 12
0
        public static TDNODE String()
        {
            TDNODE node = new TDNODE();

            if (i < LT.Count)
            {
                node.token = new Token("string", Token_Class.String);

                node.children.Add(match(Token_Class.String));
            }
            return(node);
        }
Exemplo n.º 13
0
        //21
        public static TDNODE Condition_Term()
        {
            TDNODE cond_term = new TDNODE();

            if (i < LT.Count)
            {
                cond_term.token = new Token("Condition_Term", Token_Class.other);
                cond_term.children.Add(Condition());
                cond_term.children.Add(Condition_Term_2());
            }
            return(cond_term);
        }
Exemplo n.º 14
0
        public static TDNODE Comment_State()
        {
            TDNODE node = new TDNODE();

            if (i < LT.Count)
            {
                node.token = new Token("comment_state", Token_Class.comment);

                node.children.Add(match(Token_Class.comment));
            }
            return(node);
        }
Exemplo n.º 15
0
        public static TDNODE Identifiers()
        {
            TDNODE node = new TDNODE();

            if (i < LT.Count)
            {
                node.token = new Token("identifier", Token_Class.Identifier);

                node.children.Add(match(Token_Class.Identifier));
            }
            return(node);
        }
Exemplo n.º 16
0
        public static TDNODE match(Token_Class t)
        {
            TDNODE match = new TDNODE();

            if (i < LT.Count() && LT[i].token_type == t)
            {
                match.token = LT[i];
                bing        = false;
                i++;
            }
            else
            {
                if (i >= LT.Count)
                {
                    string s = "Some essential lines doesn't exist.";
                    ErrorList.Add(s);
                }
                else if (!bing)
                {
                    int j = Scanner.NewLine.Count() - 1;
                    while (!(i <= Scanner.NewLine[j] && i > Scanner.NewLine[j - 1]))
                    {
                        j--;
                    }
                    if (i + 1 < LT.Count && LT[i + 1].token_type == t)
                    {
                        string s = "Extra ";
                        s += LT[i].lex;
                        ErrorList.Add(s);
                        match.token = LT[i + 1];
                        i          += 2;
                    }
                    else if (i == Scanner.NewLine[j - 1] + 1)
                    {
                        string s = "Expected ";
                        s += t.ToString();
                        s += " here.";
                        ErrorList.Add(s);
                        bing = true;
                    }
                    else //if (i + 1 < LT.Count && LT[i + 1].token_type != t)
                    {
                        string s = "Expected ";
                        s += t.ToString();
                        s += " instead of ";
                        s += LT[i].lex;
                        ErrorList.Add(s);
                        i++;
                    }
                }
            }
            return(match);
        }
Exemplo n.º 17
0
        public static TDNODE Term()
        {
            TDNODE node = new TDNODE();

            if (i < LT.Count)
            {
                node.token = new Token("Term", Token_Class.other);
                node.children.Add(Factor());
                node.children.Add(TermOP());
            }
            return(node);
        }
Exemplo n.º 18
0
        public static TDNODE Datatype()
        {
            TDNODE node = new TDNODE();

            if (i < LT.Count)
            {
                node.token = new Token("Datatype", Token_Class.dataType);

                node.children.Add(match(Token_Class.dataType));
            }
            return(node);
        }
Exemplo n.º 19
0
        //20
        public static TDNODE Condition_Statement()
        {
            TDNODE condition_statement = new TDNODE();

            if (i < LT.Count)
            {
                condition_statement.token = new Token("Condition_Statement", Token_Class.other);
                condition_statement.children.Add(Condition_Term());
                condition_statement.children.Add(Condition_Statement_2());
            }
            return(condition_statement);
        }
Exemplo n.º 20
0
        //27
        public static TDNODE Else_Statement()
        {
            TDNODE nd = new TDNODE();

            if (i < LT.Count)
            {
                nd.token = new Token("Else_Statement", Token_Class.Else);
                nd.children.Add(match(Token_Class.Else));
                nd.children.Add(Statements());
            }
            return(nd);
        }
Exemplo n.º 21
0
        public static TreeNode PrintSemanticTree(TDNODE root)
        {
            TreeNode tree     = new TreeNode("Syntax Tree");
            TreeNode treeRoot = PrintsyntaxTree(root);

            tree.Expand();
            if (treeRoot != null)
            {
                tree.Nodes.Add(treeRoot);
            }
            return(tree);
        }
Exemplo n.º 22
0
        //33
        public static TDNODE Function_Statement()
        {
            TDNODE nd = new TDNODE();

            if (i < LT.Count)
            {
                nd.token = new Token("Function_Statement", Token_Class.other);
                nd.children.Add(Function_Declaration());
                nd.children.Add(Function_Body());
            }
            return(nd);
        }
Exemplo n.º 23
0
        //30
        public static TDNODE Parameter()
        {
            TDNODE nd = new TDNODE();

            if (i < LT.Count)
            {
                nd.token = new Token("Parameter", Token_Class.other);
                nd.children.Add(Datatype());
                nd.children.Add(Identifiers());
            }
            return(nd);
        }
Exemplo n.º 24
0
        public static TDNODE Function_Call()
        {
            TDNODE node = new TDNODE();

            if (i < LT.Count)
            {
                node.token = new Token("function call", Token_Class.other);

                node.children.Add(Identifiers());
                node.children.Add(Function_Part());
            }
            return(node);
        }
Exemplo n.º 25
0
        public static TDNODE Expression()
        {
            TDNODE node = new TDNODE();

            if (i < LT.Count)
            {
                node.token = new Token("Expression", Token_Class.other);

                node.children.Add(Term());
                node.children.Add(Exp());
            }
            return(node);
        }
Exemplo n.º 26
0
        //18
        public static TDNODE Return_Statement()
        {
            TDNODE return_statement = new TDNODE();

            if (i < LT.Count)
            {
                return_statement.token = new Token("Return_Statement", Token_Class.Return);
                return_statement.children.Add(match(Token_Class.Return));
                return_statement.children.Add(Expression());
                return_statement.children.Add(match(Token_Class.semicolon));
            }
            return(return_statement);
        }
Exemplo n.º 27
0
        //22
        public static TDNODE Condition()
        {
            TDNODE cond = new TDNODE();

            if (i < LT.Count)
            {
                cond.token = new Token("Condition", Token_Class.other);
                cond.children.Add(Expression());
                cond.children.Add(Condition_Operator());
                cond.children.Add(Expression());
            }
            return(cond);
        }
Exemplo n.º 28
0
        //32
        public static TDNODE Function_Body()
        {
            TDNODE nd = new TDNODE();

            if (i < LT.Count)
            {
                nd.token = new Token("Function_Body", Token_Class.other);
                nd.children.Add(match(Token_Class.LeftCurlyBracket));
                nd.children.Add(Statements());
                nd.children.Add(Return_Statement());
                nd.children.Add(match(Token_Class.RightCurlyBracket));
            }
            return(nd);
        }
Exemplo n.º 29
0
        //28
        public static TDNODE Repeat_Statement()
        {
            TDNODE nd = new TDNODE();

            if (i < LT.Count)
            {
                nd.token = new Token("Repeat_Statement", Token_Class.other);
                nd.children.Add(match(Token_Class.repeat));
                nd.children.Add(Statements());
                nd.children.Add(match(Token_Class.until));
                nd.children.Add(Condition_Statement());
            }
            return(nd);
        }
Exemplo n.º 30
0
        public static TDNODE Statement()
        {
            TDNODE nd = new TDNODE();

            if (i < LT.Count)
            {
                nd.token = new Token("Statement", Token_Class.other);
                if (i < LT.Count)
                {
                    if (LT[i].token_type == Token_Class.Identifier && i + 1 < LT.Count && LT[i + 1].token_type == Token_Class.LeftBracket)
                    {
                        nd.children.Add(Function_Call());
                        nd.children.Add(match(Token_Class.semicolon));
                    }
                    else if (LT[i].token_type == Token_Class.Identifier)
                    {
                        nd.children.Add(Assignment_Statement());
                    }
                    else if (LT[i].token_type == Token_Class.dataType)
                    {
                        nd.children.Add(Declaration_Statement());
                    }
                    else if (LT[i].token_type == Token_Class.reservedKeyword && LT[i].lex == "write")
                    {
                        nd.children.Add(Write_Statement());
                    }
                    else if (LT[i].token_type == Token_Class.reservedKeyword && LT[i].lex == "read")
                    {
                        nd.children.Add(Read_Statement());
                    }
                    else if (LT[i].token_type == Token_Class.Return)
                    {
                        nd.children.Add(Return_Statement());
                    }
                    else if (LT[i].token_type == Token_Class.IF)
                    {
                        nd.children.Add(If_Statement());
                    }
                    else if (LT[i].token_type == Token_Class.repeat)
                    {
                        nd.children.Add(Repeat_Statement());
                    }
                    else if (LT[i].token_type == Token_Class.comment)
                    {
                        nd.children.Add(Comment_State());
                    }
                }
            }
            return(nd);
        }