示例#1
0
        //private List<Terminal> Primeros(NoTerminal nt)
        //{
        //    List<Terminal> terminales = new List<Terminal>();

        //    List<Produccion> listaIteracion = new List<Produccion>();

        //    List<Produccion> subLista = this.ObtenerListaProduccionesParaUnNoTerminal(nt);

        //    bool primerVuelta = true;

        //    while (primerVuelta || (listaIteracion.Count > 0))
        //    {
        //        if (primerVuelta)
        //        {
        //            primerVuelta = false;
        //        }
        //        else
        //        {
        //            subLista = new List<Produccion>(listaIteracion);
        //        }

        //        foreach (Produccion prod in subLista)
        //        {
        //            listaIteracion = new List<Produccion>();

        //            if (prod.Der != null)
        //            {
        //                ElementoGramatica elem = prod.Der.First();

        //                if (elem.GetType() == typeof(Terminal))
        //                {
        //                    terminales.Add((Terminal)elem);
        //                }
        //                else
        //                {
        //                    listaIteracion.AddRange(this.ObtenerListaProduccionesParaUnNoTerminal((NoTerminal)elem));

        //                }


        //            }
        //            else
        //            {
        //                Terminal t = new Terminal();
        //                t.Componente = new ComponenteLexico();
        //                t.Componente.Token = ComponenteLexico.TokenType.Ninguno;
        //                terminales.Add(t);
        //            }

        //        }
        //    }



        //    return terminales;
        //}

        private List <Terminal> Primeros(NoTerminal nt, Produccion p)
        {
            List <Terminal>   terminales     = new List <Terminal>();
            List <Produccion> listaIteracion = new List <Produccion>();
            List <Produccion> subLista       = new List <Produccion>();

            subLista.Add(p);

            bool primerVuelta = true;

            while (primerVuelta || (listaIteracion.Count > 0))
            {
                if (primerVuelta)
                {
                    primerVuelta = false;
                }
                else
                {
                    subLista = new List <Produccion>(listaIteracion);
                }

                foreach (Produccion prod in subLista)
                {
                    listaIteracion = new List <Produccion>();

                    if (prod.Der != null)
                    {
                        ElementoGramatica elem = prod.Der.First();

                        if (elem.GetType() == typeof(Terminal))
                        {
                            terminales.Add((Terminal)elem);
                        }
                        else
                        {
                            listaIteracion.AddRange(this.ObtenerListaProduccionesParaUnNoTerminal((NoTerminal)elem));
                        }
                    }
                    else
                    {
                        Terminal t = new Terminal();
                        t.Componente       = new ComponenteLexico();
                        t.Componente.Token = ComponenteLexico.TokenType.Ninguno;
                        terminales.Add(t);
                    }
                }
            }
            return(terminales);
        }
示例#2
0
        private bool IgualParteDerecha(Produccion prod)
        {
            if (this.Der.Count != prod.Der.Count)
            {
                return(false);
            }

            bool retorno = true;

            for (int i = 0; i < this.Der.Count; i++)
            {
                if (!this.Der[i].Equals(prod.Der[i]))
                {
                    retorno = false;
                }
            }

            return(retorno);
        }
示例#3
0
        private void ParsearXML(string filePath)
        {
            //if (!System.IO.File.Exists(filePath))
            //    throw new System.IO.FileNotFoundException(String.Format("El archivo \"{0}\" no existe!", filePath));

            try
            {
                //List<Produccion> prods = new List<Produccion>();
                //using (System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(filePath))
                using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(new StringReader(CompiladorGargar.Properties.Resources.Gramatica)))

                {
                    while (reader.Read())
                    {
                        if (reader.NodeType == System.Xml.XmlNodeType.Element)
                        {
                            if (reader.Name.Equals("Produccion"))
                            {
                                System.Xml.XmlDocument doc  = new System.Xml.XmlDocument();
                                System.Xml.XmlNode     node = doc.ReadNode(reader);
                                /*Armado de produccion*/
                                string izquierda = node["Izq"].InnerText;
                                foreach (System.Xml.XmlNode nd in node["Ders"].ChildNodes)
                                {
                                    if (nd.NodeType == System.Xml.XmlNodeType.Element)
                                    {
                                        Produccion p = new Produccion();
                                        p.Izq = new NoTerminal(izquierda);
                                        string[] elementos = nd.InnerText.Split(new char[] { ' ' });
                                        foreach (string el in elementos)
                                        {
                                            try
                                            {
                                                if (esTerminal(el.Trim()))
                                                {
                                                    p.Der.Add(new Terminal(el.Trim()));
                                                }
                                                else
                                                {
                                                    p.Der.Add(new NoTerminal(el.Trim()));
                                                }
                                            }
                                            catch (Exception)
                                            {
                                                throw;
                                            }
                                        }
                                        this.producciones.Add(p);
                                    }
                                }
                            }
                            if (reader.Name.Equals("NoTerminales"))
                            {
                                System.Xml.XmlDocument doc  = new System.Xml.XmlDocument();
                                System.Xml.XmlNode     node = doc.ReadNode(reader);
                                foreach (System.Xml.XmlNode nd in node.ChildNodes)
                                {
                                    if (nd.NodeType == System.Xml.XmlNodeType.Element)
                                    {
                                        string nt = nd.InnerText;
                                        this.noTerminales.Add(new NoTerminal(nt));
                                    }
                                }
                            }
                            if (reader.Name.Equals("SimboloInicial"))
                            {
                                System.Xml.XmlDocument doc  = new System.Xml.XmlDocument();
                                System.Xml.XmlNode     node = doc.ReadNode(reader);
                                string nt = node["NoTerminal"].InnerText;
                                this.simboloInicial = this.EncontrarNoTerminal(nt);
                            }
                        }
                    }
                }
                int count = this.producciones.Count;
            }
            catch (Exception)
            {
                throw;
            }
        }