コード例 #1
0
        public void printD(int dnumber)
        {
            DClass d = DList[dnumber];

            Console.WriteLine("D{0}:", dnumber);

            foreach (StateStruct ss in d.SList)
            {
                Console.Write("\t{0}  > ", ss.leftSide);
                int j = 0;
                foreach (string s in ss.rightSide)
                {
                    if (j == ss.pointer)
                    {
                        Console.Write(" .");
                    }
                    Console.Write(" {0}", s);
                    j++;
                }
                if (j == ss.pointer)
                {
                    Console.Write(" .");
                }

                Console.WriteLine("   /{0}", ss.origin);
            }
        }
コード例 #2
0
        private bool predict(DClass D, StateStruct stateToPredict)
        {
            bool   predictedSomething = false;
            string variableToBeAdded  = stateToPredict.rightSide[stateToPredict.pointer];

            if (gramatica.isVariavel(variableToBeAdded))
            {
                bool alreadyInAState = false;

                foreach (StateStruct ss in D.SList)
                {
                    if (string.Equals(ss.leftSide, variableToBeAdded) && ss.origin == inputPointer)
                    {
                        alreadyInAState = true;
                    }
                }

                if (!alreadyInAState)
                {
                    foreach (List <string> producao in gramatica.regrasDeProducao[variableToBeAdded])
                    {
                        StateStruct state = new StateStruct(variableToBeAdded, producao, 0, inputPointer);
                        D.SList.Add(state);
                        predictedSomething = true;
                    }
                }
            }
            return(predictedSomething);
        }
コード例 #3
0
        private void complete(DClass D, StateStruct stateToComplete)
        {
            string variableToComplete = stateToComplete.leftSide;
            bool   alreadyCompleted   = false;

            foreach (string variable in D.AlreadyCompletedList)
            {
                if (string.Equals(variableToComplete, variable))
                {
                    alreadyCompleted = true;
                }
            }

            if (!alreadyCompleted)
            {
                foreach (StateStruct state in DList[stateToComplete.origin].SList)
                {
                    if (state.pointer < state.rightSide.Count)
                    {
                        if (String.Equals(state.rightSide[state.pointer], variableToComplete))
                        {
                            StateStruct nState = new StateStruct(state.leftSide, state.rightSide, state.pointer + 1, state.origin);
                            D.SList.Add(nState);
                            if (nState.pointer >= nState.rightSide.Count)
                            {
                                complete(D, nState);
                                //predict(D, nState);
                            }
                        }
                    }
                }
                D.AlreadyCompletedList.Add(variableToComplete);
            }
        }
コード例 #4
0
        public string generateSentence(string randomSentence)
        {
            string randomTerminal = "";

            Random rng = new Random();

            DClass D = new DClass();

            List <string> terminalsFromPreviousD = getTerminalsFromPreviousDClass(inputPointer);

            if (terminalsFromPreviousD.Count != 0)
            {
                randomTerminal  = terminalsFromPreviousD[rng.Next(terminalsFromPreviousD.Count)];
                randomSentence += " " + randomTerminal;

                scan(D, randomTerminal);
                predict(D);

                DList.Add(D);

                inputPointer++;
            }

            return(randomSentence);
        }
コード例 #5
0
 private void scan(DClass D, string token)
 {
     if (gramatica.isTerminal(token))
     {
         foreach (StateStruct state in DList[inputPointer - 1].SList)
         {
             if (state.pointer < state.rightSide.Count)
             {
                 if (String.Equals(state.rightSide[state.pointer], token))
                 {
                     StateStruct nState = new StateStruct(state.leftSide, state.rightSide, state.pointer + 1, state.origin);
                     D.SList.Add(nState);
                     if (nState.pointer >= nState.rightSide.Count)
                     {
                         complete(D, nState);
                         //predict(D, nState);
                     }
                     //else
                     //{
                     //    predict(D, nState);
                     //}
                 }
             }
         }
     }
 }
コード例 #6
0
        private void createInitial()
        {
            //List<StateStruct> D0 = new List<StateStruct>();

            DClass D0 = new DClass();

            foreach (List <string> regra in gramatica.regrasDeProducao[gramatica.inicial])
            {
                StateStruct D0Initializer = new StateStruct(gramatica.inicial, regra, 0, 0);
                D0.SList.Add(D0Initializer);
                //predict(D0, D0Initializer);
            }
            predict(D0);
            //StateStruct D0Initial = new StateStruct(gramatica.inicial, gramatica.regrasDeProducao[gramatica.inicial][0], 0, 0);



            //D0.SList.Add(D0Initial);

            DList.Add(D0);

            inputPointer = 1;

            //string variableToBeAdded = D0Initial.rightSide[D0Initial.pointer];
            //bool alreadyInAState = false;

            //if (gramatica.isVariavel(variableToBeAdded))
            //{
            //    foreach(StateStruct ss in D0.SList)
            //    {
            //        if(string.Equals(ss.leftSide,variableToBeAdded))
            //        {
            //            alreadyInAState = true;
            //        }
            //    }

            //    if (!alreadyInAState)
            //    {
            //        foreach (List<string> producao in gramatica.regrasDeProducao[variableToBeAdded])
            //        {
            //            StateStruct state = new StateStruct(variableToBeAdded, producao, 0, 0);
            //            D0.SList.Add(state);


            //        }
            //    }

            //    //StateStruct D = new StateStruct(D0Initial.rightSide[D0Initial.pointer],);
            //}

            //foreach (string prod in D0Initial.rightSide)
            //{

            //}  //codigo não usado
        }
