public PanicModeErrorRecovery(RuntimeGrammar grammar, IPushParser exit, ILogging logging)
 {
     this.grammar             = grammar;
     this.exit                = exit;
     this.logging             = logging;
     this.validPrefixVerifier = exit.CloneVerifier();
 }
        public void IsNullableTest()
        {
            var grammar = new Grammar();

            var S = grammar.Symbols.Add("S");
            var a = grammar.Symbols.Add("a");
            var b = grammar.Symbols.Add("b");
            var A = grammar.Symbols.Add("A");
            var B = grammar.Symbols.Add("B");

            grammar.Start = S;
            grammar.Productions.Define(S, new[] { b, A });
            grammar.Productions.Define(A, new[] { a, A, B });
            grammar.Productions.Define(A, new Symbol[0]);
            grammar.Productions.Define(B, new Symbol[0]);

            var target = new RuntimeGrammar(grammar);

            Assert.IsTrue(target.IsNullable(A.Index));
            Assert.IsTrue(target.IsNullable(B.Index));

            Assert.IsFalse(target.IsNullable(a.Index));
            Assert.IsFalse(target.IsNullable(b.Index));
            Assert.IsFalse(target.IsNullable(S.Index));
        }
        public string FormatMessage(RuntimeGrammar grammar, List <Msg> source, List <Msg> corrected)
        {
            var output = new StringBuilder();

            ProcessMessageFormat(grammar, source, corrected, output);
            return(output.ToString());
        }
Example #4
0
 public ActionProducer(
     RuntimeGrammar grammar,
     object context,
     ProductionActionDelegate grammarAction,
     MergeDelegate merge)
     : base(grammar, context, grammarAction)
 {
     this.grammar       = grammar;
     this.context       = context;
     this.grammarAction = grammarAction;
     this.merge         = merge;
     this.ruleArgBuffer = new object[grammar.MaxRuleSize];
 }
Example #5
0
        public LocalCorrectionErrorRecovery(
            RuntimeGrammar grammar,
            IPushParser exit,
            ILogging logging)
        {
            this.grammar = grammar;
            this.exit    = exit;
            this.logging = logging;

            this.terms = grammar
                         .EnumerateTokens()
                         .Where(t => grammar.IsTerminal(t) && t >= PredefinedTokens.Count)
                         .ToArray();
        }
        private string FormatToken(RuntimeGrammar grammar, Msg msg)
        {
            if (msg.Id == PredefinedTokens.Eoi)
            {
                return("end of file");
            }

            string result = grammar.SymbolName(msg.Id);

            if (!result.StartsWith("'") && msg.Value != null)
            {
                result = msg.Value.ToString();
            }

            return(result);
        }
 private DeterministicParser(
     IProducer <TNode> producer,
     RuntimeGrammar grammar,
     TransitionDelegate actionTable,
     ResourceAllocator allocator,
     ILogging logging,
     TaggedStack <TNode> stateStack)
 {
     this.producer    = producer;
     this.grammar     = grammar;
     this.rules       = grammar.Productions.ToArray();
     this.actionTable = actionTable;
     this.allocator   = allocator;
     this.logging     = logging;
     this.stateStack  = stateStack;
 }
 public DeterministicParser(
     IProducer <TNode> producer,
     RuntimeGrammar grammar,
     TransitionDelegate actionTable,
     ResourceAllocator allocator,
     ILogging logging
     )
     : this(
         producer,
         grammar,
         actionTable,
         allocator,
         logging,
         new TaggedStack <TNode>(InitialValueStackSize))
 {
     this.Reset();
 }
Example #9
0
        public ReductionPathQueue(int[] tokenComplexity, RuntimeGrammar grammar)
        {
            this.tokenComplexity = tokenComplexity;

#if false
            Debug.WriteLine("Token complexity order:");

            var tokens = Enumerable
                         .Range(0, tokenComplexity.Length)
                         .OrderBy(token => tokenComplexity[token])
            ;

            foreach (int token in tokens)
            {
                Debug.WriteLine(grammar.TokenName(token));
            }
#endif
        }
        private void ProcessMessageFormat(
            RuntimeGrammar grammar,
            List <Msg> source,
            List <Msg> corrected,
            StringBuilder output)
        {
            int i     = 0;
            int count = messageFormat.Length;

            while (i != count)
            {
                char ch = messageFormat[i];
                switch (ch)
                {
                case '%':
                    ++i;
                    if (i == count || !char.IsDigit(messageFormat[i]))
                    {
                        throw new InvalidOperationException("Invalid message format.");
                    }

                    int correctedIndex = messageFormat[i++] - '0';
                    output.Append(FormatToken(grammar, corrected[correctedIndex]));
                    break;

                case '$':
                    ++i;
                    if (i == count || !char.IsDigit(messageFormat[i]))
                    {
                        throw new InvalidOperationException("Invalid message format.");
                    }

                    int sourceIndex = messageFormat[i++] - '0';
                    output.Append(FormatToken(grammar, source[sourceIndex]));
                    break;

                default:
                    output.Append(messageFormat[i++]);
                    break;
                }
            }
        }
