示例#1
0
 public static GATNode _typeSpecifier(this LL1Processor ll1)
 {
     var node = new GATNode();
     var next = WordContainer.GetWordType();
     switch (next)
     {
         case WordType.INT:
             {
                 var INT = WordContainer.Advance();
                 node.AddChild(INT);
                 node.generator = typeSpecifier1;
                 break;
             }
         case WordType.VOID:
             {
                 var VOID = WordContainer.Advance();
                 node.AddChild(VOID);
                 node.generator = typeSpecifier2;
                 break;
             }
         default:
             {
                 throw new BNFException();
             }
     }
     return node;
 }
示例#2
0
        public static GATNode _declaration(this LL1Processor ll1)
        {
            var node   = new GATNode();
            var offset = 2;

            //
            node.name = "global";
            CodeGenerator.AddLabel("global", 0);
            //
            switch (WordContainer.GetWordType(offset))
            {
            case WordType.SEMICOLON:
            {
                var varDeclaration = ll1._varDeclaration();
                node.AddChild(varDeclaration);
                node.generator = Declaration1;
                break;
            }

            case WordType.BRACKET_L:
            {
                var funDeclaration = ll1._funDeclaration();
                node.AddChild(funDeclaration);
                node.generator = Declaration2;
                break;
            }

            default:
            {
                throw new BNFException();
            }
            }
            return(node);
        }
        public static GATNode _selectionStmt(this LL1Processor ll1)
        {
            var node = new GATNode();

            node.generator = SelectionStmt;

            WordContainer.Advance(WordType.IF);
            WordContainer.Advance(WordType.BRACKET_L);
            var expression = ll1._expression();

            WordContainer.Advance(WordType.BRACKET_R);
            var statement = ll1._statement();

            node.AddChild(expression);         //0
            node.AddChild(GATNode.CodeNode()); //1
            node.AddChild(statement);          //2

            var next = WordContainer.GetWordType();

            if (next == WordType.ELSE)
            {
                WordContainer.Advance(WordType.ELSE);
                var elseStatement = ll1._statement();
                node.AddChild(GATNode.CodeNode());  //3
                node.AddChild(GATNode.LabelNode()); //4
                node.AddChild(elseStatement);       //5
            }
            node.AddChild(GATNode.LabelNode());     //3-6
            return(node);
        }
        public static GATNode _localDeclarations(this LL1Processor ll1)
        {
            var node   = new GATNode();
            var offset = 1;

            var next = WordContainer.GetWordType(offset);

            while (next == WordType.ID)
            {
                var varDeclaration = ll1._varDeclaration();
                node.AddChild(varDeclaration);
                next = WordContainer.GetWordType(offset);
            }

            if (node.ChildCount() == 0)
            {
                node.generator = LocalDeclarations2;
            }
            else
            {
                node.generator = LocalDeclarations1;
            }

            return(node);
        }
        public static GATNode _statement(this LL1Processor ll1)
        {
            var node = new GATNode();
            var next = WordContainer.GetWordType();

            if (ExpressionStmtProc.first.Contains(next))
            {
                var expressionStmt = ll1._expressionStmt();
                node.AddChild(expressionStmt);
                node.generator = Statement1;
                return(node);
            }
            var offset = 0;

            switch (WordContainer.GetWordType(offset))
            {
            case WordType.BRACE_L:
            {
                var compoundStmt = ll1._compoundStmt();
                node.AddChild(compoundStmt);
                node.generator = Statement2;
                break;
            }

            case WordType.IF:
            {
                var selectionStmt = ll1._selectionStmt();
                node.AddChild(selectionStmt);
                node.generator = Statement3;
                break;
            }

            case WordType.WHILE:
            {
                var iterationStmt = ll1._iterationStmt();
                node.AddChild(iterationStmt);
                node.generator = Statement4;
                break;
            }

            case WordType.RETURN:
            {
                var returnStmt = ll1._returnStmt();
                node.AddChild(returnStmt);
                node.generator = Statement5;
                break;
            }

            default:
            {
                throw new BNFException();
            }
            }
            return(node);
        }
        public static GATNode _program(this LL1Processor ll1)
        {
            var node = new GATNode();

            node.generator = program;
            var declarationList = ll1._declarationList();

            node.AddChild(GATNode.CodeNode()); //0
            node.AddChild(declarationList);    //1
            WordContainer.Advance(WordType.HASHTAG);
            return(node);
        }
