Ejemplo n.º 1
0
    /// <summary>Gets the same productions.</summary>
    /// <param name="grammar">The grammar.</param>
    /// <returns>List of productions</returns>
    public static List <Production> GetSameProductions(GenericGrammar grammar)
    {
        List <Production> sameProductions = new List <Production>();

        for (int i = 0; i < grammar.productions.Count; i++)
        {
            for (int j = 0; j < grammar.productions.Count; j++)
            {
                if (i != j)
                {
                    if (grammar.productions [i].leftSide.value.Equals(grammar.productions [j].leftSide.value))
                    {
                        if (!sameProductions.Contains(grammar.productions [i]))
                        {
                            sameProductions.Add(grammar.productions [i]);
                        }

                        if (!sameProductions.Contains(grammar.productions [j]))
                        {
                            sameProductions.Add(grammar.productions [j]);
                        }
                    }
                }
            }
        }

        return(sameProductions);
    }
    /// <summary>Initials the configuration.</summary>
    /// <param name="grammar">The grammar.</param>
    /// <returns></returns>
    public Stack InitialConfiguration(GenericGrammar grammar)
    {
        stack = new Stack();
        stack.Push(nullSymbol);
        stack.Push(grammar.productions[0].leftSide);

        return(stack);
    }
    /// <summary>Gets the first no terminals.</summary>
    /// <param name="grammar">The grammar.</param>
    /// <returns></returns>
    public static List <Set> GetFirstNoTerminals(GenericGrammar grammar)
    {
        List <Set> setFirstNoTerminals = GetSetOfNoTerminals(grammar);

        foreach (Set set in setFirstNoTerminals)
        {
            set.values.AddRange(GetFirstOfNoTerminal(new List <char> (), set.name, grammar));
        }

        return(setFirstNoTerminals);
    }
    /// <summary>Gets the set of productions.</summary>
    /// <param name="grammar">The grammar.</param>
    /// <returns></returns>
    public static List <Set> GetSetOfProductions(GenericGrammar grammar)
    {
        List <Set> setOfProductions = new List <Set>();

        for (int i = 0; i < grammar.productions.Count; i++)
        {
            setOfProductions.Add(new Set(grammar.productions [i].leftSide.index, grammar.productions [i].leftSide.value, Set.Kind.first));
        }

        return(setOfProductions);
    }
    /// <summary>Gets the next of no terminals.</summary>
    /// <param name="grammar">The grammar.</param>
    /// <returns></returns>
    public static List <Set> GetNextOfNoTerminals(GenericGrammar grammar)
    {
        List <Set> setNextProduction = new List <Set>();

        setNextProduction = GetSetOfNoTerminals(grammar);
        foreach (Set set in setNextProduction)
        {
            set.values.AddRange(GetNextOfNoTerminal(new List <char> (), new List <char> (), set.name, grammar));
        }

        return(setNextProduction);
    }
    /// <summary>Gets the seleccion set of productions.</summary>
    /// <param name="grammar">The grammar.</param>
    /// <returns></returns>
    public static List <Set> GetSeleccionSetOfProductions(GenericGrammar grammar)
    {
        List <Set> setSelection = new List <Set>();

        setSelection = GetSetOfProductions(grammar);

        foreach (Set set in setSelection)
        {
            set.values.AddRange(GetSeleccionSetOfProduction(new List <char> (), set.index, grammar));
        }
        return(setSelection);
    }