Example #11
0
 public RnGlrParser(
     RuntimeGrammar grammar,
     int[]               tokenComplexity,
     TransitionDelegate transition,
     int[]               stateToPriorToken,
     int[]               conflictActionsTable,
     IProducer <T> producer,
     ResourceAllocator allocator,
     ILogging logging)
     : this(
         grammar,
         tokenComplexity,
         transition,
         stateToPriorToken,
         conflictActionsTable,
         producer,
         allocator,
         logging,
         new Gss <T>(stateToPriorToken.Length + grammar.Productions.Count))
 {
 }
Example #12
0
        private RnGlrParser(
            RuntimeGrammar grammar,
            int[]               tokenComplexity,
            TransitionDelegate transition,
            int[]               stateToPriorToken,
            int[]               conflictActionsTable,
            IProducer <T> producer,
            ResourceAllocator allocator,
            ILogging logging,
            Gss <T> gss)
        {
            this.grammar              = grammar;
            this.tokenComplexity      = tokenComplexity;
            this.transition           = transition;
            this.stateToPriorToken    = stateToPriorToken;
            this.conflictActionsTable = conflictActionsTable;
            this.gss        = gss;
            this.nodeBuffer = new T[grammar.MaxRuleSize];
            this.producer   = producer;
            this.allocator  = allocator;
            this.logging    = logging;

            this.pendingReductions = new ModifiedReduction[grammar.Productions.Count];

            switch (producer.ReductionOrder)
            {
            case ReductionOrder.Unordered:
            {
                this.R = new ReductionQueue <T>();
                break;
            }

            case ReductionOrder.ByRuleDependency:
            {
                this.R = new ReductionPathQueue <T>(tokenComplexity, grammar);
                break;
            }
            }
        }
Example #13
0
 public void Init()
 {
     this.runtimeGrammar = new RuntimeGrammar(grammar);
     this.allocator = new ResourceAllocator(runtimeGrammar);
 }
 public ActionEpsilonProducer(RuntimeGrammar grammar, object context, ProductionActionDelegate productionAction)
 {
     this.grammar          = grammar;
     this.context          = context;
     this.productionAction = productionAction;
 }
Example #15
0
 public SppfProducer(RuntimeGrammar grammar)
     : base(grammar)
 {
 }
Example #16
0
        public void WriteGraph(IGraphView view, RuntimeGrammar grammar, int[] stateToSymbol)
        {
            var allAccessibleByLayer = GetAllNodes().GroupBy(state => state.Layer);

            var layers = Enumerable
                         .Range(0, currentLayer + 1)
                         .Select(i => new List <GssNode <T> >(2))
                         .ToList();

            foreach (var group in allAccessibleByLayer)
            {
                layers[group.Key].AddRange(group);
            }

            view.BeginDigraph("Gss");

            view.SetGraphProperties(rankDir: RankDir.RightToLeft);

            for (int layerIndex = 0; layerIndex != layers.Count; ++layerIndex)
            {
                var layer = layers[layerIndex];
                view.BeginCluster(layerIndex.ToString());

                view.SetGraphProperties(style: Style.Dotted, label: "U" + layerIndex);
                view.SetNodeProperties(shape: Shape.Circle);

                for (int nodeIndex = 0; nodeIndex != layer.Count; ++nodeIndex)
                {
                    view.AddNode(
                        Tuple.Create("s", layerIndex, nodeIndex),
                        label: StateName(layer[nodeIndex]));
                }

                view.EndCluster();
            }

            view.SetNodeProperties(shape: Shape.Rect);

            int linkIndex = 0;

            for (int layerIndex = 0; layerIndex != layers.Count; ++layerIndex)
            {
                var layer = layers[layerIndex];
                for (int nodeIndex = 0; nodeIndex != layer.Count; ++nodeIndex)
                {
                    var from = layer[nodeIndex];
                    if (from.State < 0)
                    {
                        continue;
                    }

                    int token = stateToSymbol[from.State];
                    var link  = from.FirstLink;
                    while (link != null)
                    {
                        var to = link.LeftNode;

                        view.AddNode(Tuple.Create("t", linkIndex), grammar.SymbolName(token));

                        view.AddEdge(
                            Tuple.Create("s", layerIndex, nodeIndex),
                            Tuple.Create("t", linkIndex)
                            );

                        view.AddEdge(
                            Tuple.Create("t", linkIndex),
                            Tuple.Create("s", to.Layer, layers[to.Layer].IndexOf(to))
                            );

                        ++linkIndex;

                        link = link.NextLink;
                    }
                }
            }

            view.EndDigraph();
        }
 public SppfEpsilonProducer(RuntimeGrammar grammar)
 {
     this.grammar = grammar;
     BuildCache();
 }
 public ActionEpsilonProducer(RuntimeGrammar grammar, object context, ProductionActionDelegate productionAction)
 {
     this.grammar          = grammar;
     this.context          = context;
     this.productionAction = productionAction;
 }
 public SppfEpsilonProducer(RuntimeGrammar grammar)
 {
     this.grammar = grammar;
     BuildCache();
 }