示例#1
0
        /*
         * LL(1) Grammar:
         *      PROGRAM ::= ("const" CONST_DECL+)? ("var" VAR_DECL+)? PROC_DECL* "program" STATEMENT* "end" ";"
         *      CONST_DECL ::= IDENTIFIER ":=" LITERAL ";"
         *      VAR_DECL ::= IDENTIFIER ("," IDENTIFIER)* ":" TYPE ";"
         *      LITERAL ::= SIMPLE_LITERAL | LIST
         *      SIMPLE_LITERAL ::= INTEGER_LITERAL | STRING_LITERAL | TRUE | FALSE
         *      TYPE ::= SIMPLE_TYPE | LIST_TYPE
         *      SIMPLE_TYPE ::= "integer" | "string" | "boolean"
         *      LIST_TYPE ::= "list" "of" SIMPLE_TYPE
         *      LIST ::= "{" (SIMPLE_LITERAL ("," SIMPLE_LITERAL )*)? "}"
         *      PROC_DECL ::= "procedure" IDENTIFIER "(" PARAM_DECL* ")" (":" TYPE)? ";" ("const" CONST_DECL+)? ("var" VAR_DECL+)? "begin" STATEMENT* "end" ";"
         *      PARAM_DECL ::= IDENTIFIER ("," IDENTIFIER)* ":" TYPE ";"
         *      STATEMENT ::= (IDENTIFIER (ASS_STAT | CALL_STAT) ) | IF_STAT | LOOP_STAT | FOR_STAT | RET_STAT | EXIT_STAT
         *      ASS_STAT ::= ("[" EXPR "]")? ":=" EXPR ";"
         *      CALL_STAT ::= "(" (EXPR ("," EXPR)*)? ")" ";"
         *      IF_STAT ::= "if" EXPR "then" STATEMENT* ("elseif" EXPR "THEN" STATEMENT*)* ("else" STATEMENT*)? "end" ";"
         *      LOOP_STAT ::= "loop" STATEMENT* "end" ";"
         *      FOR_STAT ::= "for" IDENTIFIER "in" EXPR "do" STATEMENT* "end" ";"
         *      RET_STAT ::= "return" EXPR? ";"
         *      EXIT_STAT ::= "exit" ";"
         *      EXPR ::= LOGIC_EXPR
         *      LOGIC_EXPR ::= REL_EXPR (LOGIC_OP REL_EXPR)*
         *      LOGIC_OP ::= "and" | "or" | "xor"
         *      REL_EXPR ::= SUM_EXPR (REL_OP SUM_EXPR)*
         *      REL_OP ::= "=" | "<>" | "<" | ">" | "<=" | ">="
         *      SUM_EXPR ::= MUL_EXPR (SUM_OP MUL_EXPR)*
         *      SUM_OP ::= "+" | "-"
         *      MUL_EXPR ::= UN_EXPR (MUL_OP UN_EXPR)*
         *      MUL_OP ::=  "*" | "div" | "rem"
         *      UN_EXPR ::= ("not" UN_EXPR) | ("-" UN_EXPR) | SIMP_EXPR
         *      SIMP_EXPR ::= ("(" Expression ")" | (IDENTIFIER CALL?) | LITERAL ) ("[" EXPR "]")?
         *      CALL ::= "(" (EXPR ("," EXPR)*)? ")"
         */

        public Node Program()
        {
            var result = new Program();

            var constantDeclarationList = new ConstantDeclarationList();

            if (CurrentToken == TokenCategory.CONST)
            {
                Expect(TokenCategory.CONST);
                do
                {
                    constantDeclarationList.Add(ConstantDeclaration());
                } while (CurrentToken == TokenCategory.IDENTIFIER);
            }
            result.Add(constantDeclarationList);

            var variableDeclarationList = new VariableDeclarationList();

            if (CurrentToken == TokenCategory.VAR)
            {
                Expect(TokenCategory.VAR);
                do
                {
                    variableDeclarationList.Add(VariableDeclaration());
                } while (CurrentToken == TokenCategory.IDENTIFIER);
            }
            result.Add(variableDeclarationList);

            var procedureDeclarationList = new ProcedureDeclarationList();

            if (CurrentToken == TokenCategory.PROCEDURE)
            {
                do
                {
                    procedureDeclarationList.Add(ProcedureDeclaration());
                } while (CurrentToken == TokenCategory.PROCEDURE);
            }
            result.Add(procedureDeclarationList);

            Expect(TokenCategory.PROGRAM);

            var statementList = new StatementList();

            while (firstOfStatement.Contains(CurrentToken))
            {
                statementList.Add(Statement());
            }
            result.Add(statementList);

            Expect(TokenCategory.END);
            Expect(TokenCategory.SEMICOLON);
            Expect(TokenCategory.EOF);

            return(result);
        }
