Пример #1
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());
            }
        }
        public object Clone()
        {
            SymbolList symbols = new SymbolList();

            foreach (var item in this)
            {
                symbols.Add(item.Copy());
            }
            return(symbols);
        }
Пример #3
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);
     }
 }
Пример #4
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);
                        }
                    }
                }
            }
        }
Пример #5
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);
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #6
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]);
                            }
                        }
                    }
                }
            }
        }
Пример #7
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);
                    }
                }
            }
        }