public SymbolList SkipFirst()
        {
            SymbolList s = new SymbolList();

            s.AddRange(this.Skip(1));
            return(s);
        }
Example #2
0
        public RuleProduction AddRule(int idSource, params Symbol[] destiny)
        {
            SymbolList sl = new SymbolList();

            sl.AddRange(destiny);
            return(AddRule(idSource, sl));
        }
Example #3
0
 public Grammar()
 {
     Variables      = new SymbolList();
     Terminals      = new SymbolList();
     VariablesEmpty = new SymbolList();
     Rules          = new List <RuleProduction>();
     VariableStart  = new Symbol(1, "S", false);
 }
        public object Clone()
        {
            SymbolList symbols = new SymbolList();

            foreach (var item in this)
            {
                symbols.Add(item.Copy());
            }
            return(symbols);
        }
Example #5
0
        public Symbol GetExclusiveVars(SymbolList destiny)
        {
            var origens = Rules.Where(c => c.Destiny == destiny).Select(r => r.Source);

            foreach (var origem in origens)
            {
                if (GetRules(origem.Name).Count == 1)
                {
                    return(origem);
                }
            }
            return(Symbol.EmptyVariable);
        }
Example #6
0
 private void GenerateNewVars()
 {
     Normalized   = new Grammar();
     NewVariables = new SymbolList();
     foreach (var item in simplified.Terminals)
     {
         int    newId = GetNewId();
         Symbol s     = new Symbol(newId, string.Empty, false);
         NewVariables.Add(s);
         NewRoles.Add(Normalized.AddRule(newId, item));
         DicVariablesTerminals.Add(item, s);
     }
 }
        public SymbolList Replace(Symbol sym1, Symbol sym2)
        {
            SymbolList returns = new SymbolList();

            returns.AddRange(this.Select(s => s.Copy()));

            for (int i = 0; i < returns.Count; i++)
            {
                if (returns[i] == sym1)
                {
                    returns[i] = sym2.Copy();
                }
            }
            return(returns);
        }
        public SymbolList RemoveFirst(Symbol sym1)
        {
            SymbolList returns = new SymbolList();

            returns.AddRange(this.Select(s => s.Copy()));

            for (int i = 0; i < returns.Count; i++)
            {
                if (returns[i] == sym1)
                {
                    returns.RemoveAt(i);
                    return(returns);
                }
            }
            return(returns);
        }
Example #9
0
        private void FillNewsNames()
        {
            NewNames = new Dictionary <Symbol, Symbol>();

            SymbolList visitados = new SymbolList();

            Queue <Symbol> variaveisAVisitar = new Queue <Symbol>();

            variaveisAVisitar.Enqueue(simplified.VariableStart);

            int i = 1;

            NewNames.Add(simplified.VariableStart, new Symbol(GetNewId(), string.Format("A{0:000}", i), false));
            visitados.Add(simplified.VariableStart);
            while (variaveisAVisitar.Count > 0)
            {
                var variavelAtual = variaveisAVisitar.Dequeue();

                List <RuleProduction> regrasAtuais = simplified.GetRules(variavelAtual);

                foreach (var item in regrasAtuais)
                {
                    foreach (var itemDestino in item.Destiny)
                    {
                        if (!itemDestino.Terminal && !visitados.Contains(itemDestino))
                        {
                            variaveisAVisitar.Enqueue(itemDestino);
                            i++;
                            NewNames.Add(itemDestino, new Symbol(GetNewId(), string.Format("A{0:000}", i), false));
                            visitados.Add(itemDestino);
                        }
                    }
                }
            }

            Normalized = new Grammar();
            Normalized.VariableStart = simplified.VariableStart;
            Normalized.Variables.AddRange(simplified.Variables);
            Normalized.Terminals.AddRange(simplified.Terminals);
            Normalized.VariablesEmpty = simplified.VariablesEmpty;
            for (i = 0; i < simplified.Rules.Count; i++)
            {
                Normalized.AddRule(simplified.Rules[i].Source, simplified.Rules[i].Destiny.Copy());
            }
        }
Example #10
0
        public RuleProduction AddRule(Symbol source, SymbolList destiny)
        {
            if (Rules.Count == 0)
            {
                VariableStart = source;
            }
            RuleProduction rule = Rules.FirstOrDefault(r => r.Destiny == destiny && r.Source == source && r.Pertinence == 1);

            if (rule == null)
            {
                rule        = new RuleProduction();
                rule.Source = source;
                rule.Destiny.AddRange(destiny.ToList());
                rule.Pertinence = 1;
                Rules.Add(rule);
            }
            return(rule);
        }
Example #11
0
        private void FillVariablesAcessibleTerminals()
        {
            VariablesCallTerminals = new SymbolList();

            for (int i = 0; i < GrammarNoUnitarianProductions.Variables.Count; i++)
            {
                Visited = new SymbolList();

                Queue <Symbol> varsToVisit = new Queue <Symbol>();

                varsToVisit.Enqueue(GrammarNoUnitarianProductions.Variables[i]);

                while (varsToVisit.Count > 0)
                {
                    Symbol variavelAtual = varsToVisit.Dequeue();
                    Visited.Add(variavelAtual);

                    List <RuleProduction> regrasAtuais = GrammarNoUnitarianProductions.Rules.Where(r => r.Source == variavelAtual).ToList();

                    foreach (var item in regrasAtuais)
                    {
                        if (item.DestinyContainsTerminal())
                        {
                            VariablesCallTerminals.Add(Source.Variables[i]);
                            varsToVisit.Clear();
                            break;
                        }
                        else
                        {
                            foreach (var itemDestino in item.Destiny)
                            {
                                if (!Visited.Contains(itemDestino))
                                {
                                    varsToVisit.Enqueue(itemDestino);
                                }
                            }
                        }
                    }
                }
            }
        }
