Пример #1
0
        public virtual void ProcessNonCombinedGrammar(Grammar g, bool gencode)
        {
            if (g.ast == null || g.ast.hasErrors)
                return;
            if (internalOption_PrintGrammarTree)
                ConsoleOut.WriteLine(g.ast.ToStringTree());

            bool ruleFail = CheckForRuleIssues(g);
            if (ruleFail)
                return;

            int prevErrors = errMgr.GetNumErrors();
            // MAKE SURE GRAMMAR IS SEMANTICALLY CORRECT (FILL IN GRAMMAR OBJECT)
            SemanticPipeline sem = new SemanticPipeline(g);
            sem.Process();

            if (errMgr.GetNumErrors() > prevErrors)
                return;

            // BUILD ATN FROM AST
            ATNFactory factory;
            if (g.IsLexer())
                factory = new LexerATNFactory((LexerGrammar)g);
            else
                factory = new ParserATNFactory(g);
            g.atn = factory.CreateATN();

            if (generate_ATN_dot)
                GenerateATNs(g);

            // PERFORM GRAMMAR ANALYSIS ON ATN: BUILD DECISION DFAs
            AnalysisPipeline anal = new AnalysisPipeline(g);
            anal.Process();

            //if ( generate_DFA_dot ) generateDFAs(g);

            if (g.tool.GetNumErrors() > prevErrors)
                return;

            // GENERATE CODE
            if (gencode)
            {
                CodeGenPipeline gen = new CodeGenPipeline(g);
                gen.Process();
            }
        }
Пример #2
0
        public void testActions(string templates, string actionName, string action, string expected)
        {
            int     lp    = templates.indexOf('(');
            string  name  = templates.substring(0, lp);
            STGroup group = new STGroupstring(templates);
            ST      st    = group.getInstanceOf(name);

            st.add(actionName, action);
            string     grammar = st.render();
            ErrorQueue equeue  = new ErrorQueue();
            Grammar    g       = new Grammar(grammar);

            if (g.ast != null && !g.ast.hasErrors)
            {
                SemanticPipeline sem = new SemanticPipeline(g);
                sem.process();

                ATNFactory factory = new ParserATNFactory(g);
                if (g.isLexer())
                {
                    factory = new LexerATNFactory((LexerGrammar)g);
                }
                g.atn = factory.createATN();

                CodeGenerator gen          = new CodeGenerator(g);
                ST            outputFileST = gen.generateParser();
                string        output       = outputFileST.render();
                //Console.WriteLine(output);
                string b       = "#" + actionName + "#";
                int    start   = output.indexOf(b);
                string e       = "#end-" + actionName + "#";
                int    end     = output.indexOf(e);
                string snippet = output.substring(start + b.length(), end);
                assertEquals(expected, snippet);
            }
            if (equeue.Count > 0)
            {
                Console.Error.WriteLine(equeue.tostring(g.tool));
            }
        }