Ejemplo n.º 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text = "";
            sPrograma     = this.textBox1.Text;
            var entrada = sPrograma + Environment.NewLine;

            byte[]             byteArray  = Encoding.ASCII.GetBytes(entrada);
            MemoryStream       stream     = new MemoryStream(byteArray);
            var                parametro1 = new AntlrInputStream(entrada);
            SIC_gramaticaLexer lex        = new SIC_gramaticaLexer(parametro1);

            //CREAMOS UN LEXER CON LA CADENA QUE ESCRIBIO EL USUARIO
            Antlr4.Runtime.CommonTokenStream tokens = new Antlr4.Runtime.CommonTokenStream(lex);
            //CREAMOS LOS TOKENS SEGUN EL LEXER CREADO
            SIC_gramaticaParser parser = new SIC_gramaticaParser(tokens);

            //CREAMOS EL PARSER CON LOS TOKENS CREADOS
            try
            {
                //parser.prog();
                this.separarPrograma();
                this.separarEtiquetas();
                this.separarInstrucciones();
                this.separDirecciones();
                //this.separDirecciones();
                this.buscarErrores();

                this.crearArchivo();
            }
            catch (Exception error)
            {
                //Console.Error.WriteLine(e.StackTrace);
                MessageBox.Show("A ocurrido un error inesperado " + error.Message);
            }
        }
        public void compila(string directorio)
        {
            bandErrors = false;
            string              nombre;
            AntlrFileStream     antFile;
            SIC_gramaticaLexer  lex;
            SIC_gramaticaParser parser;
            CommonTokenStream   tokens;
            FileStream          stream = null;
            StreamWriter        writer = null;

            nombre = new FileInfo(directorio).Name;
            string nombre2 = nombre.Replace(".s", "");

            basePath = Environment.CurrentDirectory;

            path = @basePath + nombre2 + "Error.t";

            try
            {
                //crear archivo
                stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                writer = new StreamWriter(stream);
                Console.SetError(writer);
                antFile = new AntlrFileStream(directorio);
                lex     = new SIC_gramaticaLexer(antFile);
                tokens  = new CommonTokenStream(lex);
                parser  = new SIC_gramaticaParser(tokens);

                parser.prog();

                writer.Close();
                stream.Close();

                if (parser.NumberOfSyntaxErrors != 0)
                {
                    bandErrors = true;
                }

                if (!bandErrors)
                {
                    File.Delete(path);
                    richTextBox1.Text = "El archivo esta correcto";
                }
                else
                {
                    using (StreamReader sr = new StreamReader(path))
                    {
                        richTextBox1.Text = sr.ReadToEnd();
                        sr.Close();
                    }
                }
            }
            catch
            {
                writer.Close();
                stream.Close();
                File.Delete(path);

                Console.WriteLine("**No se encuentra el archivo**");
            }
        }