Ejemplo n.º 1
0
        private ILrParserTable BuildReductionModifiedLRTable(ILrDfa dfa)
        {
            ILrParserTable result = new ReductionModifiedLrDfaTable(dfa, this.data);

            FillAmbiguousTokenActions(dfa.States, isGlr: true);
            return(result);
        }
Ejemplo n.º 2
0
        public ConfigurableLrTable(ILrDfa dfa, RuntimeOptions flags)
        {
            this.grammar = dfa.GrammarAnalysis;

            this.data = new MutableTable <int>(dfa.States.Length, grammar.Symbols.Count);

            Configure(dfa, flags, out underlyingTable);
        }
        public ConfigurableLrTable(ILrDfa dfa, RuntimeOptions flags)
        {
            this.grammar = dfa.GrammarAnalysis;

            this.data = new MutableTable<int>(dfa.States.Length, grammar.Symbols.Count);

            Configure(dfa, flags, out underlyingTable);
        }
        private ILrParserTable BuildCanonicalLRTable(ILrDfa dfa)
        {
            ILrParserTable result = new CanonicalLrDfaTable(dfa, this.data);
            if (result.RequiresGlr || !FillAmbiguousTokenActions(dfa.States, isGlr:false))
            {
                result = null;
            }

            return result;
        }
        public CanonicalLrDfaTable(ILrDfa dfa, IMutableTable<int> actionTable)
        {
            var flag = LrTableOptimizations.EliminateLr0ReduceStates;
            this.canOptimizeReduceStates = (dfa.Optimizations & flag) == flag;

            this.grammar = dfa.GrammarAnalysis;
            this.actionTable = actionTable ?? new MutableTable<int>(dfa.States.Length, grammar.Symbols.Count);
            FillDfaTable(dfa.States);
            BuildConflictTable();
        }
Ejemplo n.º 6
0
        private ILrParserTable BuildCanonicalLRTable(ILrDfa dfa)
        {
            ILrParserTable result = new CanonicalLrDfaTable(dfa, this.data);

            if (result.RequiresGlr || !FillAmbiguousTokenActions(dfa.States, isGlr: false))
            {
                result = null;
            }

            return(result);
        }
Ejemplo n.º 7
0
        public CanonicalLrDfaTable(ILrDfa dfa, IMutableTable <int> actionTable)
        {
            var flag = LrTableOptimizations.EliminateLr0ReduceStates;

            this.canOptimizeReduceStates = (dfa.Optimizations & flag) == flag;

            this.grammar     = dfa.GrammarAnalysis;
            this.actionTable = actionTable ?? new MutableTable <int>(dfa.States.Length, grammar.Symbols.Count);
            FillDfaTable(dfa.States);
            BuildConflictTable();
        }
        public ReductionModifiedLrDfaTable(ILrDfa dfa, IMutableTable<int> actionTable = null)
        {
            var flag = LrTableOptimizations.EliminateLr0ReduceStates;
            this.canOptimizeReduceStates = (dfa.Optimizations & flag) == flag;

            this.grammar = dfa.GrammarAnalysis;
            var states = dfa.States;
            this.actionTable = actionTable ?? new MutableTable<int>(states.Length, dfa.GrammarAnalysis.Symbols.Count);
            FillDfaTable(states);
            BuildConflictActionTable();
        }
Ejemplo n.º 9
0
        private bool Configure(ILrDfa dfa, RuntimeOptions flags, out ILrParserTable outputTable)
        {
            bool result;

            ComplyWithConfiguration = true;
            switch (flags & RuntimeOptions.ParserAlgorithmMask)
            {
            case RuntimeOptions.ForceDeterministic:
                outputTable = BuildCanonicalLRTable(dfa);
                RequiresGlr = false;
                result      = outputTable != null && !outputTable.RequiresGlr;
                if (!result)
                {
                    data.Clear();
                    ComplyWithConfiguration = false;
                    RequiresGlr             = true;
                    outputTable             = BuildReductionModifiedLRTable(dfa);
                }

                break;

            case RuntimeOptions.ForceNonDeterministic:
                RequiresGlr = true;
                outputTable = BuildReductionModifiedLRTable(dfa);
                result      = outputTable != null;
                break;

            case RuntimeOptions.AllowNonDeterministic:
                outputTable = BuildCanonicalLRTable(dfa);
                result      = outputTable != null && !outputTable.RequiresGlr;
                if (!result)
                {
                    data.Clear();
                    goto case RuntimeOptions.ForceNonDeterministic;
                }

                RequiresGlr = false;
                break;

            default:
#if DEBUG
                throw new InvalidOperationException(
                          "Internal error: unsupported language flags: " + (int)flags);
#else
                result      = false;
                outputTable = null;
                break;
#endif
            }

            return(result);
        }
