Exemple #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            LR_0_ au0 = new LR_0_();
            int   h   = 5;
            Point p   = new Point(80, h);

            au0.Genera_Edos();
            List <List <string> > edos = new List <List <string> >(au0.Estados);
            List <string>         Edos = new List <string>(au0.Transiciones);
            string edoAct = "";

            for (int i = 0; i < edos.Count; i++)
            {
                for (int j = 0; j < edos[i].Count; j++)
                {
                    edoAct += edos[i][j] + " ";
                }
                Label label = new Label();
                label.Location = p;
                label.Width    = 1200;
                label.Height   = 15;
                label.Text     = edoAct;
                this.Controls.Add(label);
                edoAct = "";
                p.Y    = h += 13;
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            dataGridView2.Rows.Clear();
            dataGridView2.Columns.Clear();
            LR_0_ au0 = new LR_0_();

            au0.Genera_Edos();
            List <List <string> > edos  = new List <List <string> >(au0.Estados);
            List <string>         trans = new List <string>(au0.Transiciones);
            List <string>         g     = new List <string>(au0.Grama);

            for (int i = 0; i < g.Count; i++)
            {
                if (i == 0)
                {
                    dataGridView2.Columns.Add("Edos", "Edos");
                }

                dataGridView2.Columns.Add(g[i], g[i]);
            }
            for (int i = 0; i < edos.Count; i++)
            {
                dataGridView2.Rows.Add(i.ToString());
            }

            for (int i = 0; i < g.Count; i++)
            {
                for (int j = 0; j < edos.Count; j++)
                {
                    for (int x = 0; x < trans.Count; x++)
                    {
                        if (dataGridView2.Rows[j].Cells[0].Value.ToString() == trans[x][0].ToString())
                        {
                            if (g[i] == trans[x][1].ToString())
                            {
                                dataGridView2.Rows[j].Cells[i + 1].Value = trans[x][2].ToString();
                            }
                        }
                    }
                }
            }
            Form2 f2 = new Form2();

            f2.ShowDialog();

            /*for(int i=0;i<Edos.Count;i++)
             * {
             *  MessageBox.Show(Edos[i]);
             * }*/
        }