示例#2
0
        //dache
        public Node Program()
        {
            var program     = new Program();
            var statList    = new StatementList();
            var procDecList = new ProcedureDeclarationList();
            var consDecList = new ConstantDeclarationList();

            //var consDecList2 = VariableDeclaration();
            //var varDecList = new VariableDeclarationList();

            if (CurrentToken == TokenCategory.CONST)
            {
                consDecList.AnchorToken = Expect(TokenCategory.CONST);
                var i = 0;
                do
                {
                    consDecList.Add(ConstantDeclaration());
                    Console.WriteLine(consDecList[i]);
                    i++;
                } while (CurrentToken == TokenCategory.IDENTIFIER);
                program.Add(consDecList);
            }

            if (CurrentToken == TokenCategory.VAR)
            {
                var varDecList = VariableDeclaration();
                program.Add(varDecList);
            }

            while (CurrentToken == TokenCategory.PROCEDURE)
            {
                procDecList.Add(ProcedureDeclaration());
            }
            if (procDecList.getLength() > 0)
            {
                program.Add(procDecList);
            }


            Expect(TokenCategory.PROGRAM);
            while (firstOfStatement.Contains(CurrentToken))
            {
                statList.Add(Statement());
            }

            if (statList.getLength() > 0)
            {
                program.Add(statList);
            }
            Expect(TokenCategory.END);
            Expect(TokenCategory.ENDLINE);
            Expect(TokenCategory.EOF);

            return(program);
        }
示例#3
0
        public Node Program()
        {
            var constantList  = new ConstantDeclarationList();
            var varList       = new VariableDeclarationList();
            var procedureList = new ProcedureDeclarationList();
            var stmtlist      = new StatementList();

            //IF-> do while because it's one or more times
            if (firstOfDeclaration.Contains(CurrentToken) && CurrentToken == TokenCategory.CONST)
            {
                constantList.AnchorToken = Expect(TokenCategory.CONST);

                do
                {
                    constantList.Add(ConstantDeclaration());
                } while (CurrentToken == TokenCategory.IDENTIFIER);
            }

            if (firstOfDeclaration.Contains(CurrentToken) && CurrentToken == TokenCategory.VAR)
            {
                varList.AnchorToken = Expect(TokenCategory.VAR);

                do
                {
                    varList.Add(VariableDeclaration());
                } while (CurrentToken == TokenCategory.IDENTIFIER);
            }

            //just while because it's zero or more times
            while (firstOfDeclaration.Contains(CurrentToken) && CurrentToken == TokenCategory.PROCEDURE)
            {
                procedureList.Add(ProcedureDeclaration());
            }

            Expect(TokenCategory.PROGRAM);

            while (firstOfStatement.Contains(CurrentToken))
            {
                stmtlist.Add(Statement());
            }

            Expect(TokenCategory.END);
            Expect(TokenCategory.SEMICOLON);

            return(new Program()
            {
                constantList,
                varList,
                procedureList,
                stmtlist
            });
        }
示例#4
0
        public Node ProcedureDeclaration()
        {
            var result = new ProcedureDeclaration()
            {
                AnchorToken = Expect(TokenCategory.PROCEDURE)
            };

            result.Add(new Identifier()
            {
                AnchorToken = Expect(TokenCategory.IDENTIFIER)
            });

            Expect(TokenCategory.PARENTHESIS_OPEN);

            var parameterList = new ParameterDeclarationList();

            if (CurrentToken == TokenCategory.IDENTIFIER)
            {
                while (CurrentToken == TokenCategory.IDENTIFIER)
                {
                    parameterList.Add(VariableDeclaration());
                }
            }
            result.Add(parameterList);

            Expect(TokenCategory.PARENTHESIS_CLOSE);

            var type = new TypeNode();

            if (CurrentToken == TokenCategory.COLON)
            {
                Expect(TokenCategory.COLON);

                if (CurrentToken != TokenCategory.LIST)
                {
                    type.Add(SimpleType());
                }

                else if (CurrentToken == TokenCategory.LIST)
                {
                    type.Add(ListType());
                }
            }
            result.Add(type);

            Expect(TokenCategory.SEMICOLON);

            var constantList = new ConstantDeclarationList();

            if (firstOfDeclaration.Contains(CurrentToken) && CurrentToken == TokenCategory.CONST)
            {
                constantList.AnchorToken = Expect(TokenCategory.CONST);

                do
                {
                    constantList.Add(ConstantDeclaration());
                } while (CurrentToken == TokenCategory.IDENTIFIER);
            }
            result.Add(constantList);

            var variableList = new VariableDeclarationList();

            if (firstOfDeclaration.Contains(CurrentToken) && CurrentToken == TokenCategory.VAR)
            {
                variableList.AnchorToken = Expect(TokenCategory.VAR);

                do
                {
                    variableList.Add(VariableDeclaration());
                } while (CurrentToken == TokenCategory.IDENTIFIER);
            }
            result.Add(variableList);

            Expect(TokenCategory.BEGIN);

            var statementList = new StatementList();

            if (firstOfStatement.Contains(CurrentToken))
            {
                while (firstOfStatement.Contains(CurrentToken))
                {
                    statementList.Add(Statement());
                }
            }
            result.Add(statementList);


            Expect(TokenCategory.END);
            Expect(TokenCategory.SEMICOLON);
            return(result);
        }