示例#7
0
        public static GATNode _factor(this LL1Processor ll1)
        {
            var node   = new GATNode();
            var offset = 0;

            switch (WordContainer.GetWordType(offset))
            {
            case WordType.BRACKET_L:
            {
                WordContainer.Advance(WordType.BRACKET_L);
                var expression = ll1._expression();
                node.AddChild(expression);
                node.generator = Factor1;
                WordContainer.Advance(WordType.BRACKET_R);
                break;
            }

            case WordType.ID:
            {
                offset = 1;
                if (WordContainer.GetWordType(offset) == WordType.BRACKET_L)
                {
                    var CALL = ll1._call();
                    node.AddChild(CALL);
                    node.generator = Factor2;
                }
                else
                {
                    var VAR = ll1._var();
                    node.AddChild(VAR);
                    node.generator = Factor3;
                }
                break;
            }

            case WordType.NUM:
            {
                var num = WordContainer.Advance(WordType.NUM);
                node.AddChild(num);
                node.generator = Factor4;
                break;
            }

            default:
            {
                throw new BNFException();
            }
            }
            return(node);
        }
        public static GATNode _call(this LL1Processor ll1)
        {
            var node = new GATNode();

            node.generator = Call;
            var id = WordContainer.Advance(WordType.ID);

            node.AddChild(id);//0
            WordContainer.Advance(WordType.BRACKET_L);
            var args = ll1._args();

            node.AddChild(args);//1
            WordContainer.Advance(WordType.BRACKET_R);
            return(node);
        }
        public static GATNode _returnStmt(this LL1Processor ll1)
        {
            var node = new GATNode();

            node.generator = ReturnStmt;
            WordContainer.Advance(WordType.RETURN);
            var next = WordContainer.GetWordType();

            if (ExpressionProc.first.Contains(next))
            {
                var expression = ll1._expression();
                node.AddChild(expression);
            }
            WordContainer.Advance(WordType.SEMICOLON);
            return(node);
        }
        public static GATNode _compoundStmt(this LL1Processor ll1)
        {
            var node = new GATNode();

            node.generator = CompoundStmt;

            WordContainer.Advance(WordType.BRACE_L);
            var localDeclarations = ll1._localDeclarations();
            var statementList     = ll1._statementList();

            WordContainer.Advance(WordType.BRACE_R);

            node.AddChild(localDeclarations);
            node.AddChild(statementList);

            return(node);
        }
        public static GATNode _statementList(this LL1Processor ll1)
        {
            var node = new GATNode();

            node.generator = StatmentList;

            var next = WordContainer.GetWordType();

            while (StatementProc.first.Contains(next))
            {
                var statement = ll1._statement();
                node.AddChild(statement);

                next = WordContainer.GetWordType();
            }
            return(node);
        }
示例#12
0
        public static GATNode _params(this LL1Processor ll1)
        {
            var node   = new GATNode();
            var offset = 1;

            if (WordContainer.GetWordType(offset) == WordType.BRACKET_R)
            {
                WordContainer.Advance(WordType.VOID);
            }
            else
            {
                var paramList = ll1._paramList();
                node.AddChild(paramList);
                node.generator = Params;
            }
            return(node);
        }
示例#13
0
        public static GATNode _declarationList(this LL1Processor ll1)
        {
            var node = new GATNode();

            node.generator = DeclarationList;
            var declaration = ll1._declaration();

            node.AddChild(declaration);
            var next = WordContainer.GetWordType();

            while (DeclarationProc.first.Contains(next))
            {
                declaration = ll1._declaration();
                node.AddChild(declaration);
                next = WordContainer.GetWordType();
            }
            return(node);
        }
示例#14
0
        public static GATNode _var(this LL1Processor ll1)
        {
            var node = new GATNode();

            node.generator = Var;
            var id = WordContainer.Advance(WordType.ID);

            node.AddChild(id);
            var next = WordContainer.GetWordType();

            if (next == WordType.SQUARE_BRACKET_L)
            {
                WordContainer.Advance(WordType.SQUARE_BRACKET_L);
                var expression = ll1._expression();
                node.AddChild(expression);
                WordContainer.Advance(WordType.SQUARE_BRACKET_R);
            }
            return(node);
        }