Example #12
0
        public void FillTableEmpty()
        {
            VariablesEmpty = new SymbolList();

            for (int i = 0; i < Source.Variables.Count; i++)
            {
                Visited = new SymbolList();

                Queue <Symbol> variaveisAVisitar = new Queue <Symbol>();

                variaveisAVisitar.Enqueue(Source.Variables[i]);

                while (variaveisAVisitar.Count > 0)
                {
                    Symbol variavelAtual = variaveisAVisitar.Dequeue();

                    Visited.Add(variavelAtual);
                    List <RuleProduction> regrasAtuais = Source.Rules.Where(r => r.Source == variavelAtual).ToList();

                    foreach (var item in regrasAtuais)
                    {
                        if (item.Destiny.Count == 1)
                        {
                            if (item.Destiny[0] == Symbol.EmptyTerminal)
                            {
                                VariablesEmpty.Add(Source.Variables[i]);
                                variaveisAVisitar.Clear();
                                break;
                            }

                            if (!item.Destiny[0].Terminal && !Visited.Contains(item.Destiny[0]))
                            {
                                variaveisAVisitar.Enqueue(item.Destiny[0]);
                            }
                        }
                    }
                }
            }
        }
Example #13
0
        private void FillAcessibles(Grammar gramatica)
        {
            AcessiblesVariables = new SymbolList();
            AcessiblesTerminals = new SymbolList();

            Visited = new SymbolList();

            Queue <Symbol> variaveisAVisitar = new Queue <Symbol>();

            variaveisAVisitar.Enqueue(gramatica.VariableStart);
            AcessiblesVariables.Add(gramatica.VariableStart);
            while (variaveisAVisitar.Count > 0)
            {
                var variavelAtual = variaveisAVisitar.Dequeue();
                Visited.Add(variavelAtual);

                List <RuleProduction> regrasAtuais = gramatica.Rules.Where(r => r.Source == variavelAtual).ToList();

                foreach (var item in regrasAtuais)
                {
                    foreach (var itemDestino in item.Destiny)
                    {
                        if (itemDestino.Terminal)
                        {
                            if (!AcessiblesTerminals.Contains(itemDestino))
                            {
                                AcessiblesTerminals.Add(itemDestino);
                            }
                        }
                        else if (!Visited.Contains(itemDestino) && !AcessiblesVariables.Contains(itemDestino))
                        {
                            variaveisAVisitar.Enqueue(itemDestino);
                            AcessiblesVariables.Add(itemDestino);
                        }
                    }
                }
            }
        }
Example #14
0
        private string[] SubstituiRegras(List <RuleProduction> regras)
        {
            List <string> retorno = new List <string>();

            foreach (var item in regras)
            {
                var novoNome = item.Source;
                if (NewNames.ContainsKey(item.Source))
                {
                    novoNome = NewNames[item.Source];
                }

                SymbolList destino = item.Destiny.Copy();

                foreach (var novos in NewNames)
                {
                    destino = destino.Replace(novos.Key, novos.Value);
                }

                retorno.Add(string.Format("{0} => {1}", novoNome, destino));
            }

            return(retorno.ToArray());
        }
 public RuleProduction(Symbol source, SymbolList destiny, double pertinence = 1)
 {
     Source          = source;
     Destiny         = destiny;
     this.Pertinence = pertinence;
 }
 public RuleProduction()
 {
     Destiny    = new SymbolList();
     Pertinence = 0;
 }
Example #17
0
        private void Normalize()
        {
            Normalized.VariableStart = simplified.VariableStart;
            Normalized.Terminals.AddRange(simplified.Terminals);
            Normalized.Variables.AddRange(simplified.Variables);

            for (int i = 0; i < simplified.Rules.Count; i++)
            {
                RuleProduction rgActual = simplified.Rules[i];

                var destiny = rgActual.Destiny;

                foreach (var item in DicVariablesTerminals)
                {
                    destiny = destiny.Replace(item.Key, item.Value);
                }

                if (destiny.Unitary)
                {
                    Normalized.AddRule(rgActual.Source, destiny);
                }
                else
                {
                    if (destiny.Count == 2 && !rgActual.DestinyContainsTerminal())
                    {
                        Normalized.AddRule(rgActual.Source, destiny);
                    }
                    else
                    {
                        List <SymbolList> destinys = new List <SymbolList>();

                        while (destiny.Count > 2)
                        {
                            destinys.Clear();
                            for (int k = 0; k < destiny.Count; k += 2)
                            {
                                if (k + 1 < destiny.Count)
                                {
                                    destinys.Add(new SymbolList(destiny[k], destiny[k + 1]));
                                }
                                else
                                {
                                    destinys.Add(new SymbolList(destiny[k]));
                                }
                            }

                            destiny = new SymbolList();
                            foreach (var des in destinys)
                            {
                                Symbol destinyVariable = des[0];
                                if (!des.Unitary)
                                {
                                    destinyVariable = Normalized.GetExclusiveVars(des);

                                    if (destinyVariable == Symbol.EmptyVariable)
                                    {
                                        destinyVariable = new Symbol(GetNewId(), string.Empty, false);
                                        Normalized.Variables.Add(destinyVariable);
                                        Normalized.AddRule(destinyVariable, des);
                                    }
                                }
                                destiny.Add(destinyVariable);
                            }
                        }
                        Normalized.AddRule(rgActual.Source, destiny);
                    }
                }
            }
        }
 public SymbolList AddRange(SymbolList s)
 {
     base.AddRange(s);
     return(this);
 }
Example #19
0
 public RuleProduction AddRule(int idSource, SymbolList destiny)
 {
     return(AddRule(Variables.Find(idSource), destiny));
 }