Exemple #3
0
        private void button6_Click(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
            LR_0_ lr = new LR_0_();

            lr.Genera_Edos();
            List <string> termi   = new List <string>(lr.Terminales);
            List <string> no_term = new List <string>(lr.NoTerminales);
            List <string> g       = new List <string>(lr.Grama);

            g.Add("$");
            ErroresL = Lexico();
            string[] lineas = txt_Lenguaje.Lines;
            id  = new Regex(textBox4.Text);
            num = new Regex(textBox5.Text);
            int h = 650;

            foreach (Label a in labels)
            {
                this.Controls.Remove(a);
            }
            lineas[lineas.Length - 1] += " $";
            List <string> errores = new List <string>();

            errores.Clear();

            Point      p          = new Point(80, h);
            string     err        = "";
            List <int> lineaError = new List <int>();

            for (int i = 0; i < lineas.Length; i++)
            {
                foreach (string a in ErroresL)
                {
                    if (lineas[i].Contains(a))
                    {
                        if (!errores.Contains("Error en la linea " + i.ToString() + " no se reconoce " + a))
                        {
                            errores.Add("Error en la linea " + (i + 1).ToString() + " no se reconoce " + a);
                            lineaError.Add(i);
                        }
                    }
                }
            }

            List <string> gramat = new List <string>(lr.Grmatica);


            foreach (string a in errores)
            {
                Label label = new Label();
                label.Location  = new Point(80, h);
                label.ForeColor = Color.Red;
                label.Width     = 300;
                label.Height    = 15;
                label.Text      = a;
                this.Controls.Add(label);
                labels.Add(label);
                h += 15;
            }
            Error.Visible = false;
            string[] linea;
            List <List <string> > arbol    = new List <List <string> >();
            List <string>         arbolaux = new List <string>();
            int             index          = 0;
            string          auxres         = "";
            string          elemento;
            bool            band = false;
            int             s = 0;
            string          res = "", auxnodo = "";
            int             t      = 0;
            List <string>   w      = new List <string>();
            List <string>   nodos  = new List <string>();
            Stack <int>     pila   = new Stack <int>();
            List <TreeNode> nodosA = new List <TreeNode>();
            List <TreeNode> auxA   = new List <TreeNode>();
            TreeNode        padre  = new TreeNode();

            pila.Push(0);
            for (int i = 0; i < lineas.Length; i++)
            {
                if (lineas[i].Contains(";"))
                {
                    index = lineas[i].IndexOf(';');
                    if (lineas[i][index - 1] != ' ')
                    {
                        lineas[i] = lineas[i].Insert(index, " ");
                    }
                }
                if (lineas[i].Length > 1)
                {
                    while (lineas[i][0] == ' ')
                    {
                        if (lineas[i].Length == 1)
                        {
                            break;
                        }
                        lineas[i] = lineas[i].Remove(0, 1);
                    }
                }
                linea = lineas[i].Split(' ');
                for (int j = 0; j < linea.Length; j++)
                {
                    w.Add(linea[j]);
                }
            }
            if (ErroresL.Count == 0)
            {
                for (int j = 0; j < w.Count; j++)
                {
                    elemento = w[j];
                    if (!g.Contains(elemento))
                    {
                        if (id.IsMatch(elemento) && !ErroresL.Contains(elemento))
                        {
                            elemento = "identificador";
                        }
                        else
                        {
                            if (num.IsMatch(elemento) && !ErroresL.Contains(elemento))
                            {
                                elemento = "numero";
                            }
                            else
                            {
                                Error.Visible = true;
                                break;
                            }
                        }
                    }

                    if (dataGridView3.Rows[s].Cells[(g.IndexOf(elemento) + 1)].Value != null)
                    {
                        res = dataGridView3.Rows[s].Cells[(g.IndexOf(elemento) + 1)].Value.ToString();
                    }
                    else
                    {
                        Error.Visible = true;
                        Invalidate();

                        break;
                    }
                    if (res.Contains("d"))
                    {
                        string pilita = res.Remove(0, 1);
                        pila.Push(Int32.Parse(pilita));
                        s = Int32.Parse(pilita);
                        nodosA.Add(new TreeNode(elemento));
                        nodos.Add(elemento);
                    }
                    else
                    {
                        if (res.Contains("r"))
                        {
                            while (res.Contains("r"))
                            {
                                res = res.Remove(0, 1);
                                string gra    = gramat[Int32.Parse(res)];
                                string aux    = "";
                                string auxini = "";
                                for (int c = 0; c < gramat[Int32.Parse(res)].Length; c++)
                                {
                                    if (c < gramat[Int32.Parse(res)].IndexOf('>'))
                                    {
                                        auxini += gramat[Int32.Parse(res)][c];
                                    }
                                    if (c > gramat[Int32.Parse(res)].IndexOf('>') - 1)
                                    {
                                        aux += gramat[Int32.Parse(res)][c];
                                    }
                                }
                                auxini = auxini.Remove((auxini.Length - 1), 1);
                                padre  = new TreeNode(auxini, nodosA.ToArray());
                                if (auxini == "secuencia-sent")
                                {
                                    if (!auxA.Contains(padre))
                                    {
                                        auxA.Add(padre);
                                    }
                                }
                                nodosA.Clear();
                                nodosA.Add(padre);

                                string[] nums = aux.Split(' ');
                                if (pila.Count >= nums.Length)
                                {
                                    for (int c = 0; c < nums.Length; c++)
                                    {
                                        pila.Pop();
                                    }
                                }
                                if (pila.Count > 0)
                                {
                                    t = pila.Pop();
                                    pila.Push(t);
                                }
                                if (dataGridView2.Rows[t].Cells[(g.IndexOf(auxini) + 1)].Value != null)
                                {
                                    res = dataGridView2.Rows[t].Cells[(g.IndexOf(auxini) + 1)].Value.ToString();
                                    pila.Push(Int32.Parse(res));
                                }
                                else
                                {
                                    Invalidate();
                                    Error.Visible = true;
                                    break;
                                }


                                if (dataGridView3.Rows[Int32.Parse(res)].Cells[(g.IndexOf(elemento) + 1)].Value != null)
                                {
                                    res = dataGridView3.Rows[Int32.Parse(res)].Cells[(g.IndexOf(elemento) + 1)].Value.ToString();
                                }
                                else
                                {
                                    Error.Visible = true;
                                    break;
                                }
                            }
                            if (res.Length > 1 && !res.Contains("ac"))
                            {
                                res = res.Remove(0, 1);
                                s   = Int32.Parse(res);
                                pila.Push(s);
                            }
                            else
                            {
                                if (res.Contains("ac"))
                                {
                                    MessageBox.Show("Cadena aceptada");
                                    nodosA[0].Nodes.Clear();
                                    padre = nodosA[0];
                                    TreeNode hijoP = new TreeNode("secuencia-sent", auxA.ToArray());
                                    treeView1.Nodes.Add(padre);
                                    treeView1.Nodes[0].Nodes.Add(hijoP);
                                }
                            }
                        }
                    }
                }
            }

            treeView1.ExpandAll();
        }