Ejemplo n.º 7
0
    /// <summary>Firsts the elements are terminals.</summary>
    /// <param name="grammar">The grammar.</param>
    /// <returns><para>true o false</para></para></returns>
    public static bool FirstElementsAreTerminals(GenericGrammar grammar)
    {
        foreach (Production production in grammar.productions)
        {
            if ((production.rightSide [0].kind == Element.Kind.noTerminal) || (production.rightSide [0].kind == Element.Kind.nulo))
            {
                return(false);
            }
        }

        return(true);
    }
    /// <summary>Gets the annullable productions.</summary>
    /// <param name="grammar">The grammar.</param>
    /// <returns></returns>
    public static List <int> GetAnnullableProductions(GenericGrammar grammar)
    {
        List <int> anullableProductionsIndex = new List <int>();

        for (int i = 0; i < grammar.productions.Count; i++)
        {
            if (IsAnullableProduction(grammar.productions [i], grammar))
            {
                anullableProductionsIndex.Add(i + 1);
            }
        }
        return(anullableProductionsIndex);
    }
    // Start is called before the first frame update
    #region Main methods
    /// <summary>Gets the anullable no terminals.</summary>
    /// <param name="grammar">The grammar.</param>
    /// <returns></returns>
    public static List <Element> GetAnullableNoTerminals(GenericGrammar grammar)
    {
        List <Element> nAnullables = new List <Element>();

        foreach (var production in grammar.productions)
        {
            if (IsAnullableProduction(production, grammar))
            {
                nAnullables.Add(production.leftSide);
            }
        }

        return(nAnullables);
    }
    /// <summary>Gets the set of no terminals.</summary>
    /// <param name="grammar">The grammar.</param>
    /// <returns></returns>
    public static List <Set> GetSetOfNoTerminals(GenericGrammar grammar)
    {
        List <Set> setOfNoTerminals = new List <Set>();

        //Search noterminal in grammar.produdctions
        foreach (Production production in grammar.productions)
        {
            if (!ListContains(setOfNoTerminals.ToArray(), production.leftSide))
            {
                setOfNoTerminals.Add(new Set(production.leftSide.value, Set.Kind.first));
            }
        }

        return(setOfNoTerminals);
    }
Ejemplo n.º 11
0
    /// <summary>Gets the productions of.</summary>
    /// <param name="name">The name.</param>
    /// <param name="grammar">The grammar.</param>
    /// <returns></returns>
    public static List <Production> GetProductionsOf(char name, GenericGrammar grammar)
    {
        List <Production> productions = new List <Production>();

        foreach (Production production in grammar.productions)
        {
            if (production.leftSide.value.Equals(name))
            {
                productions.Add(production);
            }
        }


        return(productions);
    }
Ejemplo n.º 12
0
    /// <summary>Checks the right side.</summary>
    /// <param name="grammar">The grammar.</param>
    /// <returns></returns>
    public static bool CheckRightSide(GenericGrammar grammar)
    {
        List <Production> nameOfSameProductions = GetSameProductions(grammar);

        foreach (Production nameOfProduction in nameOfSameProductions)
        {
            List <Production> sameProductions = GetProductions(nameOfProduction.leftSide.value, grammar);

            if (!VerificationRightSide(sameProductions, grammar))
            {
                return(false);
            }
        }

        return(true);
    }
Ejemplo n.º 13
0
    /// <summary>Determines whether [is disjoint selection set] [the specified grammar].</summary>
    /// <param name="grammar">The grammar.</param>
    /// <returns>
    ///   <c>true</c> if [is disjoint selection set] [the specified grammar]; otherwise, <c>false</c>.</returns>
    public static bool IsDisjointSelectionSet(GenericGrammar grammar)
    {
        List <Production> nameOfSameProductions = GetSameProductions(grammar);

        foreach (Production nameOfProduction in nameOfSameProductions)
        {
            List <Production> sameProductions = GetProductionsOf(nameOfProduction.leftSide.value, grammar);

            if (!IsDisjointProductions(sameProductions, grammar))
            {
                return(false);
            }
        }


        return(true);
    }
    /// <summary>Determines whether [is left side of production a nullable] [the specified element].</summary>
    /// <param name="element">The element.</param>
    /// <param name="grammar">The grammar.</param>
    /// <returns>
    ///   <c>true</c> if [is left side of production a nullable] [the specified element]; otherwise, <c>false</c>.</returns>
    public static bool IsLeftSideOfProductionANullable(Element element, GenericGrammar grammar)
    {
        foreach (var production in grammar.productions)
        {
            if (production.leftSide.value.Equals(element.value) && production.leftSide.kind.Equals(element.kind))
            {
                if (production.rightSide [0].kind.Equals(Element.Kind.nulo))
                {
                    return(true);
                }
                else if (production.rightSide [0].kind.Equals(Element.Kind.noTerminal))
                {
                    return(IsAnullableProduction(production, grammar));
                }
            }
        }

        return(false);
    }
 /// <summary>Gets the first of production.</summary>
 /// <param name="values">The values.</param>
 /// <param name="index">The index.</param>
 /// <param name="grammar">The grammar.</param>
 /// <returns></returns>
 private static List <char> GetFirstOfProduction(List <char> values, int index, GenericGrammar grammar)
 {
     foreach (Element element in grammar.productions[index - 1].rightSide)
     {
         if (element.kind.Equals(Element.Kind.terminal))
         {
             if (!values.Contains(element.value))
             {
                 values.Add(element.value);
                 break;
             }
         }
         else if (element.kind.Equals(Element.Kind.noTerminal))
         {
             values.Intersect(GetFirstOfNoTerminal(values, element.value, grammar)).Any();
         }
     }
     return(values);
 }