コード例 #7
0
        private void createInitial()
        {
            DClass D0 = new DClass();

            StateStruct D0Initial = new StateStruct(gramatica.inicial, gramatica.regrasDeProducao[gramatica.inicial][0], 0, 0);

            D0.SList.Add(D0Initial);

            predict(D0, D0Initial);

            DList.Add(D0);
        }
コード例 #8
0
        public string generateSentence(int size)
        {
            string randomSentence = "";
            string randomTerminal = "";

            Random rng = new Random();

            inputPointer = 1;

            bool noMoreTerminals = false;

            while (((inputPointer < size + 1) || (!checkSuccess())) && !noMoreTerminals)
            {
                DClass D = new DClass();

                List <string> terminalsFromPreviousD = getTerminalsFromPreviousDClass(inputPointer);

                if (terminalsFromPreviousD.Count != 0)
                {
                    randomTerminal  = terminalsFromPreviousD[rng.Next(terminalsFromPreviousD.Count)];
                    randomSentence += " " + randomTerminal;

                    scan(D, randomTerminal);
                    predict(D);

                    DList.Add(D);

                    inputPointer++;
                }
                else
                {
                    noMoreTerminals = true;
                }
            }

            //for (inputPointer = 1; inputPointer < size + 1; inputPointer++)
            //{
            //    DClass D = new DClass();

            //    List<string> terminalsFromPreviousD = getTerminalsFromPreviousDClass(D, inputPointer);

            //    randomTerminal = terminalsFromPreviousD[rng.Next(terminalsFromPreviousD.Count)];
            //    randomSentence += randomTerminal;

            //    scan(D, randomTerminal);

            //    DList.Add(D);
            //}

            return(randomSentence);
        }
コード例 #9
0
        public void parseSentence(string[] inputStringArray)
        {
            for (inputPointer = 1; inputPointer < inputStringArray.Length + 1; inputPointer++)
            {
                DClass D = new DClass();

                scan(D, inputStringArray[inputPointer - 1]);
                predict(D);

                DList.Add(D);
            }

            checkSuccess();
        }
コード例 #10
0
        /// <summary>
        /// Função para pegar todos os terminais pertinentes no atual estado
        /// </summary>
        /// <param name="D"></param>
        /// <param name="I"></param>
        /// <returns></returns>
        private List <string> PegaTerminais(DClass D, int I)
        {
            List <string> Relevantes = new List <string>();

            foreach (StateStruct State in DList[I - 1].SList)
            {
                if (State.pointer < State.rightSide.Count)
                {
                    if (gramatica.terminais.Contains(State.rightSide[State.pointer]))
                    {
                        Relevantes.Add(State.rightSide[State.pointer]);
                    }
                }
            }

            return(Relevantes);
        }
コード例 #11
0
        public void parseGrammar()
        {
            //Aqui é o principal ponto que eu alterei

            createInitial();
            inputPointer = 1;      //Ele é usado para saber em qual iteração estamos

            bool Terminou = false; //Bool que vai ser usado para parada aleatória


            string Temp;//String temporaria para comportar terminais

            StringBuilder Builder = new StringBuilder();

            Random RNG = new Random();

            do
            {
                Temp    = "";
                success = false;

                DClass D = new DClass();

                List <string> TerminaisUteis = PegaTerminais(D, inputPointer); //contem os terminais a direita do ponto no atual estado do parser

                Temp = TerminaisUteis[RNG.Next() % TerminaisUteis.Count];      //Guarda o terminal aleatório

                Console.Write(Temp + " ");

                scan(D, Temp);//Usa como entrada do parser um terminal aleatório dentro da lista de terminais uteis

                DList.Add(D);

                inputPointer++;                                             //Como não é mais um for e sim um do while eu tinha que botar isso em algum lugar

                checkSuccess();                                             //Verifica se o estado atual é um estado de parada para poder entregar essa frase aleatória

                if (success)                                                //Só verifica se pode sair se for um sucesso(levemente mais eficiente)
                {
                    Terminou = (RNG.Next() % 2 == 0 || inputPointer > 300); //Pega um numero aleatório, se ele for par a variavel de saida é verdadeira
                }
            } while (!(success && Terminou));                               //Verifica se a variavel aleatória permite e saida e se o estado atual nao vai dar merda

            Console.WriteLine("\n");
        }
コード例 #12
0
        private void predict(DClass D)
        {
            bool gotSomething     = false;
            List <StateStruct> ls = D.SList.ToList();

            foreach (StateStruct ss in ls)
            {
                if (ss.pointer < ss.rightSide.Count && gramatica.isVariavel(ss.rightSide[ss.pointer]))
                {
                    gotSomething = predict(D, ss) || gotSomething;

                    //foreach (List<string> regra in gramatica.regrasDeProducao[ss.rightSide[ss.pointer]])
                    //{
                    //    StateStruct DPredictor = new StateStruct(ss.rightSide[ss.pointer], regra, 0, inputPointer);
                    //    D.SList.Add(DPredictor);
                    //    //predict(D, DPredictor);
                    //}
                }
            }
            if (gotSomething)
            {
                predict(D);
            }
        }