private void DescribeTdfa(ITdfaData data)
 {
     using (IGraphView view = new GvGraphView("tdfa.gv"))
     {
         data.DescribeGraph(view);
     }
 }
Example #2
0
 public void WriteGv(string path)
 {
     using (var graph = new GvGraphView(path))
     {
         WriteGv(graph);
     }
 }
Example #3
0
        public void ResolvingAmbiguities()
        {
            using (var interp = new Interpreter <AmbiguousCalculator>())
            {
                interp.Parse("2+8/2");
                Assert.AreEqual(6, interp.Context.Result.Value);

                interp.Parse("8/4/2");
                Assert.AreEqual(1, interp.Context.Result.Value);

                interp.Parse("2+3"); // can be interpreted as a "2 * (+3)"
                Assert.AreEqual(5, interp.Context.Result.Value);

                // Check that implicit multiplication works
                interp.Parse("2 3");
                Assert.AreEqual(6, interp.Context.Result.Value);

                // A lot of ambiguities:
                interp.Parse("1+-+6/3");
                Assert.AreEqual(-1, interp.Context.Result.Value);
#if false
                using (var g = new GvGraphView("expr.tmp.gv"))
                {
                    interp.BuildTree("1+-+6/3").WriteGraph(g, interp.Grammar, true);
                }
#endif
            }
        }
Example #4
0
 public void TestDeterministicTree()
 {
     using (var interp = new Interpreter <NondeterministicCalcForTree>())
     {
         var sppf = interp.BuildTree("3^3");
         sppf.WriteIndented(interp.Grammar, Console.Out, 0);
         using (var graph = new GvGraphView(typeof(NondeterministicCalcForTree).Name + "_sppf_det.gv"))
         {
             sppf.WriteGraph(graph, interp.Grammar);
         }
     }
 }
Example #5
0
 public void TestAmbiguousWithEpsilonTree()
 {
     using (var interp = new Interpreter <RightNullableWithTree>())
     {
         var sppf = interp.BuildTree("aaab");
         sppf.WriteIndented(interp.Grammar, Console.Out, 0);
         using (var graph = new GvGraphView(typeof(RightNullableWithTree).Name + "_sppf_amb.gv"))
         {
             sppf.WriteGraph(graph, interp.Grammar, true);
         }
     }
 }
Example #6
0
 public void TestShareBranchNodesWithTree()
 {
     using (var interp = new Interpreter <ShareBranchNodesWithTree>())
     {
         var sppf = interp.BuildTree("bb");
         sppf.WriteIndented(interp.Grammar, Console.Out, 0);
         using (var graph = new GvGraphView(typeof(ShareBranchNodesWithTree).Name + "_sppf_amb.gv"))
         {
             sppf.WriteGraph(graph, interp.Grammar, true);
         }
     }
 }
Example #7
0
 public void TestRecursiveTree()
 {
     using (var interp = new Interpreter <RecursiveTree>())
     {
         var sppf = interp.BuildTree("a");
         sppf.WriteIndented(interp.Grammar, Console.Out, 0);
         using (var graph = new GvGraphView(typeof(RecursiveTree).Name + "_sppf_amb.gv"))
         {
             sppf.WriteGraph(graph, interp.Grammar, true);
         }
     }
 }
Example #8
0
 public void Build(IReportData data)
 {
     foreach (var condition in data.Grammar.Conditions)
     {
         int    i = condition.Index;
         string scanModeFileName = Path.GetFileName(fileName) + "_" + i + Path.GetExtension(fileName);
         string path             = Path.Combine(data.DestinationDirectory, scanModeFileName);
         using (var graph = new GvGraphView(path))
         {
             var dfa = data.GetScannerAutomata(condition);
             dfa.DescribeGraph(graph);
         }
     }
 }
Example #9
0
        public void Test2Sppf()
        {
            var filePath = DataSamples.CompileSample2FilePath;

            using (var source = new StreamReader(filePath))
                using (var interpreter = new Interpreter <ILLanguage>())
                {
                    SppfNode sppf = interpreter.BuildTree(source, filePath);

                    using (var graph = new GvGraphView("Cil_Sample2_sppf.gv"))
                    {
                        var lang = Language.Get(typeof(ILLanguage));
                        sppf.WriteGraph(graph, lang.Grammar, true);
                    }
                }
        }
Example #10
0
        public void Test()
        {
            var context = new SAdBLang();

            using (var interp = new Interpreter <SAdBLang>(context))
            {
                interp.Parse("d");
                Assert.IsTrue(context.IsFinalRuleCalledLast);
                Assert.IsTrue(context.WasMergeCalled);

                // with SPPF producer
                var sppf = interp.BuildTree("d");

                using (var graph = new GvGraphView(typeof(SAdBLang).Name + "_sppf.gv"))
                {
                    sppf.WriteGraph(graph, interp.Grammar, true);
                }
            }
        }
        public IReciever <Msg> Done()
        {
            Reducer(Eoi);
            Shifter(Eoi);

#if DIAGNOSTICS
            using (var graphView = new GvGraphView("GlrState" + gss.CurrentLayer + (accepted ? "Accept" : "Fail") + ".gv"))
            {
                gss.WriteGraph(graphView, grammar, stateToPriorToken);
            }
#endif

            // TODO: Remove non-accepting topmost nodes.

            if (!accepted)
            {
                throw new SyntaxException(null, Loc.Unknown, "Unexpected EOI");
            }

            return(this);
        }
        public IReciever <Msg> Next(Msg item)
        {
            Reducer(item);

            Shifter(item);

#if DIAGNOSTICS
            using (var graphView = new GvGraphView("GlrState" + gss.CurrentLayer + ".gv"))
            {
                gss.WriteGraph(graphView, grammar, stateToPriorToken);
            }
#endif

            if (gss.IsEmpty)
            {
                throw new SyntaxException(
                          null,
                          item.Location,
                          "Unexpected token " + grammar.TokenName(item.Id));
            }

            return(this);
        }
