public CustomValueStateNodeLR1(CustomValueStateNodeLR1 old)
        {
            Productions = new List <RuleProduction>();
            Tokens      = new List <Symbol>();

            this.IToken    = old.IToken;
            this.CoutPaths = old.CoutPaths;
            this.Productions.AddRange(old.Productions);
            this.Tokens.AddRange(old.Tokens);
            this.ASymbolReal = old.ASymbolReal;
            this.ASymbol     = old.ASymbol;
        }
        public CustomValueStateNodeLR1(CustomValueStateNodeLR1 old)
        {
            Productions = new List<RuleProduction>();
            Tokens = new List<Symbol>();

            this.IToken = old.IToken;
            this.CoutPaths = old.CoutPaths;
            this.Productions.AddRange(old.Productions);
            this.Tokens.AddRange(old.Tokens);
            this.ASymbolReal = old.ASymbolReal;
            this.ASymbol = old.ASymbol;
        }
        public override double Validate(List<Symbol> tokens)
        {
            CacheFirst = null;
            try
            {
                GraphsSyntactic = new List<Graph<Symbol, double>>();
                tokens.Add(Symbol.TapeFinal);
                Productions = new List<RuleProduction>();
                Graph<State<Symbol>, double> graph = new Graph<State<Symbol>, double>();
                FirstState.PertinenceInitial = 1;
                var firstNode = graph.AddNode("First", FirstState);
                firstNode.Parent = null;
                List<Node<State<Symbol>, double>> leafs = new List<Node<State<Symbol>, double>>();

                int idNode = 1;
                List<Node<State<Symbol>, double>> newLeafs = new List<Node<State<Symbol>, double>>();

                CustomValueStateNodeLR1 customValueStateNodeLR1 = new CustomValueStateNodeLR1();
                customValueStateNodeLR1.CoutPaths = 1;
                //customValueStateNodeLR1.Tokens.Add(tokens[0]);
                customValueStateNodeLR1.IToken = 1;
                customValueStateNodeLR1.ASymbol = tokens[0];
                customValueStateNodeLR1.ASymbolReal = tokens[0];

                firstNode.SetCustomOject("CustomValueStateNodeLR1", customValueStateNodeLR1);
                leafs.Add(firstNode);

                double maxAcept = 0;
                while (true)
                {
                    for (int iLeaf = 0; iLeaf < leafs.Count; iLeaf++)
                    {

                        var sLeaf = leafs[iLeaf];
                        customValueStateNodeLR1 = sLeaf.GetCustomOject<CustomValueStateNodeLR1>("CustomValueStateNodeLR1");

                        #region GETOP
                        double pertinenceS = sLeaf.Info.PertinenceInitial;
                        double newPertinence = 1;

                        var operations = Table[sLeaf.Info][customValueStateNodeLR1.ASymbol];
                        var emptyrule = false;

                        if (operations.Count == 0)
                        {
                            if (Table[sLeaf.Info].ContainsKey(Symbol.EmptySymbol))
                            {
                                operations = Table[sLeaf.Info][Symbol.EmptySymbol];
                                emptyrule = true;
                            }
                        }

                        if (operations.Count == 0)
                        {
                            operations = Table[sLeaf.Info][Symbol.TapeFinal];
                            emptyrule = true;
                        }

                        #endregion GETOP

                        if (operations.Count > 0)

                            for (int iOp = 0; iOp < operations.Count; iOp++)
                            {

                                customValueStateNodeLR1 = sLeaf.GetCustomOject<CustomValueStateNodeLR1>("CustomValueStateNodeLR1");

                                Operation op = operations[iOp];

                                #region FOREACH
                                if (op.Type == TypeOperation.Shift)
                                {
                                    #region SHIFT
                                    newPertinence = Norm.Calculate(pertinenceS, op.Pertinence);
                                    var newLeaf = graph.AddNode(idNode++, op.State, sLeaf, newPertinence);
                                    CustomValueStateNodeLR1 newCustomValueStateNodeLR1 = new CustomValueStateNodeLR1(customValueStateNodeLR1);
                                    newCustomValueStateNodeLR1.IndexInParent = iOp;

                                    List<Symbol> sNewTokens = new List<Symbol>();
                                    if (!emptyrule)
                                    {
                                        if (newCustomValueStateNodeLR1.IToken >= tokens.Count)
                                        {
                                            newCustomValueStateNodeLR1.ASymbol = Symbol.TapeFinal;
                                        }
                                        else
                                        {
                                            newCustomValueStateNodeLR1.ASymbol = tokens[newCustomValueStateNodeLR1.IToken];
                                        }
                                        newCustomValueStateNodeLR1.IToken++;
                                    }
                                    newLeaf.Info.PertinenceInitial = newPertinence;
                                    newLeaf.SetCustomOject("CustomValueStateNodeLR1", newCustomValueStateNodeLR1);
                                    newLeafs.Add(newLeaf);
                                    #endregion SHIFT
                                }
                                else if (op.Type == TypeOperation.Reduce)
                                {
                                    #region REDEUCE 1
                                    RuleProduction rule = op.Rule;
                                    var prox = sLeaf;
                                    int popStack = op.Rule.Destiny.Count;
                                    int countNodes = rule.Destiny.Count;
                                    for (int i = 0; i < popStack; i++)
                                    {
                                        newPertinence = prox.Info.PertinenceInitial;
                                        var temp = prox;
                                        //graph.RemoveNode(prox);
                                        prox = prox.Parent;
                                    }
                                    #endregion REDEUCE 1
                                    #region GOTO
                                    var operationsGoto = Table[prox.Info][rule.Source];
                                    if (operationsGoto.Count > 0)
                                    {
                                        var state = operationsGoto[0].State;

                                        newPertinence = Norm.Calculate(prox.Info.PertinenceInitial, op.Rule.Pertinence);

                                        var newLeaf = graph.AddNode(idNode++, state, prox, newPertinence);
                                        CustomValueStateNodeLR1 newCustomValueStateNodeLR1 = new CustomValueStateNodeLR1(customValueStateNodeLR1);
                                        newCustomValueStateNodeLR1.IndexInParent = iOp;
                                        newCustomValueStateNodeLR1.Productions.Add(rule);

                                        newLeaf.SetCustomOject("CustomValueStateNodeLR1", newCustomValueStateNodeLR1);

                                        newCustomValueStateNodeLR1.Pertinence = newPertinence;

                                        newLeaf.Info.PertinenceInitial = newPertinence;

                                        newLeafs.Add(newLeaf);
                                    }
                                    #endregion GOTO
                                }
                                else if (op.Type == TypeOperation.Acept)
                                {
                                    #region Acept

                                    Graph<Symbol, double> graphA =
                                        CreateTreeSyntax(customValueStateNodeLR1.Productions,
                                           tokens);

                                    GraphsSyntactic.Add(graphA);

                                    if (sLeaf.Info.PertinenceInitial > maxAcept)
                                    {
                                        Productions = customValueStateNodeLR1.Productions;
                                        maxAcept = sLeaf.Info.PertinenceInitial;
                                    }
                                    #endregion Acept
                                }
                                else
                                {
                                    //TODO: Recuperar de errros
                                    return 0;
                                }

                                #endregion FOREACH
                            }
                    }

                    leafs = newLeafs;

                    if (leafs.Count == 0)
                        return maxAcept;

                    newLeafs = new List<Node<State<Symbol>, double>>();
                }

            }
            catch (Exception ex)
            {

            }
            return 0;
        }
        public override double Validate(List <Symbol> tokens)
        {
            CacheFirst = null;
            try
            {
                GraphsSyntactic = new List <Graph <Symbol, double> >();
                tokens.Add(Symbol.TapeFinal);
                Productions = new List <RuleProduction>();
                Graph <State <Symbol>, double> graph = new Graph <State <Symbol>, double>();
                FirstState.PertinenceInitial = 1;
                var firstNode = graph.AddNode("First", FirstState);
                firstNode.Parent = null;
                List <Node <State <Symbol>, double> > leafs = new List <Node <State <Symbol>, double> >();

                int idNode = 1;
                List <Node <State <Symbol>, double> > newLeafs = new List <Node <State <Symbol>, double> >();

                CustomValueStateNodeLR1 customValueStateNodeLR1 = new CustomValueStateNodeLR1();
                customValueStateNodeLR1.CoutPaths = 1;
                //customValueStateNodeLR1.Tokens.Add(tokens[0]);
                customValueStateNodeLR1.IToken      = 1;
                customValueStateNodeLR1.ASymbol     = tokens[0];
                customValueStateNodeLR1.ASymbolReal = tokens[0];

                firstNode.SetCustomOject("CustomValueStateNodeLR1", customValueStateNodeLR1);
                leafs.Add(firstNode);

                double maxAcept = 0;
                while (true)
                {
                    for (int iLeaf = 0; iLeaf < leafs.Count; iLeaf++)
                    {
                        var sLeaf = leafs[iLeaf];
                        customValueStateNodeLR1 = sLeaf.GetCustomOject <CustomValueStateNodeLR1>("CustomValueStateNodeLR1");

                        #region GETOP
                        double pertinenceS   = sLeaf.Info.PertinenceInitial;
                        double newPertinence = 1;

                        var operations = Table[sLeaf.Info][customValueStateNodeLR1.ASymbol];
                        var emptyrule  = false;

                        if (operations.Count == 0)
                        {
                            if (Table[sLeaf.Info].ContainsKey(Symbol.EmptySymbol))
                            {
                                operations = Table[sLeaf.Info][Symbol.EmptySymbol];
                                emptyrule  = true;
                            }
                        }

                        if (operations.Count == 0)
                        {
                            operations = Table[sLeaf.Info][Symbol.TapeFinal];
                            emptyrule  = true;
                        }

                        #endregion GETOP

                        if (operations.Count > 0)
                        {
                            for (int iOp = 0; iOp < operations.Count; iOp++)
                            {
                                customValueStateNodeLR1 = sLeaf.GetCustomOject <CustomValueStateNodeLR1>("CustomValueStateNodeLR1");

                                Operation op = operations[iOp];

                                #region FOREACH
                                if (op.Type == TypeOperation.Shift)
                                {
                                    #region SHIFT
                                    newPertinence = Norm.Calculate(pertinenceS, op.Pertinence);
                                    var newLeaf = graph.AddNode(idNode++, op.State, sLeaf, newPertinence);
                                    CustomValueStateNodeLR1 newCustomValueStateNodeLR1 = new CustomValueStateNodeLR1(customValueStateNodeLR1);
                                    newCustomValueStateNodeLR1.IndexInParent = iOp;

                                    List <Symbol> sNewTokens = new List <Symbol>();
                                    if (!emptyrule)
                                    {
                                        if (newCustomValueStateNodeLR1.IToken >= tokens.Count)
                                        {
                                            newCustomValueStateNodeLR1.ASymbol = Symbol.TapeFinal;
                                        }
                                        else
                                        {
                                            newCustomValueStateNodeLR1.ASymbol = tokens[newCustomValueStateNodeLR1.IToken];
                                        }
                                        newCustomValueStateNodeLR1.IToken++;
                                    }
                                    newLeaf.Info.PertinenceInitial = newPertinence;
                                    newLeaf.SetCustomOject("CustomValueStateNodeLR1", newCustomValueStateNodeLR1);
                                    newLeafs.Add(newLeaf);
                                    #endregion SHIFT
                                }
                                else if (op.Type == TypeOperation.Reduce)
                                {
                                    #region REDEUCE 1
                                    RuleProduction rule       = op.Rule;
                                    var            prox       = sLeaf;
                                    int            popStack   = op.Rule.Destiny.Count;
                                    int            countNodes = rule.Destiny.Count;
                                    for (int i = 0; i < popStack; i++)
                                    {
                                        newPertinence = prox.Info.PertinenceInitial;
                                        var temp = prox;
                                        //graph.RemoveNode(prox);
                                        prox = prox.Parent;
                                    }
                                    #endregion REDEUCE 1
                                    #region GOTO
                                    var operationsGoto = Table[prox.Info][rule.Source];
                                    if (operationsGoto.Count > 0)
                                    {
                                        var state = operationsGoto[0].State;

                                        newPertinence = Norm.Calculate(prox.Info.PertinenceInitial, op.Rule.Pertinence);

                                        var newLeaf = graph.AddNode(idNode++, state, prox, newPertinence);
                                        CustomValueStateNodeLR1 newCustomValueStateNodeLR1 = new CustomValueStateNodeLR1(customValueStateNodeLR1);
                                        newCustomValueStateNodeLR1.IndexInParent = iOp;
                                        newCustomValueStateNodeLR1.Productions.Add(rule);

                                        newLeaf.SetCustomOject("CustomValueStateNodeLR1", newCustomValueStateNodeLR1);

                                        newCustomValueStateNodeLR1.Pertinence = newPertinence;

                                        newLeaf.Info.PertinenceInitial = newPertinence;

                                        newLeafs.Add(newLeaf);
                                    }
                                    #endregion GOTO
                                }
                                else if (op.Type == TypeOperation.Acept)
                                {
                                    #region Acept

                                    Graph <Symbol, double> graphA =
                                        CreateTreeSyntax(customValueStateNodeLR1.Productions,
                                                         tokens);

                                    GraphsSyntactic.Add(graphA);

                                    if (sLeaf.Info.PertinenceInitial > maxAcept)
                                    {
                                        Productions = customValueStateNodeLR1.Productions;
                                        maxAcept    = sLeaf.Info.PertinenceInitial;
                                    }
                                    #endregion Acept
                                }
                                else
                                {
                                    //TODO: Recuperar de errros
                                    return(0);
                                }

                                #endregion FOREACH
                            }
                        }
                    }

                    leafs = newLeafs;

                    if (leafs.Count == 0)
                    {
                        return(maxAcept);
                    }

                    newLeafs = new List <Node <State <Symbol>, double> >();
                }
            }
            catch (Exception ex)
            {
            }
            return(0);
        }