/// <summary>Checks the production of.</summary>
    /// <param name="x">The x.</param>
    /// <returns></returns>
    public bool CheckProductionOf(char x)
    {
        List <Production> productions = GrammarTools.GetProductionsOf((( Element )stack.Peek()).value, grammar);

        if (productions.Count == 0)
        {
            //GamEvent.instance.RejectRow
            return(false);
        }

        Production productioToCheck = GrammarTools.GetProductionWithValue(x, productions);

        if (productioToCheck is null)
        {
            //GamEvent.instance.RejectRow
            return(false);
        }

        else if (productioToCheck.rightSide.Count == 1)
        {
            stack.Pop();
        }

        else
        {
            stack.Pop();
            for (int i = productioToCheck.rightSide.Count - 1; i >= 1; i--)
            {
                stack.Push(productioToCheck.rightSide [i]);
            }
        }
        return(true);
    }
    /// <summary>Checks the productions righ side start no terminal.</summary>
    /// <param name="productions">The productions.</param>
    /// <param name="x">The x.</param>
    /// <returns></returns>
    public bool CheckProductionsRighSideStartNoTerminal(List <Production> productions, char x)
    {
        foreach (Production production in productions)
        {
            if (production.rightSide [0].kind.Equals(Element.Kind.noTerminal))
            {
                if (GrammarTools.GetSeleccionSetOfProduction(new List <char> (), production.leftSide.index, grammar).Contains(x))
                {
                    stack.Pop();

                    for (int i = production.rightSide.Count - 1; i >= 0; i--)
                    {
                        stack.Push(production.rightSide [i]);
                    }
                    return(Transition(x));
                }
                return(false);
            }
        }
        return(true);
    }
