Exemple #1
0
 public void StateToStringShouldCreateCorrectFormat()
 {
     var state = new NormalState(
         new Production(new NonTerminal("A"), new NonTerminal("B"), new NonTerminal("C")),
         1, 0);
     Assert.AreEqual("A -> B\u25CFC\t\t(0)", state.ToString());
 }
Exemple #2
0
 public IState NextState()
 {
     if (IsComplete)
         return null;
     var state = new NormalState(
         Production,
         Position + 1,
         Origin);
     return state;
 }
Exemple #3
0
 public void ChartEnqueShouldAvoidDuplication()
 {
     ProductionExpression L = "L";
     var aToZ = new RangeTerminal('a', 'z');
     L.Rule = L + aToZ | aToZ;
     var grammar = new GrammarExpression(L, new[] { L }).ToGrammar();
     var chart = new Chart();
     var firstState = new NormalState(grammar.Productions[0], 0, 1);
     var secondState = new NormalState(grammar.Productions[0], 0, 1);
     chart.Enqueue(0, firstState);
     chart.Enqueue(0, secondState);
     Assert.AreEqual(1, chart.EarleySets[0].Predictions.Count);
 }
Exemple #4
0
        private void PredictProduction(INormalState 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 NormalState(production, 0, j);
            if (_chart.Enqueue(j, predictedState))
                Log("Predict", j, predictedState);

            var isNullable = Grammar.IsNullable(evidence.PostDotSymbol as INonTerminal);
            if (isNullable)
            {
                var nullParseNode = CreateNullParseNode(evidence.PostDotSymbol, j);
                var aycockHorspoolState = evidence.NextState();
                var evidenceParseNode = evidence.ParseNode as IInternalForestNode;
                if (evidenceParseNode == null)
                    aycockHorspoolState.ParseNode = CreateParseNode(aycockHorspoolState, null, nullParseNode, j);
                else if (evidenceParseNode.Children.Count > 0
                    && evidenceParseNode.Children[0].Children.Count > 0)
                {
                    var firstChildNode = evidenceParseNode;
                    var parseNode = CreateParseNode(aycockHorspoolState, firstChildNode, nullParseNode, j);
                    aycockHorspoolState.ParseNode = parseNode;
                }
                if (_chart.Enqueue(j, aycockHorspoolState))
                    Log("Predict", j, aycockHorspoolState);
            }
        }
Exemple #5
0
        private void LeoComplete(ITransitionState transitionState, IState completed, int k)
        {
            var earleySet = _chart.EarleySets[transitionState.Index];
            var rootTransitionState = earleySet.FindTransitionState(
                transitionState.PreDotSymbol);

            if (rootTransitionState == null)
                rootTransitionState = transitionState;

            var virtualParseNode = CreateVirtualParseNode(completed, k, rootTransitionState);

            var topmostItem = new NormalState(
                transitionState.Production,
                transitionState.Position,
                transitionState.Origin);

            topmostItem.ParseNode = virtualParseNode;

            if (_chart.Enqueue(k, topmostItem))
                Log("Complete", k, topmostItem);
        }
Exemple #6
0
 private void Initialize()
 {
     Location = 0;
     _chart = new Chart();
     foreach (var startProduction in Grammar.StartProductions())
     {
         var startState = new NormalState(startProduction, 0, 0);
         if (_chart.Enqueue(0, startState))
             Log("Start", 0, startState);
     }
     ReductionPass(Location);
 }