Exemple #1
0
        private void UpdateTerminalList()
        {
            for (int i = 0; i < newGrammarList.Count; i++)
            {
                Rule          currRule    = newGrammarList[i];
                List <string> composition = currRule.ruleСomposition;

                foreach (string element in composition)
                {
                    bool isTerminal = (element[0] == '<') && (element[element.Length - 1] == '>');
                    if (isTerminal)
                    {
                        bool isExist = newTerminalList.Exists(x => x.terminal == element);
                        if (!isExist)
                        {
                            TerminalList newTerninal;
                            newTerninal.terminal = element;
                            newTerninal.index    = new List <int>();
                            newTerninal.index.Add(i);
                            newTerminalList.Add(newTerninal);
                        }
                        else
                        {
                            TerminalList currTerminalList = newTerminalList.Find(x => x.terminal.Contains(element));
                            currTerminalList.index.Add(i);
                        }
                    }
                }
            }
        }
        private void FindTerminalSetWithEnd(string terminal, ref List <string> currSet, ref List <string> guideSetForRule, ref bool isFirstRule)
        {
            bool isExist = terminalList.Exists(x => x.terminal == terminal);

            if (isExist)
            {
                TerminalList currTerminalList = terminalList.Find(x => x.terminal.Contains(terminal));
                int          indexAmount      = currTerminalList.index.Count();

                for (int i = 0; i < indexAmount; i++)
                {
                    int           currIndex       = currTerminalList.index[i];
                    Rule          currRule        = grammarList[currIndex];
                    List <string> ruleСomposition = currRule.ruleСomposition;
                    int           elementIndex    = ruleСomposition.FindIndex(x => x == terminal);

                    if (elementIndex < ruleСomposition.Count() - 1)
                    {
                        string nextElement     = ruleСomposition[elementIndex + 1];
                        string newtElementType = DefineStringType(nextElement);

                        bool isEndInSet = false;

                        switch (newtElementType)
                        {
                        case TERMINAL:
                            bool isExist1 = guideSet.Exists(x => x.ruleName == nextElement);
                            if (isExist1)
                            {
                                GuideSet currGuideSet = guideSet.Find(x => x.ruleName == nextElement);

                                foreach (string setElement in currGuideSet.set)
                                {
                                    if (setElement == END)
                                    {
                                        isEndInSet = true;
                                    }

                                    AddNotTerminalToList(setElement, ref currSet);
                                    AddNotTerminalToList(setElement, ref guideSetForRule);
                                }
                            }

                            if (isEndInSet)
                            {
                                FindTerminalSetWithEnd(nextElement, ref currSet, ref guideSetForRule, ref isFirstRule);
                            }

                            break;

                        case NOT_TERMINAL:

                            AddNotTerminalToList(nextElement, ref currSet);
                            AddNotTerminalToList(nextElement, ref guideSetForRule);
                            break;
                        }
                    }
                }
            }
        }
        public void PrintTerminalList()
        {
            int amount1 = terminalList.Count;

            Console.WriteLine("Printing Terminal List...");
            Console.WriteLine("--benig");

            for (int i = 0; i < amount1; i++)
            {
                TerminalList t1 = terminalList[i];
                Console.Write(t1.terminal);
                Console.Write(" : ");

                int amount2 = t1.index.Count;

                for (int j = 0; j < amount2; j++)
                {
                    Console.Write(t1.index[j]);
                    Console.Write(", ");
                }

                Console.WriteLine();
            }

            Console.WriteLine("--end");
            Console.WriteLine();
        }
Exemple #4
0
        private void AddComponentsToTerminalList(string[] words, int rawIndex, ref List <TerminalList> terminalList)
        {
            int amount = words.Length;

            for (int i = 1; i < amount; i++)
            {
                string currWord   = words[i];
                bool   isTerminal = (currWord[0] == '<') && (currWord[currWord.Length - 1] == '>');
                if (isTerminal)
                {
                    bool isExist = terminalList.Exists(x => x.terminal.Contains(currWord));
                    if (!isExist)
                    {
                        TerminalList newTerninal;
                        newTerninal.terminal = currWord;
                        newTerninal.index    = new List <int>();
                        newTerninal.index.Add(rawIndex + 1);
                        terminalList.Add(newTerninal);
                    }
                    else
                    {
                        TerminalList currTerminalList = terminalList.Find(x => x.terminal.Contains(currWord));
                        currTerminalList.index.Add(rawIndex + 1);
                    }
                }
            }
        }
        private void FindEmptyTerminalSet(string terminal, ref List <string> currSet,
                                          ref List <string> guideSetForRule, ref bool isFirstRule)
        {
            TerminalList currTerminalList = terminalList.Find(x => x.terminal.Contains(terminal));
            int          indexAmount      = currTerminalList.index.Count();

            for (int i = 0; i < indexAmount; i++)
            {
                int           currIndex       = currTerminalList.index[i];
                Rule          currRule        = grammarList[currIndex];
                List <string> ruleСomposition = currRule.ruleСomposition;
                int           elementIndex    = ruleСomposition.FindIndex(x => x == terminal);

                if (elementIndex < ruleСomposition.Count() - 1)
                {
                    string nextElement     = ruleСomposition[elementIndex + 1];
                    string newtElementType = DefineStringType(nextElement);

                    switch (newtElementType)
                    {
                    case TERMINAL:
                        bool isNeedToCheckNextSymbol = false;
                        FindTerminalSet(nextElement, ref currSet, ref guideSetForRule, ref isNeedToCheckNextSymbol);
                        break;

                    case NOT_TERMINAL:
                        AddNotTerminalToList(nextElement, ref currSet);
                        AddNotTerminalToList(nextElement, ref guideSetForRule);
                        if (isFirstRule)
                        {
                            AddNotTerminalToList(END, ref currSet);
                            AddNotTerminalToList(END, ref guideSetForRule);
                            isFirstRule = false;
                        }
                        break;
                    }
                }
                else
                {
                    string rule = currRule.ruleName;

                    if (rule != terminal)
                    {
                        FindEmptyTerminalSet(rule, ref currSet, ref guideSetForRule, ref isFirstRule);
                    }
                    else
                    {
                        AddNotTerminalToList(END, ref currSet);
                        AddNotTerminalToList(END, ref guideSetForRule);
                    }
                }
            }
        }
        public void InitializeStartRule()
        {
            string terminal = grammarList[0].ruleName;

            Rule newRule;

            newRule.ruleName = "<Start>";

            List <string> currComposition = new List <string>();

            currComposition.Add(terminal);
            currComposition.Add("[end]");
            newRule.ruleСomposition = currComposition;
            newRule.guideSet        = new List <string>();

            bool isExist = terminalList.Exists(x => x.terminal == terminal);

            if (isExist)
            {
                TerminalList currTerminalList = terminalList.Find(x => x.terminal.Contains(terminal));
                currTerminalList.index.Add(1);
            }
            else
            {
                TerminalList newTerminalList;
                newTerminalList.terminal = terminal;
                newTerminalList.index    = new List <int>()
                {
                    1
                };
                terminalList.Add(newTerminalList);
            }

            IndexOfTerminal startIndexOfTerminal = new IndexOfTerminal();

            startIndexOfTerminal.terminal = newRule.ruleName;
            startIndexOfTerminal.rowIndex = new List <int>()
            {
                0
            };
            startIndexOfTerminal.startIndex = new List <int>()
            {
                0
            };
            indexOfTerminalList.Insert(0, startIndexOfTerminal);

            grammarList.Insert(0, newRule);
        }