Exemplo n.º 3
0
    /// <summary>Determines whether [is disjoint productions] [the specified same productions].</summary>
    /// <param name="sameProductions">The same productions.</param>
    /// <param name="grammar">The grammar.</param>
    /// <returns>
    ///   <c>true</c> if [is disjoint productions] [the specified same productions]; otherwise, <c>false</c>.</returns>
    private static bool IsDisjointProductions(List <Production> sameProductions, GenericGrammar grammar)
    {
        for (int i = 0; i < sameProductions.Count; i++)
        {
            List <char> selectionSet = GrammarTools.GetSeleccionSetOfProduction(new List <char>(), sameProductions[i].leftSide.index, grammar);

            for (int j = 0; j < sameProductions.Count; j++)
            {
                if (i != j)
                {
                    List <char> selectionSetToCompare = GrammarTools.GetSeleccionSetOfProduction(new List <char>(), sameProductions[j].leftSide.index, grammar);

                    if (ListAreEqual(selectionSet, selectionSetToCompare))
                    {
                        return(false);
                    }
                }
            }
        }

        return(true);
    }
    /// <summary>Checks the selection set of peek.</summary>
    /// <param name="productions">The productions.</param>
    /// <param name="x">The x.</param>
    /// <returns></returns>
    public bool CheckSelectionSetOfPeek(List <Production> productions, char x)
    {
        foreach (Production production in productions)
        {
            if (production.rightSide [0].kind.Equals(Element.Kind.nulo))
            {
                if (GrammarTools.GetSeleccionSetOfProduction(new List <char> (), production.leftSide.index, grammar).Contains(x))
                {
                    stack.Pop();

                    return(Transition(x));
                }
                else
                {
                    //GamEvent.instance.RejectRow
                    return(false);
                }
            }
        }

        return(true);
    }
    /// <summary>Checks the production of.</summary>
    /// <param name="x">The x.</param>
    /// <returns></returns>
    public bool CheckProductionOf(char x)
    {
        List <Production> productions = GrammarTools.GetProductionsOf((( Element )stack.Peek()).value, grammar);

        if (productions.Count == 0)
        {
            //GamEvent.instance.RejectRow
            Debug.Log("Reject Row");
            return(false);
        }

        Production productioToCheck = GrammarTools.GetProductionWithValue(x, productions);



        if (productioToCheck is null)
        {
            if (!CheckSelectionSetOfPeek(productions, x) || !CheckProductionsRighSideStartNoTerminal(productions, x))
            {
                return(false);
            }
        }
        else
        {
            stack.Pop();
            if (productioToCheck.rightSide.Count >= 2)
            {
                for (int i = productioToCheck.rightSide.Count - 1; i >= 1; i--)
                {
                    stack.Push(productioToCheck.rightSide [i]);
                }
            }
        }

        return(true);
    }
    /// <summary>Gets the next of no terminal.</summary>
    /// <param name="visited">The visited.</param>
    /// <param name="values">The values.</param>
    /// <param name="nameNoTerminal">The name no terminal.</param>
    /// <param name="grammar">The grammar.</param>
    /// <returns></returns>
    public static List <char> GetNextOfNoTerminal(List <char> visited, List <char> values, char nameNoTerminal, GenericGrammar grammar)
    {
        visited.Add(nameNoTerminal);
        for (int i = 0; i < grammar.productions.Count; i++)
        {
            for (int j = 0; j < grammar.productions [i].rightSide.Count; j++)
            {
                if (grammar.productions [i].rightSide [j].value.Equals(nameNoTerminal))
                {
                    #region  s
                    if (GetSetOfNoTerminals(grammar) [0].name.Equals(grammar.productions [i].rightSide [j].value))
                    {
                        if (!values.Contains('¬'))
                        {
                            values.Add('¬');
                        }
                    }
                    #endregion


                    if ((j + 1) < grammar.productions [i].rightSide.Count)
                    {
                        #region Next is a terminal
                        if (grammar.productions [i].rightSide [j + 1].kind.Equals(Element.Kind.terminal))
                        {
                            if (!values.Contains(grammar.productions [i].rightSide [j + 1].value))
                            {
                                values.Add(grammar.productions [i].rightSide [j + 1].value);
                            }
                        }
                        #endregion

                        #region Next is a no terminal
                        else if (grammar.productions [i].rightSide [j + 1].kind.Equals(Element.Kind.noTerminal))
                        {
                            values.Intersect(GetFirstOfNoTerminal(values, grammar.productions [i].rightSide [j + 1].value, grammar)).Any();
                            #region Next is anullable
                            if (ListContains(GrammarTools.GetAnullableNoTerminals(grammar), grammar.productions [i].rightSide [j + 1]) && !visited.Contains(grammar.productions [i].rightSide [j + 1].value))
                            {
                                values.Intersect(GetNextOfNoTerminal(visited, values, grammar.productions [i].rightSide [j + 1].value, grammar)).Any();
                            }
                            #endregion
                        }
                        #endregion
                    }
                    #region At the end
                    else if ((j + 1) == grammar.productions [i].rightSide.Count)
                    {
                        //Tener en cuenta si es anulable que se hace

                        if (!grammar.productions [i].leftSide.value.Equals(nameNoTerminal) && !visited.Contains(grammar.productions [i].leftSide.value))
                        {
                            values.Intersect(GetNextOfNoTerminal(visited, values, grammar.productions [i].leftSide.value, grammar)).Any();
                        }
                    }
                    #endregion
                }
            }
        }
        return(values);
    }