示例#15
0
        public static GATNode _term(this LL1Processor ll1)
        {
            var node = new GATNode();

            node.generator = Term;
            var factor1 = ll1._factor();

            node.AddChild(factor1);
            var next = WordContainer.GetWordType();

            while (next == WordType.MULOP)
            {
                var mulop = WordContainer.Advance(WordType.MULOP);
                node.AddChild(mulop);
                var factor2 = ll1._factor();
                node.AddChild(factor2);
                next = WordContainer.GetWordType();
            }
            return(node);
        }
示例#16
0
        public static GATNode _iterationStmt(this LL1Processor ll1)
        {
            var node = new GATNode();

            node.generator = IterationStmt;
            WordContainer.Advance(WordType.WHILE);
            WordContainer.Advance(WordType.BRACKET_L);
            node.AddChild(GATNode.LabelNode());//0
            var expression = ll1._expression();

            node.AddChild(expression);         //1
            node.AddChild(GATNode.CodeNode()); //2
            WordContainer.Advance(WordType.BRACKET_R);
            var statement = ll1._statement();

            node.AddChild(statement);           //3
            node.AddChild(GATNode.CodeNode());  //4
            node.AddChild(GATNode.LabelNode()); //5
            return(node);
        }
        public static GATNode _argList(this LL1Processor ll1)
        {
            var node = new GATNode();

            node.generator = ArgList;
            var expression1 = ll1._expression();

            node.AddChild(expression1);
            var next = WordContainer.GetWordType();

            while (next == WordType.COMMA)
            {
                WordContainer.Advance(WordType.COMMA);
                var expression2 = ll1._expression();
                node.AddChild(expression2);
                next = WordContainer.GetWordType();
            }

            return(node);
        }
        public static GATNode _additiveExpression(this LL1Processor ll1)
        {
            var node = new GATNode();

            node.generator = AdditiveExpression;
            var term1 = ll1._term();

            node.AddChild(term1);
            var next = WordContainer.GetWordType();

            while (next == WordType.ADDOP)
            {
                var addop = WordContainer.Advance(WordType.ADDOP);
                node.AddChild(addop);
                var term2 = ll1._term();
                node.AddChild(term2);
                next = WordContainer.GetWordType();
            }
            return(node);
        }
示例#19
0
        public static GATNode _simpleExpression(this LL1Processor ll1)
        {
            var node = new GATNode();

            node.generator = SimpleExpression;
            var additiveExpression1 = ll1._additiveExpression();

            node.AddChild(additiveExpression1);
            var next = WordContainer.GetWordType();

            while (next == WordType.RELOP)
            {
                var relop = WordContainer.Advance(WordType.RELOP);
                node.AddChild(relop);
                var additiveExpression2 = ll1._additiveExpression();
                node.AddChild(additiveExpression2);
                next = WordContainer.GetWordType();
            }
            return(node);
        }
示例#20
0
        public static GATNode _args(this LL1Processor ll1)
        {
            var node = new GATNode();
            var next = WordContainer.GetWordType();

            if (ArgListProc.first.Contains(next))
            {
                var argList = ll1._argList();
                node.AddChild(argList);
            }

            if (node.ChildCount() == 0)
            {
                node.generator = Args2;
            }
            else
            {
                node.generator = Args1;
            }
            return(node);
        }
