Example #1
0
 private int ComputeHashCode()
 {
     return(HashCode.Compute(
                ((int)NodeType).GetHashCode(),
                Location.GetHashCode(),
                Origin.GetHashCode(),
                DottedRule.GetHashCode()));
 }
Example #2
0
        public IIntermediateForestNode AddOrGetExistingIntermediateNode(DottedRule dottedRule, int origin, int location)
        {
            var key = (dottedRule, origin, location);

            if (this._intermediateNodes.TryGetValue(key, out var intermediateNode))
            {
                return(intermediateNode);
            }

            intermediateNode = new IntermediateForestNode(dottedRule, origin, location);
            this._intermediateNodes.Add(key, intermediateNode);
            return(intermediateNode);
        }
Example #3
0
        private IForestNode CreateParseNode(
            DottedRule nextDottedRule,
            int origin,
            IForestNode w,
            IForestNode v,
            int location)
        {
            Assert.IsNotNull(v, nameof(v));
            var anyPreDotRuleNull = true;

            if (nextDottedRule.Dot > 1)
            {
                var preDotPrecursorSymbol = nextDottedRule.Production[nextDottedRule.Dot - 2];
                anyPreDotRuleNull = IsSymbolTransitiveNullable(preDotPrecursorSymbol);
            }

            var anyPostDotRuleNull = IsSymbolTransitiveNullable(nextDottedRule.PostDotSymbol);

            if (anyPreDotRuleNull && !anyPostDotRuleNull)
            {
                return(v);
            }

            IInternalForestNode internalNode;

            if (anyPostDotRuleNull)
            {
                internalNode = NodeSet.AddOrGetExistingSymbolNode(nextDottedRule.Production.LeftHandSide, origin, location);
            }
            else
            {
                internalNode = NodeSet.AddOrGetExistingIntermediateNode(nextDottedRule, origin, location);
            }

            // if w = null and y doesn't have a family of children (v)
            if (w == null)
            {
                internalNode.AddUniqueFamily(v);
            }

            // if w != null and y doesn't have a family of children (w, v)
            else
            {
                internalNode.AddUniqueFamily(v, w);
            }

            return(internalNode);
        }
Example #4
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 dottedRule  = new DottedRule(grammar.Productions[0], 0);
            var firstState  = new NormalState(dottedRule, 1);
            var secondState = new NormalState(dottedRule, 1);

            chart.Enqueue(0, firstState);
            chart.Enqueue(0, secondState);
            Assert.AreEqual(1, chart.EarleySets[0].Predictions.Count);
        }
Example #5
0
        public override bool Equals(object obj)
        {
            if (obj is null)
            {
                return(false);
            }

            if (!(obj is IIntermediateForestNode intermediateNode))
            {
                return(false);
            }

            return(Location == intermediateNode.Location &&
                   Origin == intermediateNode.Origin &&
                   NodeType == intermediateNode.NodeType &&
                   DottedRule.Equals(intermediateNode.DottedRule));
        }
Example #6
0
 public IntermediateForestNode(DottedRule dottedRule, int origin, int location)
     : base(origin, location)
 {
     DottedRule    = dottedRule;
     this.hashCode = (DottedRule, Origin, Location).GetHashCode();
 }
Example #7
0
 private static bool IsComplete(DottedRule preComputedState)
 {
     return(preComputedState.Position == preComputedState.Production.RightHandSide.Count);
 }
Example #8
0
        private bool IsStartState(DottedRule state)
        {
            var start = _preComputedGrammar.Grammar.Start;

            return(state.Production.LeftHandSide.Equals(start));
        }
Example #9
0
 private static ISymbol GetPostDotSymbol(DottedRule preComputedState)
 {
     return(preComputedState.Production.RightHandSide[preComputedState.Position]);
 }
Example #10
0
 private static bool IsCompleted(DottedRule dottedRule)
 {
     return(dottedRule.Production.RightHandSide.Count == dottedRule.Position);
 }