Exemplo n.º 7
0
    public void GetSeleccionSetOfProductionsTest()
    {
        #region Define grammar
        GenericGrammar genericGrammar = new GenericGrammar();


        Production production1  = new Production();
        Production production2  = new Production();
        Production production3  = new Production();
        Production production4  = new Production();
        Production production5  = new Production();
        Production production6  = new Production();
        Production production7  = new Production();
        Production production8  = new Production();
        Production production9  = new Production();
        Production production10 = new Production();

        production1.leftSide  = new Element(Element.Kind.noTerminal, 'A', 1);
        production2.leftSide  = new Element(Element.Kind.noTerminal, 'A', 2);
        production3.leftSide  = new Element(Element.Kind.noTerminal, 'B', 3);
        production4.leftSide  = new Element(Element.Kind.noTerminal, 'B', 4);
        production5.leftSide  = new Element(Element.Kind.noTerminal, 'C', 5);
        production6.leftSide  = new Element(Element.Kind.noTerminal, 'C', 6);
        production7.leftSide  = new Element(Element.Kind.noTerminal, 'D', 7);
        production8.leftSide  = new Element(Element.Kind.noTerminal, 'D', 8);
        production9.leftSide  = new Element(Element.Kind.noTerminal, 'E', 9);
        production10.leftSide = new Element(Element.Kind.noTerminal, 'E', 10);


        production1.rightSide.Add(new Element(Element.Kind.terminal, 'a'));
        production1.rightSide.Add(new Element(Element.Kind.noTerminal, 'B'));
        production1.rightSide.Add(new Element(Element.Kind.noTerminal, 'C'));

        production2.rightSide.Add(new Element(Element.Kind.noTerminal, 'D'));
        production2.rightSide.Add(new Element(Element.Kind.terminal, 'b'));
        production2.rightSide.Add(new Element(Element.Kind.noTerminal, 'A'));


        production3.rightSide.Add(new Element(Element.Kind.nulo, '¬'));


        production4.rightSide.Add(new Element(Element.Kind.terminal, 'b'));
        production4.rightSide.Add(new Element(Element.Kind.noTerminal, 'A'));
        production4.rightSide.Add(new Element(Element.Kind.noTerminal, 'B'));

        production5.rightSide.Add(new Element(Element.Kind.terminal, 'c'));
        production5.rightSide.Add(new Element(Element.Kind.noTerminal, 'C'));

        production6.rightSide.Add(new Element(Element.Kind.noTerminal, 'D'));
        production6.rightSide.Add(new Element(Element.Kind.terminal, 'd'));
        production6.rightSide.Add(new Element(Element.Kind.noTerminal, 'B'));

        production7.rightSide.Add(new Element(Element.Kind.nulo, '¬'));

        production8.rightSide.Add(new Element(Element.Kind.terminal, 'e'));
        production8.rightSide.Add(new Element(Element.Kind.noTerminal, 'E'));

        production9.rightSide.Add(new Element(Element.Kind.noTerminal, 'B'));
        production9.rightSide.Add(new Element(Element.Kind.noTerminal, 'D'));

        production10.rightSide.Add(new Element(Element.Kind.terminal, 'f'));


        genericGrammar.productions.Add(production1);
        genericGrammar.productions.Add(production2);
        genericGrammar.productions.Add(production3);
        genericGrammar.productions.Add(production4);
        genericGrammar.productions.Add(production5);
        genericGrammar.productions.Add(production6);
        genericGrammar.productions.Add(production7);
        genericGrammar.productions.Add(production8);
        genericGrammar.productions.Add(production9);
        genericGrammar.productions.Add(production10);
        #endregion

        var result = GrammarTools.GetSeleccionSetOfProductions(genericGrammar);


        #region print result

        foreach (Set set in result)
        {
            Debug.Log(set.index);
            foreach (char value in set.values)
            {
                Debug.Log("Values :" + value);
            }
            Debug.Log("--------");
        }
        #endregion

        #region Define expectedSet
        List <Set> expectedSet = new List <Set>()
        {
            new Set(1, new List <char>()
            {
                'a'
            }, Set.Kind.first),
            new Set(2, new List <char>()
            {
                'b', 'e'
            }, Set.Kind.first),
            new Set(3, new List <char>()
            {
                'b', 'c', 'd', 'e', '¬'
            }, Set.Kind.first),
            new Set(4, new List <char>()
            {
                'b',
            }, Set.Kind.first),
            new Set(5, new List <char>()
            {
                'c'
            }, Set.Kind.first),
            new Set(6, new List <char>()
            {
                'd', 'e'
            }, Set.Kind.first),
            new Set(7, new List <char>()
            {
                'b', 'd'
            }, Set.Kind.first),
            new Set(8, new List <char>()
            {
                'e'
            }, Set.Kind.first),
            new Set(9, new List <char>()
            {
                'b', 'd', 'e'
            }, Set.Kind.first),
            new Set(10, new List <char>()
            {
                'f'
            }, Set.Kind.first)
        };
        var r = true;
        for (int i = 0; i < result.Count; i++)
        {
            if (!(result [i].index.Equals(expectedSet [i].index)))
            {
                r = false;
                break;
            }
            else
            {
                for (int j = 0; j < result [i].values.Count; j++)
                {
                    if (!(result [i].values.Contains(expectedSet [i].values [j])))
                    {
                        r = false;
                        break;
                    }
                }
            }
        }
        #endregion

        Assert.That(r, Is.EqualTo(true));
    }