示例#21
0
        public static GATNode _param(this LL1Processor ll1)
        {
            var node = new GATNode();

            node.generator = Param;

            var typeSpecifier = ll1._typeSpecifier();
            var id            = WordContainer.Advance(WordType.ID);

            node.AddChild(typeSpecifier);
            node.AddChild(id);

            var next = WordContainer.GetWordType();

            if (next == WordType.SQUARE_BRACKET_L)
            {
                WordContainer.Advance(WordType.SQUARE_BRACKET_L);
                //TODO:也许要对数组特别处理
                WordContainer.Advance(WordType.SQUARE_BRACKET_R);
            }
            return(node);
        }
        public static GATNode _varDeclaration(this LL1Processor ll1)
        {
            var node = new GATNode();

            node.generator = varDeclaration;
            var typeSpecifier = ll1._typeSpecifier();
            var id            = WordContainer.Advance(WordType.ID);

            node.AddChild(typeSpecifier);
            node.AddChild(id);

            if (WordContainer.GetWordType() == WordType.SQUARE_BRACKET_L)
            {
                WordContainer.Advance(WordType.SQUARE_BRACKET_L);
                var num = WordContainer.Advance(WordType.NUM);
                WordContainer.Advance(WordType.SQUARE_BRACKET_R);

                node.AddChild(num);
            }
            WordContainer.Advance(WordType.SEMICOLON);
            return(node);
        }
        public static GATNode _paramList(this LL1Processor ll1)
        {
            var node = new GATNode();

            node.generator = ParamList;

            var param = ll1._param();

            node.AddChild(param);

            var next = WordContainer.GetWordType();

            while (next == WordType.COMMA)
            {
                WordContainer.Advance(WordType.COMMA);

                param = ll1._param();
                node.AddChild(param);
                next = WordContainer.GetWordType();
            }
            return(node);
        }
        public static GATNode _funDeclaration(this LL1Processor ll1)
        {
            var node          = new GATNode();
            var typeSpecifier = ll1._typeSpecifier();
            var id            = WordContainer.Advance(WordType.ID);

            WordContainer.Advance(WordType.BRACKET_L);
            var param = ll1._params();

            WordContainer.Advance(WordType.BRACKET_R);
            var compoundStmt = ll1._compoundStmt();

            //
            node.name = id.value;
            //
            node.generator = FunDeclaration;
            node.AddChild(typeSpecifier);       //0
            node.AddChild(id);                  //1
            node.AddChild(param);               //2
            node.AddChild(GATNode.LabelNode()); //3
            node.AddChild(compoundStmt);        //4

            return(node);
        }
示例#25
0
        static void Main(string[] args)
        {
            var    output    = Processor.WordAnalyse(FileManager.ReadFile("test.txt"));
            string midString = string.Empty;
            int    index     = 0;

            foreach (var v in output)
            {
                midString += v.ToString() + '\n';
                Console.WriteLine($"{index++} : {v.ToString()}");
            }
            FileManager.WriteFile("midString.txt", midString);

            WordContainer.InjectData(output);

            //Console.WriteLine();
            //Console.Write(WordContainer.GetString());

            var ll1 = new LL1Processor();

            ll1.StartProcess();
            ll1.StartGenerate();

            Console.Write(CodeGenerator.CodeToString());
            Console.Write(CodeGenerator.SymbolToString());
            Console.Write(CodeGenerator.LabelToString());

            /*
             * FileManager.WriteFile("ttttttt.json", JsonConvert.SerializeObject(new Anony{
             * data = "aaaa\\\"aaaa"
             * }));
             * Anony obj = JsonConvert.DeserializeObject<Anony>(FileManager.ReadFile("ttttttt.json"));
             *
             * Console.WriteLine(obj.data);
             */
        }
示例#26
0
        public static GATNode _expression(this LL1Processor ll1)
        {
            var node         = new GATNode();
            var offset       = 0;
            var search       = WordContainer.GetWordType(offset);
            var depth        = 0;
            var finishSearch = false;

            while (search != WordType.IGNORE)
            {
                switch (search)
                {
                case WordType.SEMICOLON:
                {
                    var simpleExpression = ll1._simpleExpression();
                    node.AddChild(simpleExpression);
                    node.generator = Expression1;
                    finishSearch   = true;
                    break;
                }

                case WordType.EQUAL:
                {
                    var VAR = ll1._var();
                    node.AddChild(VAR);
                    node.generator = Expression2;
                    WordContainer.Advance(WordType.EQUAL);
                    var expression = ll1._expression();
                    node.AddChild(expression);
                    finishSearch = true;
                    break;
                }

                case WordType.BRACKET_L:
                {
                    depth++;
                    break;
                }

                case WordType.BRACKET_R:
                {
                    depth--;
                    if (depth < 0)
                    {
                        var simpleExpression = ll1._simpleExpression();
                        node.AddChild(simpleExpression);
                        node.generator = Expression1;
                        finishSearch   = true;
                    }
                    break;
                }
                }
                if (finishSearch)
                {
                    break;
                }
                offset++;
                search = WordContainer.GetWordType(offset);
            }

            return(node);
        }