Exemplo n.º 1
0
        private void ParseNonterminals(List <string> nonterminalLines)
        {
            foreach (string line in nonterminalLines)
            {
                Nonterminal newNonterminal = new Nonterminal(line);

                if (startSymbol == null)
                {
                    startSymbol = newNonterminal.identifier;
                }

                bool alreadyExists = false;
                foreach (Nonterminal n in this.nonterminals)
                {
                    if (n.Equals(newNonterminal))
                    {
                        n.Combine(newNonterminal);
                        alreadyExists = true;
                        break;
                    }
                }

                if (!alreadyExists)
                {
                    this.nonterminals.Add(newNonterminal);
                }
            }
        }
Exemplo n.º 2
0
        public override bool Equals(Object Other)
        {
            if (Other is Nonterminal)
            {
                Nonterminal OtherNonterminal = (Nonterminal)Other;
                return(this.identifier.Equals(OtherNonterminal.identifier));
            }

            return(false);
        }
Exemplo n.º 3
0
        private HashSet <LR0Item> ComputeClosure(HashSet <LR0Item> S)
        {
            HashSet <LR0Item> S2         = new HashSet <LR0Item>(S);
            List <LR0Item>    toConsider = new List <LR0Item>(S);

            int i = 0;

            while (i < toConsider.Count)
            {
                LR0Item item = toConsider[i];
                i += 1;
                if (!item.DposAtEnd())
                {
                    string sym = item.Rhs[item.Dpos];

                    Nonterminal n = null;
                    foreach (Nonterminal nt in this.nonterminals)
                    {
                        if (nt.identifier == sym)
                        {
                            n = nt;
                            break;
                        }
                    }

                    if (n != null)
                    {
                        foreach (List <string> prod in n.productions)
                        {
                            LR0Item item2 = new LR0Item(sym, prod, 0);
                            if (!S2.Contains(item2))
                            {
                                S2.Add(item2);
                                toConsider.Add(item2);
                            }
                        }
                    }
                }
            }

            return(S2);
        }
Exemplo n.º 4
0
 public void Combine(Nonterminal OtherNonterminal)
 {
     this.productions.AddRange(OtherNonterminal.productions);
 }