Exemplo n.º 8
0
    public void AnullableProductionsTest()
    {
        #region Define grammar
        GenericGrammar genericGrammar = new GenericGrammar();


        Production production1  = new Production();
        Production production2  = new Production();
        Production production3  = new Production();
        Production production4  = new Production();
        Production production5  = new Production();
        Production production6  = new Production();
        Production production7  = new Production();
        Production production8  = new Production();
        Production production9  = new Production();
        Production production10 = new Production();

        production1.leftSide  = new Element(Element.Kind.noTerminal, 'A');
        production2.leftSide  = new Element(Element.Kind.noTerminal, 'A');
        production3.leftSide  = new Element(Element.Kind.noTerminal, 'B');
        production4.leftSide  = new Element(Element.Kind.noTerminal, 'B');
        production5.leftSide  = new Element(Element.Kind.noTerminal, 'C');
        production6.leftSide  = new Element(Element.Kind.noTerminal, 'C');
        production7.leftSide  = new Element(Element.Kind.noTerminal, 'D');
        production8.leftSide  = new Element(Element.Kind.noTerminal, 'D');
        production9.leftSide  = new Element(Element.Kind.noTerminal, 'E');
        production10.leftSide = new Element(Element.Kind.noTerminal, 'E');


        production1.rightSide.Add(new Element(Element.Kind.terminal, 'a'));
        production1.rightSide.Add(new Element(Element.Kind.noTerminal, 'B'));
        production1.rightSide.Add(new Element(Element.Kind.noTerminal, 'C'));

        production2.rightSide.Add(new Element(Element.Kind.noTerminal, 'D'));
        production2.rightSide.Add(new Element(Element.Kind.terminal, 'b'));
        production2.rightSide.Add(new Element(Element.Kind.noTerminal, 'A'));


        production3.rightSide.Add(new Element(Element.Kind.nulo, '¬'));


        production4.rightSide.Add(new Element(Element.Kind.terminal, 'b'));
        production4.rightSide.Add(new Element(Element.Kind.noTerminal, 'A'));
        production4.rightSide.Add(new Element(Element.Kind.noTerminal, 'B'));

        production5.rightSide.Add(new Element(Element.Kind.terminal, 'c'));
        production5.rightSide.Add(new Element(Element.Kind.noTerminal, 'C'));

        production6.rightSide.Add(new Element(Element.Kind.noTerminal, 'D'));
        production6.rightSide.Add(new Element(Element.Kind.terminal, 'd'));
        production6.rightSide.Add(new Element(Element.Kind.noTerminal, 'B'));

        production7.rightSide.Add(new Element(Element.Kind.nulo, '¬'));

        production8.rightSide.Add(new Element(Element.Kind.terminal, 'e'));
        production8.rightSide.Add(new Element(Element.Kind.noTerminal, 'E'));

        production9.rightSide.Add(new Element(Element.Kind.noTerminal, 'B'));
        production9.rightSide.Add(new Element(Element.Kind.noTerminal, 'D'));

        production10.rightSide.Add(new Element(Element.Kind.terminal, 'f'));


        genericGrammar.productions.Add(production1);
        genericGrammar.productions.Add(production2);
        genericGrammar.productions.Add(production3);
        genericGrammar.productions.Add(production4);
        genericGrammar.productions.Add(production5);
        genericGrammar.productions.Add(production6);
        genericGrammar.productions.Add(production7);
        genericGrammar.productions.Add(production8);
        genericGrammar.productions.Add(production9);
        genericGrammar.productions.Add(production10);
        #endregion

        var result = GrammarTools.GetAnnullableProductions(genericGrammar);

        foreach (var item in result)
        {
            // Debug.Log ( item );
        }


        List <int> expectedElements = new List <int>()
        {
            3, 7, 9
        };
        var r = false;
        for (int i = 0; i < result.Count; i++)
        {
            if (result [i].Equals(expectedElements [i]))
            {
                r = true;
            }
        }

        Assert.That(r, Is.EqualTo(true));
    }
