예제 #1
0
        public IEnumerable <string> Build(Grammar grammar, GenOptions options)
        {
            this.report            = new GrammarReport <int, object>();
            this.grammar           = grammar;
            this.precedenceTable   = new PrecedenceTable <int>(grammar.SymbolsRep);
            this.functionsRegistry = new FunctionRegistry();

            transformMultiplications();

            try
            {
                createRules();
                if (grammar.PostValidate(s => report.AddError(s), s => report.AddWarning(s)))
                {
                    createPrecedences();
                }
            }
            catch (ParseControlException ex)
            {
                Console.WriteLine(ex.Message);
                return(null);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while building parser: " + ex.Message);
                Console.WriteLine(ex.StackTrace);
                return(null);
            }


            Productions <int, object> productions = productionBuilder.GetProductions(grammar.GetSymbolId(Grammar.EOFSymbol),
                                                                                     grammar.GetSymbolId(Grammar.ErrorSymbol), report);

            if (report.HasGrammarErrors)
            {
                Console.WriteLine(String.Join(Environment.NewLine, report.ReportGrammarProblems()));
                return(null);
            }
            else
            {
                ActionTable <int, object> action_table = ParserFactory.CreateActionTable(productions, precedenceTable, report, lookaheadWidth);

                if (action_table == null)
                {
                    if (!options.NoOutput)
                    {
                        Console.WriteLine("Grammar has errors, reports were written " + report.WriteReports("report_"));
                    }
                    return(null);
                }
                else
                {
                    if (options.ReportOther)
                    {
                        Console.WriteLine("Reports were written " + report.WriteReports("report_"));
                    }

                    return(buildNamespaceHeader(grammar)
                           .Concat(buildClassHeader(grammar.ParserTypeInfo))
                           .Concat(buildRules(action_table, grammar.TokenTypeInfo,
                                              (id => grammar.TokenTypeInfo.FieldNameOf(grammar.GetSymbolName(id))), grammar.TreeNodeName))
                           .Concat(buildClassFooter())
                           .Concat(buildNamespaceFooter())
                           );
                }
            }
        }