Ejemplo n.º 16
0
    public void ValidateRowTest()
    {
        GenericGrammar genericGrammar = new GenericGrammar();

        #region Define grammar 2

        Production production1 = new Production();
        Production production2 = new Production();
        Production production3 = new Production();
        Production production4 = new Production();

        production1.leftSide = new Element(Element.Kind.noTerminal, 'B');
        production2.leftSide = new Element(Element.Kind.noTerminal, 'B');
        production3.leftSide = new Element(Element.Kind.noTerminal, 'C');
        production4.leftSide = new Element(Element.Kind.noTerminal, 'C');

        production1.rightSide.Add(new Element(Element.Kind.terminal, 'b'));
        production1.rightSide.Add(new Element(Element.Kind.noTerminal, 'C'));
        production1.rightSide.Add(new Element(Element.Kind.terminal, 'd'));

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

        production3.rightSide.Add(new Element(Element.Kind.terminal, 'b'));
        production3.rightSide.Add(new Element(Element.Kind.noTerminal, 'C'));
        production3.rightSide.Add(new Element(Element.Kind.terminal, 'd'));

        production4.rightSide.Add(new Element(Element.Kind.terminal, 'a'));

        genericGrammar.productions.Add(production1);
        genericGrammar.productions.Add(production2);
        genericGrammar.productions.Add(production3);
        genericGrammar.productions.Add(production4);

        #endregion
        string row = "abbadd¬";

        S   grammarS = new S(genericGrammar);
        var result   = grammarS.ValidateRow(row);

        Assert.That(result, Is.EqualTo(true));
    }
Ejemplo n.º 17
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>Determines whether [is anullable production] [the specified production].</summary>
 /// <param name="production">The production.</param>
 /// <param name="grammar">The grammar.</param>
 /// <returns>
 ///   <c>true</c> if [is anullable production] [the specified production]; otherwise, <c>false</c>.</returns>
 public static bool IsAnullableProduction(Production production, GenericGrammar grammar)
 {
     foreach (var element in production.rightSide)
     {
         if (element.kind.Equals(Element.Kind.nulo))
         {
             return(true);
         }
         else if (element.kind.Equals(Element.Kind.noTerminal))
         {
             if (!IsLeftSideOfProductionANullable(element, grammar))
             {
                 return(false);
             }
         }
         else
         {
             return(false);
         }
     }
     return(true);
 }
