Example #1
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;
            }
        }