示例#1
0
 public SyntaxTreeNode this[Nonterminal nonterminal]
 {
     get
     {
         return(_children.FirstOrDefault(child => child.Value.Equals(nonterminal)));
     }
 }
示例#2
0
        private static SymbolsChain FirstRightSideChain(SymbolsChain chain, Nonterminal nonterminal)
        {
            int index = chain.FindIndex(nonterminal);

            if (index == -1)
            {
                return(null);
            }

            return(chain.Subchain(index + 1));
        }
示例#3
0
        public static ISet <Terminal> Follow(IEnumerable <Production> productions, Nonterminal nonterminal, Nonterminal axiom)
        {
            var follow = CreateNewEmptyTerminalSet();

            if (nonterminal.Equals(axiom))
            {
                follow.Add(GeneralizedTerminal.EndOfText);
            }

            // X -> ... A ..., A - specified symbol
            var appropriateProductions = productions.Where(p => p.RightPart.Contains(nonterminal));

            foreach (var production in appropriateProductions)
            {
                // X -> ... A
                if (production.RightPart.Last.Equals(nonterminal))
                {
                    // preventing infinite follow call
                    if (!production.LeftPart.Equals(nonterminal))
                    {
                        follow.UnionWith(Follow(productions, production.LeftPart, axiom));
                    }
                }

                foreach (var rightSideChain in RightSideChains(production.RightPart, nonterminal))
                //var rightSideChain = FirstRightSideChain(production.RightPart, nonterminal);
                {
                    // X -> ... A g
                    //if (!production.RightPart.Last.Equals(nonterminal))
                    //{
                    follow.UnionWith(First(productions, rightSideChain).ExceptTerminal(GeneralizedTerminal.Epsilon));
                    //}

                    // X -> ... A h, where h is epsilon generating chain
                    if (First(productions, rightSideChain).Contains(GeneralizedTerminal.Epsilon))
                    {
                        // preventing infinite follow call
                        if (!production.LeftPart.Equals(nonterminal))
                        {
                            follow.UnionWith(Follow(productions, production.LeftPart, axiom));
                        }
                    }
                }
            }

            return(follow);
        }
示例#4
0
        public Production(Nonterminal leftPart, SymbolsChain rightPart)
        {
            LeftPart = leftPart;

            RightPart = rightPart;
        }
示例#5
0
        private static IEnumerable <SymbolsChain> RightSideChains(SymbolsChain chain, Nonterminal nonterminal)
        {
            var rightChains = new List <SymbolsChain>();

            SymbolsChain current = chain;

            do
            {
                current = FirstRightSideChain(current, nonterminal);

                if (current != null)
                {
                    rightChains.Add(current);
                }
                else
                {
                    break;
                }
            } while (true);

            return(rightChains);
        }