Esempio n. 1
0
        public Form1()
        {
            InitializeComponent();
            grid.RowHeadersWidth = 50;
            var listOfRules = new List <string>();

            listEntrada = "n v n w n".Split(' ').ToList();

            rm = new RuleManager();
            //rm.Rules.Add(rm.CreateTextRule("E->TP"));
            //rm.Rules.Add(rm.CreateTextRule("P->vTP/?"));
            //rm.Rules.Add(rm.CreateTextRule("T->FO"));
            //rm.Rules.Add(rm.CreateTextRule("O->&FO/?"));
            //rm.Rules.Add(rm.CreateTextRule("F->~F/i"));

            listOfRules.Add("A->BPC");
            listOfRules.Add("P->vBPC/?");
            listOfRules.Add("B->CR");
            listOfRules.Add("R->wCR/?");
            listOfRules.Add("C->n");

            //rm.Rules.Add(rm.CreateTextRule("A->BPC"));
            //rm.Rules.Add(rm.CreateTextRule("P->vBPC"));
            ////rm.Rules.Add(rm.CreateTextRule("P->?"));
            //rm.Rules.Add(rm.CreateTextRule("P->vBPC/?"));
            //rm.Rules.Add(rm.CreateTextRule("B->CR"));
            //rm.Rules.Add(rm.CreateTextRule("R->wCR/?"));
            //rm.Rules.Add(rm.CreateTextRule("R->wCR"));
            ////rm.Rules.Add(rm.CreateTextRule("R->?"));
            //rm.Rules.Add(rm.CreateTextRule("C->n"));

            foreach (var rule in listOfRules)
            {
                rm.Rules.Add(rm.CreateTextRule(rule));
                if (rule.Contains("/?"))
                {
                    var separated = rule.Split(new[] { "->" }, StringSplitOptions.None)[1].Split('/')[0];
                    rm.Rules.Add(rm.CreateTextRule($"{rule.Select(x => x).FirstOrDefault()}->{separated}"));
                    rm.Rules.Add(rm.CreateTextRule($"{rule.Select(x=> x).FirstOrDefault()}->?"));
                }
            }

            listOfRules.ForEach(x => lblProduction.Text += x.ToString() + Environment.NewLine);

            foreach (Rule rule in rm.Rules)
            {
                Console.Write($"{rule,-15}");
                Console.Write("{0, -15}", string.Join(",", rm.First(rule.Definitions)));
                Console.Write("{0, -15}", string.Join(",", rm.Follow(rule)));
                Console.WriteLine();
            }

            var listOfNames = rm.Rules.Select(x => x.Name).Distinct().ToList();
            var terminais   = new List <string>();

            foreach (var rule in rm.Rules)
            {
                var segments = rule.ToString().Split(new[] { "->" }, StringSplitOptions.None);
                var ruleText = segments[1];
                terminais.Add(ruleText);
            }

            var listOfChars = new List <string>();

            terminais.ForEach(prod => listOfChars.AddRange(prod.Select(c => c.ToString())));

            var naoTerminais = listOfChars
                               .Where(x => !listOfNames.Contains(x.ToString()) && x != "?" && x != "/")
                               .Distinct()
                               .ToList();

            naoTerminais.Add("$");

            foreach (var naoTerminal in naoTerminais)
            {
                grid.Columns.Add(naoTerminal.ToString(), naoTerminal.ToString());
            }

            int i = 0;

            foreach (var item in listOfNames)
            {
                grid.Rows.Add();
                grid.Rows[i].HeaderCell.Value = item;
                i++;
            }

            foreach (var rule in rm.Rules)
            {
                var firsts  = rm.First(rule.Definitions).Select(x => x.Name).ToList();
                var follows = rm.Follow(rule).Select(x => x.Name).ToList();
                var teste   = rm.Follow(rule);

                foreach (DataGridViewRow row in grid.Rows)
                {
                    if (row.HeaderCell.Value?.ToString() == rule.Name)
                    {
                        if (rule.Empty != null && !string.IsNullOrEmpty(rule.Empty))
                        {
                            foreach (var follow in follows)
                            {
                                foreach (DataGridViewColumn col in grid.Columns)
                                {
                                    if (col.HeaderCell.Value?.ToString() == follow)
                                    {
                                        grid[col.Index, row.Index].Value = rule.Name + $"->{rule.Empty} (follow)";
                                    }
                                }
                            }
                        }

                        if (rule.NotEmpty != null && !string.IsNullOrEmpty(rule.NotEmpty))
                        {
                            foreach (var first in firsts)
                            {
                                foreach (DataGridViewColumn col in grid.Columns)
                                {
                                    if (col.HeaderCell.Value?.ToString() == first)
                                    {
                                        grid[col.Index, row.Index].Value = rule.Name + $"->{rule.NotEmpty} (first)";
                                    }
                                }
                            }
                        }
                    }
                }
            }

            gridAnalise.Columns.Add("Pilha", "Pilha");
            gridAnalise.Columns.Add("Entrada", "Entrada");
            gridAnalise.Columns.Add("Ação", "Ação");

            pilha = new List <string>()
            {
                listOfNames.First()
            };
        }