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);
    }
    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);
            ops.Add(footprint);
        }
        return(B);
    }
Ejemplo n.º 4
0
 public BnfRule(BnfNonterminal nt, ISymbol [] symbols_of_rule)
 {
     nonterminal = nt;
     parts       = symbols_of_rule;
 }
    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);
            ops.Add(footprint);
        }
        return(B);
    }