Beispiel #1
0
        public static void CheckTraversal(Grammar g, Sentence sentence, CFGLib.Parsers.Sppf.SppfNode sppf)
        {
            if (sppf == null)
            {
                return;
            }
            var t = new Traversal(sppf, g);
            TraverseResultCollection r = null;

            r = t.Traverse();
            foreach (var option in r)
            {
                if (!(option.Payload is Sentence))
                {
                }
                var sgen = (Sentence)option.Payload;
                if (!sentence.SequenceEqual(sgen))
                {
                    throw new Exception();
                }
            }
        }
Beispiel #2
0
        private void BuildAndSetResult(SppfNode node)
        {
            if (_cache.ContainsKey(node) || _branchCache.ContainsKey(node))
            {
                return;
            }
            if (node.Families.Count() == 0)
            {
                if (node is SppfEpsilon)
                {
                    _cache[node] = new TraverseResultCollection(new List <TraverseResult> {
                        new TraverseResult("", node, null)
                    });
                }
                else if (node is SppfWord sppfWord)
                {
                    if (!(sppfWord.Word is Terminal terminal))
                    {
                        throw new Exception();
                    }
                    _cache[node] = new TraverseResultCollection(new List <TraverseResult> {
                        new TraverseResult(terminal.Name, node, null)
                    });
                }
                return;
            }

            var resultList = new List <TraverseResult>();

            foreach (var family in node.Families)
            {
                // var argList = new List<TraverseResultCollection>();
                if (family.Members.Count == 0)
                {
                    throw new NotImplementedException();
                }
                else if (family.Members.Count == 1)
                {
                    var child      = family.Members[0];
                    var childValue = _cache[child];

                    if (node is SppfBranch)                       // this only happens in old (not contracted) sppf
                    {
                        if (family.Production != null)
                        {
                            throw new Exception();
                        }
                        var newValues = new TraverseResultCollection[] { childValue };
                        _branchCache[node] = newValues;
                    }
                    else
                    {
                        if (family.Production == null)
                        {
                            throw new Exception();
                        }
                        foreach (var tr in childValue)
                        {
                            var args = new TraverseResult[1] {
                                tr
                            };
                            var payload   = GetPayload(args, family.Production);
                            var oneResult = new TraverseResult(payload, node, family.Production);
                            resultList.Add(oneResult);
                        }
                    }
                }
                else if (family.Members.Count == 2)
                {
                    var left  = family.Members[0];
                    var right = family.Members[1];

                    if (family.Production == null)
                    {
                        if (left is SppfBranch)                           // middle of long production
                        {
                            var leftValues = _branchCache[left];
                            var rightValue = _cache[right];
                            var newValues  = AppendToArray(leftValues, rightValue);
                            _branchCache[node] = newValues;
                        }
                        else                             // bottom left of a long production
                        {
                            var leftValue  = _cache[left];
                            var rightValue = _cache[right];
                            var newValues  = new TraverseResultCollection[] { leftValue, rightValue };
                            _branchCache[node] = newValues;
                        }
                    }
                    else                         // top of production
                    {
                        var rightValue = _cache[right];
                        TraverseResultCollection[] args;
                        if (left is SppfBranch)
                        {
                            var leftValues = _branchCache[left];
                            args = AppendToArray(leftValues, rightValue);
                        }
                        else
                        {
                            var leftValue = _cache[left];
                            args = new TraverseResultCollection[] { leftValue, rightValue };
                        }
                        var someResults = BuildResultList(args, node, family.Production);
                        resultList.AddRange(someResults);
                        // throw new NotImplementedException();
                    }
                }
                else
                {
                    throw new Exception();
                }

                //foreach (var child in family.Members) {
                //	if (!_cache.TryGetValue(child, out var childValue)) {
                //		throw new Exception();
                //	}
                //	argList.Add(childValue);
                //}
                // argSet.Add(argList.ToArray());
            }
            var collection = new TraverseResultCollection(resultList);

            _cache[node] = collection;
            // var value = new TraverseResultCollection
            // var payload = GetPayload(_emptyArgs, )
        }