Exemplo n.º 1
0
        public Node ParameterDeclaration()
        {
            var paramList = new ParameterDeclarationList();

            while (CurrentToken == TokenCategory.IDENTIFIER)
            {
                var  tempList = new VariableDeclarationList();
                Node idToken  = new Identifier()
                {
                    AnchorToken = Expect(TokenCategory.IDENTIFIER)
                };

                tempList.Add(idToken);

                while (CurrentToken == TokenCategory.COMMA)
                {
                    Expect(TokenCategory.COMMA);
                    tempList.Add(new Identifier()
                    {
                        AnchorToken = Expect(TokenCategory.IDENTIFIER)
                    });
                }
                Expect(TokenCategory.DECLARATION);
                var tipo = Type();
                //declarationList.Add(Type());
                foreach (var node in tempList)
                {
                    tipo.Add(node);
                }
                paramList.Add(tipo);
                Expect(TokenCategory.ENDLINE);
            }

            return(paramList);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 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
            });
        }
Exemplo n.º 4
0
        public Node VariableDeclaration()
        {
            var declarationList = new VariableDeclarationList();

            declarationList.AnchorToken = Expect(TokenCategory.VAR);
            do
            {
                var tempList        = new VariableDeclarationList();
                var firstIdentifier = new Identifier()
                {
                    AnchorToken = Expect(TokenCategory.IDENTIFIER)
                };
                tempList.Add(firstIdentifier);

                //declarationList.Add(firstIdentifier);
                while (CurrentToken == TokenCategory.COMMA)
                {
                    Expect(TokenCategory.COMMA);
                    tempList.Add(new Identifier()
                    {
                        AnchorToken = Expect(TokenCategory.IDENTIFIER)
                    });
                }
                Expect(TokenCategory.DECLARATION);
                var tipo = Type();
                Console.WriteLine("PERIQUEAR" + tipo);
                //declarationList.Add(Type());
                foreach (var node in tempList)
                {
                    tipo.Add(node);
                }
                declarationList.Add(tipo);
                Expect(TokenCategory.ENDLINE);
            } while (CurrentToken == TokenCategory.IDENTIFIER);
            return(declarationList);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
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);
        }