Example #13
0
        public void TestAmbiguousTree()
        {
            var lang = Language.Get(typeof(NondeterministicCalcForTree));

            using (var interp = new Interpreter <NondeterministicCalcForTree>())
            {
                var sppf = interp.BuildTree("3^3^3");
                sppf.WriteIndented(lang.Grammar, Console.Out, 0);
                using (var graph = new GvGraphView(typeof(NondeterministicCalcForTree).Name + "_sppf_amb.gv"))
                {
                    sppf.WriteGraph(graph, lang.Grammar);
                }

                var allNodes = sppf.Flatten().ToArray();

                var NUM      = lang.Identify("3");
                var numNodes = allNodes.Where(n => n.Id == NUM).Distinct(IdentityComparer.Default).ToArray();
                Assert.AreEqual(3, numNodes.Length, "Leaf SPPF nodes should be shared");

                var POW      = lang.Identify("^");
                var powNodes = allNodes.Where(n => n.Id == POW).Distinct(IdentityComparer.Default).ToArray();
                Assert.AreEqual(2, powNodes.Length, "Leaf SPPF nodes should be shared");
            }
        }
Example #14
0
        public IReceiver <Msg> Next(Msg envelope)
        {
            gss.BeginEdit();

            N.Clear();

            var     front = gss.FrontArray;
            MsgData data  = envelope.FirstData;

            do
            {
                int lookahead = data.Token;

                Actor(lookahead);
                Reducer(lookahead);

                if (accepted)
                {
                    int count = gss.Count;
                    for (int i = 0; i != count; ++i)
                    {
                        var node = front[i];
                        if (IsAccepting(node.State))
                        {
                            producer.Result = node.FirstLink.Label;
                            break;
                        }
                    }
                }

                var termValue = producer.CreateLeaf(envelope, data);
                N[GetNKey(lookahead, gss.CurrentLayer + 1)] = termValue;

                for (int i = 0; i != gss.Count; ++i)
                {
                    var frontNode = front[i];

                    // Plan shift
                    var shift = GetShift(frontNode.State, lookahead);
                    if (shift >= 0)
                    {
                        Q.Enqueue(new PendingShift(frontNode, shift, lookahead));
                    }

                    // Shift and plan reduce
                    var action = GetShiftReduce(frontNode.State, lookahead);
                    if (action.Kind == ParserActionKind.ShiftReduce)
                    {
                        PlanShiftReduce(
                            frontNode,
                            lookahead,
                            termValue,
                            action.ProductionId,
                            action.Size);
                    }
                }

                data = data.Next;
            }while (data != null);

            gss.PushLayer();
            Shifter();

            // Run reducer again to complete
            // shift-reduce and goto-reduce actions.
            Reducer();

#if DIAGNOSTICS
            if (!isVerifier)
            {
                using (var graphView = new GvGraphView("GlrState" + gss.CurrentLayer + ".gv"))
                {
                    gss.WriteGraph(graphView, grammar, stateToPriorToken);
                }
            }
#endif

            if (gss.IsEmpty)
            {
                if (accepted)
                {
                    return(FinalReceiver <Msg> .Instance);
                }

                if (isVerifier)
                {
                    return(null);
                }

                gss.Undo(0); // restore state before the current input token

                {
                    var message = new StringBuilder();
                    message
                    .Append("Unexpected token ")
                    .Append(grammar.SymbolName(envelope.Id))
                    .Append(" in state stacks: {");
                    bool firstStack = true;
                    for (int i = 0; i != gss.Count; ++i)
                    {
                        var node = gss.FrontArray[i];

                        if (firstStack)
                        {
                            firstStack = false;
                        }
                        else
                        {
                            message.Append(", ");
                        }



                        message.Append("[");
                        var  n          = node;
                        bool firstState = true;
                        while (true)
                        {
                            if (firstState)
                            {
                                firstState = false;
                            }
                            else
                            {
                                message.Append(", ");
                            }

                            message.Append(n.State);
                            if (n.State == 0)
                            {
                                break;
                            }

                            n = n.FirstLink.LeftNode;
                        }

                        message.Append("]");
                    }

                    message.Append("}");

                    logging.Write(
                        new LogEntry
                    {
                        Severity  = Severity.Verbose,
                        Location  = envelope.Location,
                        HLocation = envelope.HLocation,
                        Message   = message.ToString()
                    });
                }

                return(RecoverFromError(envelope));
            }

            gss.EndEdit();
            this.priorInput = envelope;
            return(this);
        }