/// <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);
    }
Example #2
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
    }