示例#5
0
        public Node ProcedureDeclaration()
        {
            var result = new ProcedureDeclaration();

            Expect(TokenCategory.PROCEDURE);
            result.AnchorToken = Expect(TokenCategory.IDENTIFIER);
            Expect(TokenCategory.LEFT_PAR);

            var parameterDeclarationList = new ParameterDeclarationList();

            while (CurrentToken == TokenCategory.IDENTIFIER)
            {
                parameterDeclarationList.Add(ParameterDeclaration());
            }
            result.Add(parameterDeclarationList);

            Expect(TokenCategory.RIGHT_PAR);

            if (CurrentToken == TokenCategory.COLON)
            {
                Expect(TokenCategory.COLON);
                result.Add(Type());
            }

            Expect(TokenCategory.SEMICOLON);

            if (CurrentToken == TokenCategory.CONST)
            {
                var constantDeclarationList = new ConstantDeclarationList()
                {
                    AnchorToken = Expect(TokenCategory.CONST)
                };
                do
                {
                    constantDeclarationList.Add(ConstantDeclaration());
                } while (CurrentToken == TokenCategory.IDENTIFIER);

                result.Add(constantDeclarationList);
            }

            if (CurrentToken == TokenCategory.VAR)
            {
                var variableDeclarationList = new VariableDeclarationList()
                {
                    AnchorToken = Expect(TokenCategory.VAR)
                };
                do
                {
                    variableDeclarationList.Add(VariableDeclaration());
                } while (CurrentToken == TokenCategory.IDENTIFIER);

                result.Add(variableDeclarationList);
            }

            Expect(TokenCategory.BEGIN);

            var statementList = new StatementList();

            while (firstOfStatement.Contains(CurrentToken))
            {
                statementList.Add(Statement());
            }
            result.Add(statementList);

            Expect(TokenCategory.END);
            Expect(TokenCategory.SEMICOLON);

            return(result);
        }
示例#6
0
        public Node ProcedureDeclaration()
        {
            var procedure = new ProcedureDeclaration();

            Expect(TokenCategory.PROCEDURE);
            procedure.AnchorToken = Expect(TokenCategory.IDENTIFIER);

            var consDecList = new ConstantDeclarationList();
            var statement   = new StatementList();



            Expect(TokenCategory.INITPARENTHESIS);

            if (CurrentToken == TokenCategory.IDENTIFIER)
            {
                var parDecList = ParameterDeclaration();
                procedure.Add(parDecList);
            }

            Expect(TokenCategory.CLOSINGPARENTHESIS);
            if (CurrentToken == TokenCategory.DECLARATION)
            {
                Expect(TokenCategory.DECLARATION);
                var type = Type();
                procedure.Add(type);
            }

            Expect(TokenCategory.ENDLINE);
            if (CurrentToken == TokenCategory.CONST)
            {
                consDecList.AnchorToken = Expect(TokenCategory.CONST);
                var i = 0;
                do
                {
                    consDecList.Add(ConstantDeclaration());
                    Console.WriteLine(consDecList[i]);
                    i++;
                } while (CurrentToken == TokenCategory.IDENTIFIER);
                procedure.Add(consDecList);
            }

            if (CurrentToken == TokenCategory.VAR)
            {
                var varDecList = VariableDeclaration();
                procedure.Add(varDecList);
            }

            var tempStatement = Expect(TokenCategory.BEGIN);

            while (firstOfStatement.Contains(CurrentToken))
            {
                statement.Add(Statement());
            }
            if (statement.getLength() > 0)
            {
                statement.AnchorToken = tempStatement;
                procedure.Add(statement);
            }

            Expect(TokenCategory.END);
            Expect(TokenCategory.ENDLINE);

            return(procedure);
        }