Пример #1
0
        private void GenerateSimplified()
        {
            Simplified = new Grammar();

            Simplified.Terminals.AddRange(AcessiblesTerminals);
            Simplified.Variables.AddRange(AcessiblesVariables);
            Simplified.VariableStart  = GrammarNoUnitarianProductions.VariableStart;
            Simplified.VariablesEmpty = VariablesEmpty;

            bool change = true;

            Simplified.Rules.AddRange(GrammarNoUnitarianProductions.Rules.Select(r => new RuleProduction()
            {
                Destiny = r.Destiny, Source = r.Source
            }));

            while (change)
            {
                change = false;
                if (Simplified.Terminals.RemoveAll(terminal => !AcessiblesTerminals.Contains(terminal)) > 0)
                {
                    change = true;
                }

                if (Simplified.Variables.RemoveAll(variavel => !AcessiblesVariables.Contains(variavel)) > 0)
                {
                    change = true;
                }

                for (int i = Simplified.Rules.Count - 1; i >= 0; i--)
                {
                    RuleProduction rAtual = Simplified.Rules[i];
                    if (!AcessiblesVariables.Contains(rAtual.Source) ||
                        rAtual.Destiny.Exists
                            (simbolo =>
                            (
                                (simbolo.Terminal && !AcessiblesTerminals.Contains(simbolo)) ||
                                (!simbolo.Terminal && !AcessiblesVariables.Contains(simbolo))

                            )
                            )
                        )
                    {
                        change = true;
                        Simplified.Rules.Remove(rAtual);
                    }
                }

                FillAcessibles(Simplified);
            }
        }
Пример #2
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());
            }
        }
Пример #3
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);
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #4
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]);
                            }
                        }
                    }
                }
            }
        }
Пример #5
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);
                        }
                    }
                }
            }
        }