Ejemplo n.º 19
0
    /// <summary>Verifications the right side.</summary>
    /// <param name="sameProductions">The same productions.</param>
    /// <param name="grammar">The grammar.</param>
    /// <returns></returns>
    private static bool VerificationRightSide(List <Production> sameProductions, GenericGrammar grammar)
    {
        for (int i = 0; i < sameProductions.Count; i++)
        {
            Production production = ( Production )grammar.productions [i];
            if (production.rightSide [0].kind.Equals(Element.Kind.terminal))
            {
                for (int j = 0; j < sameProductions.Count; j++)
                {
                    Production production2 = ( Production )grammar.productions [j];

                    if (i != j)
                    {
                        if (production2.rightSide [0].kind.Equals(Element.Kind.terminal))
                        {
                            return(false);
                        }
                    }
                }
            }
        }

        return(true);
    }
Ejemplo n.º 20
0
 /// <summary>Sames the production difference right terminal.</summary>
 /// <param name="grammar">The grammar.</param>
 /// <returns></returns>
 public static bool SameProductionDiffRightTerminal(GenericGrammar grammar) => (CompareRightFirstTerminal(GetSameProductions(grammar).ToArray())) ? true : false;
Ejemplo n.º 21
0
 /// <summary>Belongs the specified grammar.</summary>
 /// <param name="grammar">The grammar.</param>
 /// <returns><para>true o false if the gramar belong to that grammar</para></para></returns>
 public static bool Belong(GenericGrammar grammar) => (!FirstElementsAreTerminals(grammar) ||
                                                       !SameProductionDiffRightTerminal(grammar))
                                                          ? false : true;
Ejemplo n.º 22
0
    /// <summary>Initializes a new instance of the <see cref="S" /> class.</summary>
    /// <param name="grammar">The grammar.</param>
    public S(GenericGrammar grammar)
    {
        this.grammar = grammar;

        stackAutomat = new S_StackAutomat(grammar);
    }
Ejemplo n.º 23
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
    }
    /// <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);
    }
Ejemplo n.º 25
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));
    }
    /// <summary>Initializes a new instance of the <see cref="S_StackAutomat" /> class.</summary>
    /// <param name="grammar">The grammar.</param>
    public S_StackAutomat(GenericGrammar grammar)
    {
        this.grammar = grammar;

        stack = InitialConfiguration(grammar);
    }
Ejemplo n.º 27
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));
    }
    /// <summary>Gets the first of no terminal.</summary>
    /// <param name="values">The values.</param>
    /// <param name="noTerminalValue">The no terminal value.</param>
    /// <param name="grammar">The grammar.</param>
    /// <returns></returns>
    public static List <char> GetFirstOfNoTerminal(List <char> values, char noTerminalValue, GenericGrammar grammar)
    {
        foreach (Production production in grammar.productions)
        {
            if (noTerminalValue.Equals(production.leftSide.value))
            {
                foreach (Element element in production.rightSide)
                {
                    if (element.kind.Equals(Element.Kind.terminal))
                    {
                        if (!values.Contains(element.value))
                        {
                            values.Add(element.value);
                        }

                        break;
                    }
                    else if (element.kind.Equals(Element.Kind.noTerminal))
                    {
                        values.Intersect(GetFirstOfNoTerminal(values, element.value, grammar)).Any();
                    }
                }
            }
        }

        return(values);
    }
    /// <summary>Gets the seleccion set of production.</summary>
    /// <param name="values">The values.</param>
    /// <param name="index">The index.</param>
    /// <param name="grammar">The grammar.</param>
    /// <returns></returns>
    public static List <char> GetSeleccionSetOfProduction(List <char> values, int index, GenericGrammar grammar)
    {
        Production production = grammar.productions[index - 1];

        values.Intersect(GetFirstOfProduction(values, index, grammar)).Any();

        if (IsAnullableProduction(production, grammar))
        {
            values.Intersect(GetNextOfNoTerminal(new List <char> (), values, production.leftSide.value, grammar)).Any();
        }

        return(values);
    }
Ejemplo n.º 30
0
 /// <summary>Initializes a new instance of the <see cref="Q" /> class.</summary>
 /// <param name="grammar">The grammar.</param>
 public Q(GenericGrammar grammar)
 {
     this.grammar = grammar;
     //Cond
     stackAutomat = new Q_StackAutomat(grammar);
 }