Beispiel #1
0
        static void Main(string[] args)
        {
            InputOutput      fileHandler      = new InputOutput();
            GrammarConverter grammarConverter = new GrammarConverter();

            fileHandler.ExtractGrammarData(ref grammarConverter);
            grammarConverter.InitializeStartRule();

            Factorizator factorizator = new Factorizator();

            factorizator.ApplyFactorization(ref grammarConverter);

            LeftRecursion leftRecursion = new LeftRecursion();

            leftRecursion.ApplyLeftRecursion(ref grammarConverter);

            grammarConverter.DefineGuideSet();

            //grammarConverter.PrintTerminalList();
            grammarConverter.PrintGuideSet();
            grammarConverter.PrintGrammarList();
            //grammarConverter.PrintIndex();

            List <RowInTable> table = grammarConverter.CreateTable();
            //grammarConverter.PrintTable();

            Runner runner = new Runner();

            runner.ProcessSequenceFromFile(ref table);

            Console.ReadKey();
        }
Beispiel #2
0
        private void DeleteLeftRecursion(int index, string currRuleName, ref GrammarConverter grammarConverter)
        {
            List <Rule>            grammarList         = grammarConverter.GetGrammarList();
            List <IndexOfTerminal> currIndexOfTerminal = grammarConverter.GetIndexOfTerminalList();
            IndexOfTerminal        currRuleList        = currIndexOfTerminal.Find(x => x.terminal == currRuleName);
            List <int>             rowList             = currRuleList.rowIndex;
            string newRule = "<" + RULE_NAME + ruleIndex + ">";

            Rule rule;

            foreach (int row in rowList)
            {
                if (row != index)
                {
                    rule                 = new Rule();
                    rule.ruleName        = currRuleName;
                    rule.guideSet        = new List <string>();
                    rule.ruleСomposition = new List <string>();

                    foreach (string element in grammarList[row].ruleСomposition)
                    {
                        if (element != "[empty]")
                        {
                            rule.ruleСomposition.Add(element);
                        }
                    }

                    rule.ruleСomposition.Add(newRule);

                    newGrammarList.Add(rule);
                }
            }

            rule                 = new Rule();
            rule.ruleName        = newRule;
            rule.guideSet        = new List <string>();
            rule.ruleСomposition = new List <string> {
                "[empty]"
            };
            newGrammarList.Add(rule);

            rule                 = new Rule();
            rule.ruleName        = newRule;
            rule.guideSet        = new List <string>();
            rule.ruleСomposition = new List <string> ();

            List <string> composition = grammarList[index].ruleСomposition;

            for (int i = 1; i < composition.Count(); i++)
            {
                rule.ruleСomposition.Add(composition[i]);
            }

            rule.ruleСomposition.Add(newRule);
            newGrammarList.Add(rule);

            ruleIndex++;
        }
Beispiel #3
0
        public void ExtractGrammarData(ref GrammarConverter grammarConverter)
        {
            System.IO.StreamReader file = new System.IO.StreamReader("test_3.txt");

            string line;
            int    rawIndex  = 0;
            int    wordIndex = START_RULE_ELEMENT_AMOUNT;

            while ((line = file.ReadLine()) != null)
            {
                rawIndex++;
                string[] words = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                List <Rule>            grammarList         = grammarConverter.GetGrammarList();
                List <TerminalList>    terminalList        = grammarConverter.GetTerminalList();
                List <IndexOfTerminal> indexOfTerminalList = grammarConverter.GetIndexOfTerminalList();

                PutDataToStracture(words, ref grammarList);
                AddComponentsToTerminalList(words, rawIndex, ref terminalList);
                UpdateTerminalIndexList(words[0], wordIndex, rawIndex, ref indexOfTerminalList);
                wordIndex += words.Count() - START_RULE_ELEMENT_AMOUNT;
            }
        }
Beispiel #4
0
        private void AddRulesWithoutRecursion(ref GrammarConverter grammarConverter)
        {
            List <IndexOfTerminal> indexOfTerminal = grammarConverter.GetIndexOfTerminalList();
            List <Rule>            grammarList     = grammarConverter.GetGrammarList();

            foreach (IndexOfTerminal terminalInfo in indexOfTerminal)
            {
                if (!rulesInNewGrammar.Contains(terminalInfo.terminal))
                {
                    List <int> list = terminalInfo.rowIndex;
                    foreach (int index in list)
                    {
                        if (terminalInfo.terminal == "<Start>")
                        {
                            newGrammarList.Insert(0, grammarList[index]);
                        }
                        else
                        {
                            newGrammarList.Add(grammarList[index]);
                        }
                    }
                }
            }
        }
Beispiel #5
0
        public void ApplyLeftRecursion(ref GrammarConverter grammarConverter)
        {
            List <Rule> grammarList = grammarConverter.GetGrammarList();

            for (int i = 0; i < grammarList.Count(); i++)
            {
                Rule   rule           = grammarList[i];
                string ruleName       = rule.ruleName;
                string firstComponent = rule.ruleСomposition[0];

                if (ruleName == firstComponent)
                {
                    rulesInNewGrammar.Add(ruleName);
                    DeleteLeftRecursion(i, ruleName, ref grammarConverter);
                }
            }

            AddRulesWithoutRecursion(ref grammarConverter);
            grammarConverter.SetGrammarList(newGrammarList);
            UpdateTerminalIndexList();
            grammarConverter.SetTerminalIndexList(newIndexOfTerminal);
            UpdateTerminalList();
            grammarConverter.SetTerminalList(newTerminalList);
        }