public void Add(EbnfRule rule)
 {
     if (rules.ContainsKey(rule) == false)
     {
         rules.Add(rule, rule);
     }
 }
示例#2
0
        public void Add(EbnfRule rule)
        {
            rules.Add(rule);
            var nt = rule.Nonterminal;

            if (NonTerminals.ContainsKey(nt.Name) == false)
            {
                NonTerminals.Add(nt.Name, nt);
            }
            nt.Add(rule);
        }
示例#3
0
        public static EbnfGrammar Test2()
        {
            EbnfNonterminal S = new EbnfNonterminal(nameof(S));
            EbnfNonterminal A = new EbnfNonterminal(nameof(A));

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

            var rule1 = new EbnfRule(S, new EbnfEnumeration(new IEbnfEnumerationPart [] { c, A, d }));
            var rule2 = new EbnfRule(A, new EbnfEnumeration(new IEbnfEnumerationPart [] { a, b }));
            var rule3 = new EbnfRule(A, new EbnfEnumeration(new IEbnfEnumerationPart [] { a }));
            var G     = new EbnfGrammar();

            G.Add(rule1);
            G.Add(rule2);
            G.Add(rule3);
            return(G);
        }
示例#4
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);
        }