public FixSyn(Node<Symbol, double> node, Grammar grammar)
 {
     this.Symbols = grammar.Symbols;
     this.Grammar = grammar;
     var rule = node.GetCustomOject<RuleProduction>("Rule");
     NodeResult = CreateNode(node);
     NodeResult.Name = rule.Source.Name;
     NodeResult = CreateTreeNode(NodeResult);
 }
 public NormalFormChomsky(Grammar simplified)
 {
     newsIds = 1000;
     DicVariablesTerminals = new Dictionary<Symbol, Symbol>();
     NewRoles = new List<RuleProduction>();
     this.simplified = simplified;
     GenerateNewVars();
     Normalize();
 }
 public CompilerFuzzy(List<RecognitionToken> recs,
         Grammar grammar,
         Dictionary<int, List<string>> namesVars,
         Dictionary<string, Func<Container, string>> dicCompileExample,
         NormAbstract norm,
         ConormAbstract conorm)
 {
     this.Norm = norm;
     this.Conorm = conorm;
     this.DicCompile = dicCompileExample;
     this.Grammar = grammar;
     this.NameVars = namesVars;
 }
 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 NormalFormGreibach(Grammar simplified)
 {
     Normalized = new Grammar();
     newsIds = 100;
     this.simplified = simplified;
     //2
     FillNewsNames();
     //3,4
     PreencherRegrasArParaArEArParaMaiorAr();
     //5
     ColocarTerminalInicio();
     //6
     RemoveTerminalsNoMeio();
 }
 public Simplification(Grammar source)
 {
     this.Source = source;
     Simplified = new Grammar();
     FillTableEmpty();
     EliminarProducoesVazio();
     FillClosures();
     EliminarProducoesUnitarias();
     FillVariablesAcessibleTerminals();
     FillAcessibles(GrammarNoUnitarianProductions);
     GenerateSimplified();
     if (VariablesEmpty.Contains(Simplified.VariableStart))
     {
         Simplified.AddRule(Simplified.VariableStart.Id, Symbol.EmptyTerminal);
     }
 }
        public CompilerXmlParser(string filename)
        {
            NamesVars = new Dictionary<int, List<string>>();
            RecsTokens = new List<RecognitionToken>();
            Grammar = new Grammar();

            using (FileStream file = File.OpenRead(filename))
            {
                XDocument xdoc = XDocument.Load(file, LoadOptions.None);

                var settings = xdoc.Element("Compiler").Element("Settings");
                NormAbstract norm;
                switch (settings.Element("Norm").Value)
                {
                    case "MIN":
                        norm = new MinNorm();
                        break;
                    default:
                        norm = new MultiplyNorm();
                        break;
                }
                ConormAbstract conorm;
                switch (settings.Element("Conorm").Value)
                {
                    case "SUM":
                        conorm = new SumMinusProductConorm();
                        break;
                    default:
                        conorm = new MaxConorm();
                        break;
                }

                var recTokens = xdoc.Element("Compiler").Element("RecTokens").Elements("RecToken");
                int idTokens = 1;
                foreach (var token in recTokens)
                {

                    int id = idTokens++;
                    if (token.Attributes("id").Any() && !string.IsNullOrWhiteSpace(token.Attribute("id").Value))
                    {
                        id = Convert.ToInt32(token.Attribute("id").Value);
                    }

                    string name = token.Attribute("name").Value;
                    string fregex = token.Attribute("fregex").Value;

                    string hexColor = string.Empty;
                    if (token.Attributes("color").Any())
                    {
                        hexColor = token.Attribute("color").Value;
                    }

                    RecsTokens.Add(new RecognitionToken(id, name, fregex, hexColor, norm, conorm));
                }

                int idSymbols = 100;
                var symbolsXml = xdoc.Element("Compiler").Element("Grammar").Element("Symbols").Elements("Symbol");
                foreach (var symbolXml in symbolsXml)
                {
                    int id = idSymbols++;
                    if (symbolXml.Attributes("id").Any() && !string.IsNullOrWhiteSpace(symbolXml.Attribute("id").Value))
                    {
                        id = Convert.ToInt32(symbolXml.Attribute("id").Value);
                    }

                    string name = symbolXml.Attribute("name").Value;
                    bool terminal = false;

                    if (symbolXml.Attributes("terminal").Any())
                    {
                        terminal = Convert.ToBoolean(symbolXml.Attribute("terminal").Value);
                    }

                    string tempTokenReference = string.Empty;
                    int recTokenId = 0;

                    if (symbolXml.Attributes("recTokenId").Any())
                    {
                        tempTokenReference = symbolXml.Attribute("recTokenId").Value;
                        if (!string.IsNullOrWhiteSpace(tempTokenReference))
                            recTokenId = Convert.ToInt32(tempTokenReference);
                    }

                    if (string.IsNullOrWhiteSpace(tempTokenReference) && symbolXml.Attributes("recTokenName").Any())
                    {
                        tempTokenReference = symbolXml.Attribute("recTokenName").Value;
                    }

                    string temp = symbolXml.Attribute("charValue").Value;
                    if (string.IsNullOrWhiteSpace(temp))
                    {
                        temp = "\0";
                    }
                    char value = temp[0];
                    Symbol symbol = new Symbol(id, name, terminal, value);

                    if (terminal)
                    {
                        if (recTokenId > 0)
                        {
                            symbol.SetCustomValue("RecToken", RecsTokens.First(rt => rt.Id == recTokenId));
                        }
                        else if (!string.IsNullOrWhiteSpace(tempTokenReference))
                        {
                            symbol.SetCustomValue("RecToken", RecsTokens.First(rt => rt.Name == tempTokenReference));
                        }

                        Grammar.Terminals.Add(symbol);
                    }
                    else
                    {
                        bool variableInitial = false;
                        if (symbolXml.Attribute("variableInitial") != null)
                            variableInitial = Convert.ToBoolean(symbolXml.Attribute("variableInitial").Value);

                        if (variableInitial)
                        {
                            Grammar.VariableStart = symbol;
                        }
                        Grammar.Variables.Add(symbol);
                    }
                }

                int idRules = 100;
                var rulesXml = xdoc.Element("Compiler").Element("Grammar").Element("Rules").Elements("Rule");

                foreach (var ruleXml in rulesXml)
                {

                    bool gotoEmpty = false;
                    string sourceName = ruleXml.Attribute("sourceName").Value;

                    if (sourceName.Contains("normalAnnotation"))
                    {

                    }

                    double pertinence = Convert.ToDouble(ruleXml.Attribute("pertinence").Value.Replace(",", "."), CultureInfo.InvariantCulture);

                    SymbolList destinys = new SymbolList();

                    var destinysXml = ruleXml.Element("Destinys").Elements("Destiny");

                    List<string> nameVars = new List<string>();
                    foreach (var destinyXml in destinysXml)
                    {
                        string name = destinyXml.Attribute("name").Value;

                        if (name != "Empty")
                        {

                            var symbol = Grammar.Symbols.FirstOrDefault(s => s.Name == name);

                            if (symbol == null)
                            {
                                symbol = new Symbol(idSymbols++, name, false, '\0');
                                Grammar.Variables.Add(symbol);
                            }

                            destinys.Add(symbol);
                        }
                        else
                        {
                            gotoEmpty = destinysXml.Count() == 1;

                            destinys.Add(Symbol.EmptySymbol);

                            if (!Grammar.Terminals.Contains(Symbol.EmptySymbol))
                            {
                                Grammar.Terminals.Add(Symbol.EmptySymbol);
                            }

                        }
                        string namevar = name;
                        if (destinyXml.Attribute("var") != null && !string.IsNullOrWhiteSpace(destinyXml.Attribute("var").Value))
                        {
                            namevar = destinyXml.Attribute("var").Value.Trim();
                        }
                        nameVars.Add(namevar);
                    }

                    var sourceSymbol = Grammar.Variables.FirstOrDefault(s => s.Name == sourceName);

                    if (sourceSymbol == null)
                    {
                        if (sourceSymbol == null)
                        {
                            sourceSymbol = new Symbol(idSymbols++, sourceName, false, '\0');
                        }
                        Grammar.Variables.Add(sourceSymbol);
                    }
                    sourceSymbol.GoToEmpty = gotoEmpty;
                    RuleProduction rule = new RuleProduction(sourceSymbol, destinys, pertinence);

                    rule.Description = ruleXml.Element("Description").Value;

                    foreach (var attr in ruleXml.Attributes())
                    {
                        if (attr.Name == "typeName")
                            rule.TypeName = attr.Value;
                        if (attr.Name == "id")
                        {
                            if (!string.IsNullOrWhiteSpace(attr.Value))
                            {
                                rule.Id = Convert.ToInt32(attr.Value);
                            }
                        }
                        if (attr.Name == "default")
                            rule.Default = Convert.ToBoolean(attr.Value);
                    }

                    if (rule.Id == 0)
                    {
                        rule.Id = idRules++;
                    }

                    string temp = ruleXml.Attribute("idRuleParent").Value;
                    int idRuleParent = 0;
                    if (!string.IsNullOrWhiteSpace(temp))
                        idRuleParent = Convert.ToInt32(temp);

                    if (idRuleParent > 0)
                    {
                        rule.Parent = Grammar.Rules.First(r => r.Id == idRuleParent);
                    }

                    //Adicionando Nome das variáveis
                    NamesVars.Add(rule.Id, nameVars);

                    Grammar.Rules.Add(rule);
                }

                this.Compiler = new CompilerFuzzy(RecsTokens, Grammar, NamesVars, null, norm, conorm);

                switch (settings.Element("Parser").Value)
                {
                    case "SyntacticAnalysisCYK":
                        this.Compiler.Syn = new SyntacticAnalysisCYK(Grammar);
                        break;
                    case "SyntacticAnalysisLR1":
                    default:
                        this.Compiler.Syn = new SyntacticAnalysisLR1(Grammar, norm, conorm);
                        break;
                }
                switch (settings.Element("Lexer").Value)
                {
                    case "FullLexicalAnalysis":
                        this.Compiler.Lex = new FullLexicalAnalysis(RecsTokens, norm, conorm);
                        break;
                    case "TokenizerLexicalAnalysis":
                    default:
                        this.Compiler.Lex = new TokenizerLexicalAnalysis(RecsTokens, norm, conorm);
                        break;
                }
            }
        }
        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());
            }
        }
        private CompilerFuzzy Compiler4()
        {
            //LR(1)
            //0. S
            //0 → S$
            //1. S → V = E
            //2. S → E
            //3. E → V
            //4. V → x
            //5. V → ∗E
            List<RecognitionToken> recs = new List<RecognitionToken>();
            var requal = new RecognitionToken(1, "equal", "=", "#7bf6b6", norm, conorm);
            var rast = new RecognitionToken(2, "ast", "*", "#f28686", norm, conorm);
            var rx = new RecognitionToken(3, "x", "x", "#fbdb65", norm, conorm);
            recs.Add(requal);
            recs.Add(rast);
            recs.Add(rx);

            Grammar grammar = new Grammar();
            var x = new Symbol(1, "x", true, 'x');
            x.SetCustomValue("RecToken", rx);

            var ast = new Symbol(2, "*", true, '*');
            ast.SetCustomValue("RecToken", rast);

            var equal = new Symbol(3, "=", true, '=');
            equal.SetCustomValue("RecToken", requal);

            var S = new Symbol(4, "S", false);
            var V = new Symbol(5, "V", false);
            var V1 = new Symbol(7, "V1", false);
            var E = new Symbol(6, "E", false);

            grammar.Terminals.AddRange(new SymbolList(x, ast, equal));

            grammar.Variables.AddRange(new SymbolList(S, V, E, V1));
            grammar.VariableStart = S;
            grammar.AddRule(S, V, equal, E);
            grammar.AddRule(S, E);
            grammar.AddRule(E, V);
            grammar.AddRule(E, V1).Pertinence = 0.8;
            var ruleV = grammar.AddRule(V, x);
            grammar.AddRule(V1, equal).Parent = ruleV;
            grammar.AddRule(V, ast, E);

            return new CompilerFuzzy(recs, grammar, null, null, norm, conorm);
        }
        private Grammar grammar3()
        {
            Grammar grammar = new Grammar();
            //var a = new Symbol(1, "a", true, 'a') { CustomValue = new Token(1, 0, 0, null, 'a') };
            //var left = new Symbol(2, "(", true, '(') { CustomValue = new Token(2, 0, 0, null, '(') };
            //var right = new Symbol(3, ")", true, ')') { CustomValue = new Token(3, 0, 0, null, ')') };
            //var A = new Symbol(4, "A", false);

            //grammar.Terminals.AddRange(new SymbolList(left));
            //grammar.Terminals.AddRange(new SymbolList(a));
            //grammar.Terminals.AddRange(new SymbolList(right));

            //grammar.Variables.AddRange(new SymbolList(A));
            //grammar.VariableStart = A;
            //grammar.AddRole(A, left, A, right);
            //grammar.AddRole(A, a);
            return grammar;
        }
        private Grammar grammar1()
        {
            Grammar grammar = new Grammar();
            //var sc = new Symbol(3, "c", true, 'c') { CustomValue = new Token(1, 0, 0, null, 'c') };
            //var sd = new Symbol(4, "d", true, 'd') { CustomValue = new Token(2, 0, 0, null, 'd') };
            //grammar.Terminals.AddRange(new SymbolList(sc, sd));
            //var sS = new Symbol(1, "S", false);
            //var sC = new Symbol(2, "C", false);
            //grammar.Variables.AddRange(new SymbolList(sS, sC));
            //grammar.VariableStart = grammar.Variables[0];
            //grammar.AddRole(sS, sC, sC);
            //grammar.Rules[0].Pertinence = 1;

            //grammar.AddRole(sC, sc, sC);
            //grammar.Rules[1].Pertinence = 1;

            //grammar.AddRole(sC, sd);
            //grammar.Rules[2].Pertinence = 1;
            return grammar;
        }
 public SyntacticAnalysisAbstract(Grammar grammar, NormAbstract norm, ConormAbstract conorm)
 {
     this.Norm = norm;
     this.Conorm = conorm;
     this.Grammar = grammar;
 }
        public void EliminarProducoesVazio()
        {
            GrammarNoEmpty = new Grammar();

            GrammarNoEmpty.Terminals.AddRange(Source.Terminals);
            GrammarNoEmpty.Variables.AddRange(Source.Variables);
            for (int i = 0; i < Source.Rules.Count; i++)
            {
                RuleProduction regraAtual = Source.Rules[i];
                Symbol orig = regraAtual.Source;

                List<SymbolList> destinos = new List<SymbolList>();

                if (Source.Rules[i].Destiny.Count == 1 && Source.Rules[i].Destiny[0] == Symbol.EmptyTerminal)
                {
                    //Do Nothing
                }
                else
                {

                    if (regraAtual.Destiny.Count > 0)
                    {
                        destinos.Add(regraAtual.Destiny);
                        for (int j = 0; j < destinos.Count; j++)
                        {
                            for (int k = 0; k < VariablesEmpty.Count; k++)
                            {
                                if (destinos[j].Contains(VariablesEmpty[k]))
                                {
                                    var destino = regraAtual.Destiny;

                                    while (destino.Contains(VariablesEmpty[k]))
                                    {
                                        destino = destino.RemoveFirst(VariablesEmpty[k]);

                                        if (destinos.FirstOrDefault(list => list == destino) == null
                                                && !destino.IsEmpty())
                                        {
                                            destinos.Add(destino);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    for (int j = 0; j < destinos.Count; j++)
                    {
                        var destino = destinos[j].Copy();
                        GrammarNoEmpty.AddRule(orig, destino);
                    }
                }
            }
        }
        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);
            }
        }
        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);
                        }

                    }
                }
            }
        }
        private void EliminarProducoesUnitarias()
        {
            GrammarNoUnitarianProductions = new Grammar();
            GrammarNoUnitarianProductions.Terminals.AddRange(GrammarNoEmpty.Terminals);
            GrammarNoUnitarianProductions.Variables.AddRange(GrammarNoEmpty.Variables);
            for (int i = 0; i < GrammarNoEmpty.Rules.Count; i++)
            {
                RuleProduction rgAtual = GrammarNoEmpty.Rules[i];
                GrammarNoUnitarianProductions.AddRule(rgAtual.Source, rgAtual.Destiny.Copy());
            }
            bool trocou = true;
            while (trocou)
            {
                trocou = false;
                for (int i = GrammarNoUnitarianProductions.Rules.Count - 1; i >= 0; i--)
                {
                    RuleProduction rgAtual = GrammarNoUnitarianProductions.Rules[i];

                    if (rgAtual.IsUnityVariable())
                    {
                        var regras = GrammarNoUnitarianProductions.GetRules(rgAtual.FirstDestiny().Name);
                        foreach (var item in regras)
                        {
                            GrammarNoUnitarianProductions.AddRule(rgAtual.Source, item.Destiny.Copy());
                        }
                        trocou = true;
                        GrammarNoUnitarianProductions.Rules.Remove(rgAtual);
                    }
                }
            }
        }