Ejemplo n.º 1
0
 public void Test_State_That_ToString_Renders_A_B_C_State()
 {
     var state = new State(
         new Production("A", new NonTerminal("B"), new NonTerminal("C")),
         1,0);
     Assert.AreEqual("A -> B\u25CFC\t\t(0)", state.ToString());
 }
Ejemplo n.º 2
0
 public void Test_Chart_That_Enqueue_Avoids_Duplicates()
 {
     var grammar = new GrammarBuilder("L")
         .Production("L", r => r
             .Rule("L", new RangeTerminal('a', 'z'))
             .Rule(new RangeTerminal('a','z')))
         .ToGrammar();
     var chart = new Chart();
     var firstState = new State(grammar.Productions[0], 0, 1);
     var secondState = new State(grammar.Productions[0], 0, 1);
     chart.Enqueue(0, firstState);
     chart.Enqueue(0, secondState);
     Assert.AreEqual(1, chart.EarleySets[0].Predictions.Count);
 }
Ejemplo n.º 3
0
        private void PredictProduction(IState evidence, int j, IProduction production)
        {
            // TODO: Pre-Compute Leo Items. If item is 1 step from being complete, add a transition item
            var predictedState = new State(production, 0, j);
            if (_chart.Enqueue(j, predictedState))
                Log("Predict", j, predictedState);

            var stateIsNullable = predictedState.Production.IsEmpty;
            if (stateIsNullable)
            {
                var aycockHorspoolState = evidence.NextState(j);

                var predictedParseNode = CreateNullParseNode(
                    predictedState.Production.LeftHandSide, j);

                aycockHorspoolState.ParseNode
                    = CreateParseNode(
                        aycockHorspoolState,
                        evidence.ParseNode,
                        predictedParseNode,
                        j);

                if (_chart.Enqueue(j, aycockHorspoolState))
                    Log("Predict", j, aycockHorspoolState);
            }
        }
Ejemplo n.º 4
0
        private void LeoComplete(ITransitionState transitionState, IState completed, int k)
        {
            var earleySet = _chart.EarleySets[transitionState.Position];
            var rootTransitionState = earleySet.FindTransitionState(
                transitionState.PreDotSymbol);

            if (rootTransitionState == null)
                rootTransitionState = transitionState;

            var virtualParseNode = new VirtualNode(k, rootTransitionState, completed.ParseNode);

            var topmostItem = new State(
                transitionState.Production,
                transitionState.Length,
                transitionState.Origin,
                virtualParseNode);

            if (_chart.Enqueue(k, topmostItem))
                Log("Complete", k, topmostItem);
        }
Ejemplo n.º 5
0
 private void Initialize()
 {
     Location = 0;
     _chart = new Chart();
     foreach (var startProduction in Grammar.StartProductions())
     {
         var startState = new State(startProduction, 0, 0);
         if (_chart.Enqueue(0, startState))
             Log("Start", 0, startState);
     }
     ReductionPass(Location);
 }