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 BnfNonterminal AddNonterminal(string name)
        {
            if (NonTerminals.ContainsKey(name))
            {
                return(NonTerminals [name]);
            }
            var nt = new BnfNonterminal(name);

            NonTerminals.Add(name, nt);
            return(nt);
        }
Ejemplo n.º 3
0
        BnfNonterminal Convert(EbnfNonterminal E)
        {
            BnfNonterminal B = null;

            if (nonterminalConversions.ContainsKey(E))
            {
                var footprint = nonterminalConversions [E];
                B = footprint.After;
            }
            else
            {
                B = Bnf.AddNonterminal(E.Name);
                var footprint = new NonterminalConversionFootprint(E, B);
                nonterminalConversions.Add(E, footprint);
            }
            return(B);
        }
Ejemplo n.º 4
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.º 5
0
        BnfRule Convert(EbnfRule E)
        {
            BnfRule B = null;

            if (ruleConversions.ContainsKey(E))
            {
                var footprint = ruleConversions [E];
                B = footprint.After;
            }
            else
            {
                BnfNonterminal bn = Convert(E.Nonterminal);

                List <ISymbol> terms = new List <ISymbol> ();
                var            p     = E.RightPart;
                if (p is EbnfEnumeration)
                {
                    foreach (var pp in ((EbnfEnumeration)p).Parts)
                    {
                        if (pp is EbnfNonterminal)
                        {
                            var name = (pp as EbnfNonterminal).Name;
                            var node = Bnf.AddNonterminal(name);
                            terms.Add(node);
                        }
                        else
                        {
                            if (pp is EbnfTerminal)
                            {
                                var node = (TerminalSymbol)pp;
                                terms.Add(node);
                            }
                            else
                            {
                                throw new ApplicationException();
                            }
                        }
                    }
                }
                else
                {
                    if (p is EbnfNonterminal)
                    {
                        var name = (p as EbnfNonterminal).Name;
                        var node = Bnf.AddNonterminal(name);
                        terms.Add(node);
                    }
                    else
                    {
                        if (p is EbnfTerminal)
                        {
                            var node = (TerminalSymbol)p;
                            terms.Add(node);
                        }
                        else
                        {
                            throw new ApplicationException();
                        }
                    }
                }

                B = new BnfRule(bn, terms.ToArray());
                bn.Add(B);
                var footprint = new RuleConversionFootprint(E, B);
                ruleConversions.Add(E, footprint);
            }
            return(B);
        }
Ejemplo n.º 6
0
 public BnfRule(BnfNonterminal nt, ISymbol [] symbols_of_rule)
 {
     nonterminal = nt;
     parts       = symbols_of_rule;
 }