Exemple #4
0
        private void button5_Click(object sender, EventArgs e)
        {
            dataGridView2.Rows.Clear();
            dataGridView2.Columns.Clear();
            dataGridView3.Rows.Clear();
            dataGridView3.Columns.Clear();
            LR_0_         au0  = new LR_0_();
            List <string> nume = new List <string>();

            nume.Add("1");
            nume.Add("2");
            nume.Add("3");
            nume.Add("4");
            nume.Add("5");
            nume.Add("6");
            nume.Add("7");
            nume.Add("8");
            nume.Add("9");
            nume.Add("0");
            au0.Genera_Edos();
            List <List <string> > edos = new List <List <string> >(au0.Estados);

            // for (int i = 0; i < edos.Count; i++)
            //   for(int j=0;j<edos[i].Count;j++)
            //MessageBox.Show(edos.Count.ToString());
            List <string> trans = new List <string>(au0.Transiciones);
            List <string> g     = new List <string>(au0.Grama);

            g.Add("$");
            au0.TablaanalisisSintactico();
            List <string> tabla = new List <string>(au0.Tabla);
            string        ini = "", gra = "", fin = "";

            for (int i = 0; i < g.Count; i++)
            {
                if (i == 0)
                {
                    dataGridView2.Columns.Add("Edos", "Edos");
                    dataGridView3.Columns.Add("Edos", "Edos");
                }

                dataGridView2.Columns.Add(g[i], g[i]);
            }
            for (int i = 0; i < edos.Count; i++)
            {
                dataGridView2.Rows.Add(i.ToString());
                dataGridView3.Rows.Add(i.ToString());
            }
            string aux = "", auxin = "";

            for (int i = 0; i < g.Count; i++)
            {
                dataGridView3.Columns.Add(g[i].ToString(), g[i].ToString());
            }

            for (int i = 0; i < g.Count; i++)
            {
                for (int j = 0; j < edos.Count; j++)
                {
                    for (int x = 0; x < trans.Count; x++)
                    {
                        ini = ""; gra = ""; fin = "";
                        for (int z = 0; z < trans[x].Length; z++)
                        {
                            if (nume.Contains(trans[x][z].ToString()))
                            {
                                if (z == 0 || z == 1)
                                {
                                    ini += trans[x][z];
                                }
                                else
                                if (z == trans[x].Length - 1 || z == trans[x].Length - 2)
                                {
                                    fin += trans[x][z];
                                }
                            }
                            else
                            {
                                gra += trans[x][z];
                            }
                        }
                        if (dataGridView2.Rows[j].Cells[0].Value.ToString() == ini)
                        {
                            if (g[i] == gra)
                            {
                                dataGridView2.Rows[j].Cells[i + 1].Value = fin;
                            }
                        }
                        else
                        {
                            //dataGridView2.Rows[j].Cells[i + 1].Value = "ɸ";
                        }
                    }
                }
            }
            int cont = 0;

            for (int i = 0; i < g.Count; i++)
            {
                for (int j = 0; j < edos.Count; j++)
                {
                    for (int x = 0; x < tabla.Count; x++)
                    {
                        ini  = ""; gra = ""; fin = "";
                        cont = 0;
                        for (int z = 0; z < tabla[x].Length; z++)
                        {
                            if (tabla[x][z] != ' ' && cont == 0)
                            {
                                ini += tabla[x][z];
                            }
                            else
                            {
                                if (cont == 0 && tabla[x][z] == ' ')
                                {
                                    cont++;
                                }
                                else
                                {
                                    if (tabla[x][z] != ' ' && cont == 1)
                                    {
                                        gra += tabla[x][z];
                                    }
                                    else
                                    {
                                        if (cont == 1 && tabla[x][z] == ' ')
                                        {
                                            cont++;
                                        }
                                        else
                                        {
                                            if (cont == 2)
                                            {
                                                fin += tabla[x][z];
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (dataGridView3.Rows[j].Cells[0].Value.ToString() == ini)
                        {
                            if (g[i] == gra)
                            {
                                dataGridView3.Rows[j].Cells[i + 1].Value = fin;
                            }
                        }
                    }
                }
            }
            Form2 f2 = new Form2();

            f2.ShowDialog();
        }