Ejemplo n.º 10
0
        public ReductionModifiedLrDfaTable(ILrDfa dfa, IMutableTable <int> actionTable = null)
        {
            var flag = LrTableOptimizations.EliminateLr0ReduceStates;

            this.canOptimizeReduceStates = (dfa.Optimizations & flag) == flag;

            this.grammar = dfa.GrammarAnalysis;
            var states = dfa.States;

            this.actionTable = actionTable ?? new MutableTable <int>(states.Length, dfa.GrammarAnalysis.Symbols.Count);
            FillDfaTable(states);
            BuildConflictActionTable();
        }
Ejemplo n.º 11
0
        private bool Configure(ILrDfa dfa, RuntimeOptions flags, out ILrParserTable outputTable)
        {
            bool result;

            ComplyWithConfiguration = true;
            switch (flags & RuntimeOptions.ParserAlgorithmMask)
            {
                case RuntimeOptions.ForceDeterministic:
                    outputTable = BuildCanonicalLRTable(dfa);
                    RequiresGlr = false;
                    result = outputTable != null && !outputTable.RequiresGlr;
                    if (!result)
                    {
                        data.Clear();
                        ComplyWithConfiguration = false;
                        RequiresGlr   = true;
                        outputTable = BuildReductionModifiedLRTable(dfa);
                    }

                    break;
                case RuntimeOptions.ForceNonDeterministic:
                    RequiresGlr   = true;
                    outputTable = BuildReductionModifiedLRTable(dfa);
                    result = outputTable != null;
                    break;
                case RuntimeOptions.AllowNonDeterministic:
                    outputTable = BuildCanonicalLRTable(dfa);
                    result = outputTable != null && !outputTable.RequiresGlr;
                    if (!result)
                    {
                        data.Clear();
                        goto case RuntimeOptions.ForceNonDeterministic;
                    }

                    RequiresGlr   = false;
                    break;
                default:
            #if DEBUG
                    throw new InvalidOperationException(
                        "Internal error: unsupported language flags: " + (int)flags);
            #else
                    result = false;
                    outputTable = null;
                    break;
            #endif
            }

            return result;
        }
Ejemplo n.º 12
0
 private ILrParserTable BuildReductionModifiedLRTable(ILrDfa dfa)
 {
     ILrParserTable result = new ReductionModifiedLrDfaTable(dfa, this.data);
     FillAmbiguousTokenActions(dfa.States, isGlr: true);
     return result;
 }
Ejemplo n.º 13
0
 public static int[] GetStateToSymbolTable(this ILrDfa lrDfa)
 {
     return(Array.ConvertAll(lrDfa.States, state => state.GetStateToken()));
 }
Ejemplo n.º 14
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);
        }
Ejemplo n.º 15
0
        private static List <LocalContextBinding> CollectLocalContexts(Grammar grammar, ILrDfa lrDfa)
        {
            var result = new List <LocalContextBinding>();

            var states     = lrDfa.States;
            int stateCount = states.Length;

            for (int parentState = 0; parentState != stateCount; ++parentState)
            {
                foreach (var item in states[parentState].Items)
                {
                    if (item.Position == 0 || item.IsReduce)
                    {
                        // Skip items in which local context cannot be provided.
                        continue;
                    }

                    var providingProd   = grammar.Productions[item.ProductionId];
                    var providingSymbol = providingProd.Pattern[0];
                    var childSymbol     = providingProd.Pattern[item.Position];

                    foreach (var consumingProd in childSymbol.Productions)
                    {
                        var action = consumingProd.Actions[0];

                        if (providingSymbol.LocalContextProvider.Provides(action.ContextRef))
                        {
                            result.Add(
                                new LocalContextBinding
                            {
                                StackState    = parentState,
                                StackLookback = item.Position,
                                Provider      = providingSymbol.LocalContextProvider,
                                Consumer      = action.ContextRef
                            });
                        }
                    }
                }
            }

            return(result);
        }