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

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

            foreach (var rule in t.Rules)
            {
                int currentPosition = position;
                success [i] = true;
                foreach (var sym in rule.Parts)
                {
                    if (sym.isTerminal == false)
                    {
                        if (RecusiveDescent(sym as BnfNonterminal) == false)
                        {
                            success [i] = false;
                            break;
                        }
                    }
                    else
                    {
                        int len = sym.Content.Length;
                        if (string.Compare(sym.Content, text.Substring(position, len)) == 0)
                        {
                            position += len;
                            continue;
                        }
                        else
                        {
                            success [i] = false;
                            break;
                        }
                    }
                }
                if (success [i] == false)
                {
                    position = currentPosition;
                }
                i++;
            }
            int total = 0;

            foreach (var b in success)
            {
                if (b)
                {
                    total++;
                }
            }
            if (total > 1)
            {
                throw new ApplicationException();
            }
            return(total == 1);
        }
Beispiel #2
0
        public static BnfGrammar Test1()
        {
            BnfNonterminal S     = new BnfNonterminal(nameof(S));
            BnfNonterminal A     = new BnfNonterminal(nameof(A));
            Symbol         a     = new Symbol(nameof(a));
            Symbol         b     = new Symbol(nameof(b));
            Symbol         c     = new Symbol(nameof(c));
            Symbol         d     = new Symbol(nameof(d));
            var            rule1 = new BnfRule(S, new Symbol [] { c, A, d });
            var            rule2 = new BnfRule(A, new Symbol [] { a, b });
            var            rule3 = new BnfRule(A, new Symbol [] { a });
            var            G     = new BnfGrammar();

            G.Add(rule1);
            G.Add(rule2);
            G.Add(rule3);
            return(G);
        }
 public BnfRule(BnfNonterminal nt, Symbol [] symbols_of_rule)
 {
     nonterminal = nt;
     parts       = symbols_of_rule;
 }