コード例 #1
0
        public void Test()
        {
            const int tokenCount = 50;
            const int ruleSize   = 10;

            int[] tokens = new int[tokenCount];

            this.grammar = new Grammar();

            for (int i = 0; i != tokenCount; ++i)
            {
                tokens[i] = grammar.Symbols.Add(i.ToString()).Index;
            }

            int iterationCount = tokenCount - ruleSize;

            for (int i = 0; i < iterationCount; ++i)
            {
                int   outcome = tokens[i];
                int[] pattern = new int[ruleSize];
                Array.Copy(tokens, i + 1, pattern, 0, ruleSize);
                grammar.Productions.Define(outcome, pattern);
            }

            var target = new Lalr1Dfa(new GrammarAnalysis(grammar), LrTableOptimizations.Default);
        }
コード例 #2
0
        private bool Build(ILogging logging, out LanguageData result)
        {
            this.logging = logging;

            result = new LanguageData();

            var readerType = Type.GetType(source.ReaderTypeName);

            if (readerType == null)
            {
                logging.Write(
                    new LogEntry
                {
                    Severity = Severity.Error,
                    Message  = string.Format(
                        "Unable to find grammar reader '{0}' for language '{1}'",
                        source.ReaderTypeName,
                        source.LanguageName),
                    Origin = source.Origin
                });
                return(false);
            }

            var reader  = (IGrammarReader)Activator.CreateInstance(readerType);
            var grammar = reader.Read(source, logging);

            if (grammar == null)
            {
                return(false);
            }

            grammar.Joint.Add(source);

//            var inliner = new ProductionInliner(grammar);
//            grammar = inliner.Inline();

            if (!bootstrap && !CompileScannerTdfas(grammar))
            {
                result = null;
                return(false);
            }

            // Build parsing tables
            ILrDfa parserDfa = null;

            var grammarAnalysis = new GrammarAnalysis(grammar);

            logging.WithTimeLogging(
                source.LanguageName,
                source.Origin,
                () =>
            {
                parserDfa = new Lalr1Dfa(grammarAnalysis, LrTableOptimizations.Default);
            },
                "building LALR1 DFA");

            if (parserDfa == null)
            {
                result = null;
                return(false);
            }

            var lrTable = new ConfigurableLrTable(parserDfa, grammar.Options);

            if (!lrTable.ComplyWithConfiguration)
            {
                grammar.Reports.Add(new ConflictMessageBuilder(logging));
            }

            var localParseContexts = CollectLocalContexts(grammar, parserDfa);

            // Prepare language data for the language assembly generation
            result.IsDeterministic           = !lrTable.RequiresGlr;
            result.Grammar                   = grammar;
            result.TokenComplexity           = grammarAnalysis.GetTokenComplexity();
            result.StateToToken              = parserDfa.GetStateToSymbolTable();
            result.ParserActionTable         = lrTable.GetParserActionTable();
            result.ParserConflictActionTable = lrTable.GetConflictActionTable();

            result.LocalParseContexts = localParseContexts.ToArray();

            if (!bootstrap)
            {
                IReportData reportData = new ReportData(source, result, lrTable.Conflicts, parserDfa.States);
                foreach (var report in grammar.Reports)
                {
                    report.Build(reportData);
                }
            }

            return(true);
        }