public bool RecusiveDescent(BnfNonterminal t)
        {
            int rulesCount = t.RulesCount;

            bool [] success = new bool [rulesCount];
            int     i       = 0;

            foreach (var rule in t.Rules)
            {
                var currentPosition = r.Position;
                success [i] = true;
                foreach (var sym in rule.Parts)
                {
                    if (sym is NonterminalSymbol)
                    {
                        if (RecusiveDescent(sym as BnfNonterminal) == false)
                        {
                            success [i] = false;
                            break;
                        }
                    }
                    else
                    {
                        var inputSymbol = r.GetSymbol();
                        if (TerminalSymbol.Compare(sym as TerminalSymbol, inputSymbol) == 0)
                        {
                            continue;
                        }
                        else
                        {
                            success [i] = false;
                            break;
                        }
                    }
                }
                if (success [i] == false)
                {
                    r.Position = currentPosition;
                }
                i++;
            }
            int total = 0;

            foreach (var b in success)
            {
                if (b)
                {
                    total++;
                }
            }
            if (total > 1)
            {
                throw new ApplicationException();
            }
            return(total == 1);
        }
Ejemplo n.º 2
0
        public static BnfGrammar Test1()
        {
            BnfNonterminal S = new BnfNonterminal(nameof(S));
            BnfNonterminal A = new BnfNonterminal(nameof(A));

            TerminalSymbol a = new TerminalSymbol(nameof(a));
            TerminalSymbol b = new TerminalSymbol(nameof(b));
            TerminalSymbol c = new TerminalSymbol(nameof(c));
            TerminalSymbol d = new TerminalSymbol(nameof(d));

            var rule1 = new BnfRule(S, new ISymbol [] { c, A, d });
            var rule2 = new BnfRule(A, new ISymbol [] { a, b });
            var rule3 = new BnfRule(A, new ISymbol [] { a });
            var G     = new BnfGrammar();

            G.Add(rule1);
            G.Add(rule2);
            G.Add(rule3);
            return(G);
        }
Ejemplo n.º 3
0
 public static int Compare(TerminalSymbol sym, TerminalSymbol inputSymbol)
 {
     return(string.Compare(sym.c, inputSymbol.c));
 }