Exemplo n.º 9
0
    public void NAnullableTest()
    {
        //ARRANGE: All preconditions and inputs
        GenericGrammar genericGrammar = new GenericGrammar();

        #region Define grammar

        Production production1  = new Production();
        Production production2  = new Production();
        Production production3  = new Production();
        Production production4  = new Production();
        Production production5  = new Production();
        Production production6  = new Production();
        Production production7  = new Production();
        Production production8  = new Production();
        Production production9  = new Production();
        Production production10 = new Production();

        production1.leftSide  = new Element(Element.Kind.noTerminal, 'A');
        production2.leftSide  = new Element(Element.Kind.noTerminal, 'A');
        production3.leftSide  = new Element(Element.Kind.noTerminal, 'B');
        production4.leftSide  = new Element(Element.Kind.noTerminal, 'B');
        production5.leftSide  = new Element(Element.Kind.noTerminal, 'C');
        production6.leftSide  = new Element(Element.Kind.noTerminal, 'C');
        production7.leftSide  = new Element(Element.Kind.noTerminal, 'D');
        production8.leftSide  = new Element(Element.Kind.noTerminal, 'D');
        production9.leftSide  = new Element(Element.Kind.noTerminal, 'E');
        production10.leftSide = new Element(Element.Kind.noTerminal, 'E');


        production1.rightSide.Add(new Element(Element.Kind.terminal, 'a'));
        production1.rightSide.Add(new Element(Element.Kind.noTerminal, 'B'));
        production1.rightSide.Add(new Element(Element.Kind.noTerminal, 'C'));

        production2.rightSide.Add(new Element(Element.Kind.noTerminal, 'D'));
        production2.rightSide.Add(new Element(Element.Kind.terminal, 'b'));
        production2.rightSide.Add(new Element(Element.Kind.noTerminal, 'A'));


        production3.rightSide.Add(new Element(Element.Kind.nulo, '¬'));


        production4.rightSide.Add(new Element(Element.Kind.terminal, 'b'));
        production4.rightSide.Add(new Element(Element.Kind.noTerminal, 'A'));
        production4.rightSide.Add(new Element(Element.Kind.noTerminal, 'B'));

        production5.rightSide.Add(new Element(Element.Kind.terminal, 'c'));
        production5.rightSide.Add(new Element(Element.Kind.noTerminal, 'C'));

        production6.rightSide.Add(new Element(Element.Kind.noTerminal, 'D'));
        production6.rightSide.Add(new Element(Element.Kind.terminal, 'd'));
        production6.rightSide.Add(new Element(Element.Kind.noTerminal, 'B'));

        production7.rightSide.Add(new Element(Element.Kind.nulo, '¬'));

        production8.rightSide.Add(new Element(Element.Kind.terminal, 'e'));
        production8.rightSide.Add(new Element(Element.Kind.noTerminal, 'E'));

        production9.rightSide.Add(new Element(Element.Kind.noTerminal, 'B'));
        production9.rightSide.Add(new Element(Element.Kind.noTerminal, 'D'));

        production10.rightSide.Add(new Element(Element.Kind.terminal, 'f'));


        genericGrammar.productions.Add(production1);
        genericGrammar.productions.Add(production2);
        genericGrammar.productions.Add(production3);
        genericGrammar.productions.Add(production4);
        genericGrammar.productions.Add(production5);
        genericGrammar.productions.Add(production6);
        genericGrammar.productions.Add(production7);
        genericGrammar.productions.Add(production8);
        genericGrammar.productions.Add(production9);
        genericGrammar.productions.Add(production10);
        #endregion
        #region ACT
        //ACT on the method under test

        var result = GrammarTools.GetAnullableNoTerminals(genericGrammar);
        #endregion

        #region Expected

        //ASSERT occurence of expected results


        List <Element> expectedElements = new List <Element>
        {
            new Element(Element.Kind.noTerminal, 'B'),
            new Element(Element.Kind.noTerminal, 'D'),
            new Element(Element.Kind.noTerminal, 'E')
        };

        var r = false;

        for (int i = 0; i < result.Count; i++)
        {
            if (result [i].value.Equals(expectedElements[i].value) && result [i].kind.Equals(expectedElements [i].kind))
            {
                r = true;
            }
        }

        // CollectionAssert.AreEqual ( expectedElements.ToArray() ,result.ToArray() );
        Assert.That(r, Is.EqualTo(true));
        #endregion
    }