예제 #1
0
        public List <string> ParsingFile(string file)
        {
            var input  = new AntlrFileStream(file);
            var lexer  = new CoolLexer(input);
            var errors = new List <string>();

            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new LexerErrorListener(errors));

            tokens = new CommonTokenStream(lexer);
            parser = new CoolParser(tokens);

            parser.RemoveErrorListeners();
            parser.AddErrorListener(new ParserErrorListener(errors));

            tree = parser.program();

            return(errors);
        }
예제 #2
0
        private static ASTNode ParseInput(string inputPath)
        {
            //try
            {
                var input = new AntlrFileStream(inputPath);
                var lexer = new CoolLexer(input);

                var errors = new List <string>();
                lexer.RemoveErrorListeners();
                lexer.AddErrorListener(new LexerErrorListener(errors));

                var tokens = new CommonTokenStream(lexer);

                var parser = new CoolParser(tokens);

                parser.RemoveErrorListeners();
                parser.AddErrorListener(new ParserErrorListener(errors));

                IParseTree tree = parser.program();

                if (errors.Any())
                {
                    Console.WriteLine();
                    foreach (var item in errors)
                    {
                        Console.WriteLine(item);
                    }
                    return(null);
                }

                var     astBuilder = new ASTBuilder();
                ASTNode ast        = astBuilder.Visit(tree);
                return(ast);
            }

            /*catch (Exception e)
             * {
             *
             *  Console.WriteLine(e.Message);
             *  return null;
             * }*/
        }
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                return;
            }
            var input = new AntlrFileStream(args[0]);
            var lexer = new CoolLexer(input);

            var tokens = new CommonTokenStream(lexer);
            var parser = new CoolParser(tokens);

            var tree = parser.program();

            Context_to_AST astbuilder = new Context_to_AST();
            ProgNode       program    = astbuilder.VisitProgram(tree) as ProgNode;

            var scope = new Scope();

            Console.WriteLine(CheckSemantic(program, scope));
            Console.WriteLine("\n");

            var code_visitor = new CodeGenVisitor(program, scope);
            var code         = code_visitor.GetIntermediateCode();

            foreach (var line in code)
            {
                Console.WriteLine(line);
            }

            MIPSCodeGenerator generator = new MIPSCodeGenerator();
            var mips = generator.GenerateCode(code);

            Console.WriteLine(mips);

            File.WriteAllText("result.mips", mips);
        }
예제 #4
0
        static void Main(string[] args)
        {
            #region Variables

            double version   = 0.2;
            string copyright = "David Clavijo Castro & Rafael Martinex Martinez";
            var    verbose   = true;
            string usage     = "Usage:\n\tcoolc [--verbose/--v] [ -o fileout ] file1.cl file2.cl ... filen.cl";
            #endregion

            #region Welcome Message
            ////// ////// Welcome Message   ////// //////

            #endregion

            #region Open file
            ////// ////// Open file         ////// //////

            if (args.Length == 0)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("ERROR: No arguments given ");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine(usage);
                Environment.Exit(1);
                return;
            }

            string       exitname   = args[0];
            StreamWriter tempwriter = new StreamWriter(args[0] + ".tempread.txt");
            for (int i = 0; i < args.Count() - 1; i++)
            {
                if (args[i].Contains("-v") || args[i].Contains("--verbose"))
                {
                    verbose = true;
                }
                else
                {
                    try
                    {
                        if (args[i].Contains("-o"))
                        {
                            string[] sp = args[i].Split(' '); exitname = sp[1];
                        }
                        else
                        {
                            try
                            {
                                StreamReader file_s = new StreamReader(args[i]);
                                tempwriter.WriteLine(file_s.ReadToEnd() + "\n");
                            }
                            catch (Exception)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine("ERROR: Opening file {0}", args[i]);
                                Console.ForegroundColor = ConsoleColor.Gray;

                                Environment.Exit(1);
                                return;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Mala Entrada de argumentos en {0}", args[i]);
                        Console.ForegroundColor = ConsoleColor.Gray;

                        Console.WriteLine(usage);
                        Environment.Exit(1);
                    }
                }
            }

            if (verbose == true)
            {
                Console.ForegroundColor = ConsoleColor.DarkCyan;
                Console.WriteLine("A cool compiler :)");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine("Version: {0}", version);
                Console.WriteLine("Copyright (C) 2018-2019: {0}", copyright);
            }


            Console.WriteLine("Opening: {0}\t", "E:\\coolDavid y Rafael\\mytest\\test_code.cl");
            AntlrFileStream input_file = new AntlrFileStream(args[0] + ".tempread.txt");
            #endregion

            #region Parse
            ////// ////// Parse             ////// //////
            //Error Logerss

            StreamWriter lexerwriter  = new StreamWriter(input_file.SourceName + ".lexer.errors.txt");
            StreamWriter parserwriter = new StreamWriter(input_file.SourceName + ".parser.errors.txt");


            CoolLexer         lexer  = new CoolLexer(input_file, lexerwriter, lexerwriter);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            CoolParser        parser = new CoolParser(tokens, parserwriter, parserwriter);



            //Console.ForegroundColor = ConsoleColor.Yellow;
            //IParseTree tree = parser.program(); //si aqui pongo ParseTree da palo
            //CoolBaseVisitor<bool> visitor = new CoolBaseVisitor<bool>();
            /*Chequeo de errores en logger*/
            //var myerror = new BaseErrorListener();
            //parser.AddErrorListener(myerror);
            IParseTree tree = parser.program();

            lexerwriter.Close();
            parserwriter.Close();

            //Imprimir en consola los errores de parsear o lexear
            if (parser.NumberOfSyntaxErrors > 0)
            {
                //Console.WriteLine("{0,20}","[Parsing Error]");
                Console.WriteLine();
                StreamReader lexread   = new StreamReader(input_file.SourceName + ".lexer.errors.txt");
                StreamReader parseread = new StreamReader(input_file.SourceName + ".parser.errors.txt");

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Compiler Lexer Errors:");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(lexread.ReadToEnd());
                lexread.Close();

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("compiler Parser Errors:");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(parseread.ReadToEnd());
                parseread.Close();
                Console.ForegroundColor = ConsoleColor.Gray;

                Environment.Exit(1);
            }
            Console.ForegroundColor = ConsoleColor.Gray;
            //Devolver el color de la consola



            //parser.ErrorListeners.RemoveAt(0);



            Console.ForegroundColor = ConsoleColor.Gray;

            #endregion

            #region Semantics Check
            // My_visitor semantic = new My_visitor();
            VisitASTFactory semantic = new VisitASTFactory();

            ProgramSemantic ast = (ProgramSemantic)semantic.Visit(tree);



            if (ast.Errors.Count > 0 || ast.CheckSemantics().Count > 0)
            {
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Semantic errors: ");
                Console.ForegroundColor = ConsoleColor.Yellow;

                foreach (Error e in ast.Errors)
                {
                    Console.WriteLine(e);
                }
                Console.ForegroundColor = ConsoleColor.Gray;

                //File.Delete(args[0] + ".lexer.errors.txt");
                //File.Delete(args[0] + ".parser.errors.txt");

                Environment.Exit(1);
                //Console.WriteLine();
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("No semantic Errors so far \t");
                Console.ForegroundColor = ConsoleColor.Gray;
            }



            #endregion

            #region Generate Code
            ////// ////// Code Generation      ////// //////
            // TODO Generate Code

            //DotCodeGenerator dcg = new DotCodeGenerator(input_file.SourceName);
            //dcg.Visit(tree);

            Builder b = new Builder(input_file.SourceName);
            try
            {
                b.Compile(tree, exitname);
                #endregion

                #region Exit
                ////// ////// Exit              ////// //////
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("{0,17}", "[Build succeded]");
                Console.ForegroundColor = ConsoleColor.Gray;
                #endregion
            }
            catch (Exception)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("{0,20}", "[Build failed]");
                Console.ForegroundColor = ConsoleColor.Gray;
            }

            File.Delete(input_file.SourceName + ".lexer.errors.txt");
            File.Delete(input_file.SourceName + ".parser.errors.txt");
            File.Delete(input_file.SourceName);
            Console.ReadLine();
        }
예제 #5
0
        static void Main(string[] args)
        {
            // TODO --verbose parameter?

            #region debuging
            ////// To Release comment from HERE ##
            //#if Debug
            //    Fake_Main(new string[] { "../test/semanticerrors/selfmethod.cl" });//TODO
            //}
            //static void Fake_Main(string[] args)
            //{
            //#endif

            // To Release comment til HERE ##
            #endregion

            #region Variables
            double version   = 0.1;
            string copyright = "ADB & JLA";
            var    verbose   = false;
            string usage     = "Usage:\n\tcoolc.exe <filename.cl>";
            #endregion

            #region Welcome Message
            ////// ////// Welcome Message   ////// //////
            Console.ForegroundColor = ConsoleColor.Gray;

            if (verbose == true)
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine("A cool compiler :)");
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine("Version: {0}", version);
                Console.WriteLine("Copyright (C) 2017-2018: {0}", copyright);
            }
            #endregion

            #region Open file
            ////// ////// Open file         ////// //////

            if (args.Length == 0)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("ERROR: No arguments given ");
                Console.ForegroundColor = ConsoleColor.Gray;
                //Console.WriteLine(usage);
                Environment.Exit(1);
                return;
            }

            //Console.WriteLine("Opening: {0}\t", args[0]);
            Console.Write("Program: {0,40}\t", args[0]);
            AntlrFileStream input_file;
            try
            {
                input_file = new AntlrFileStream(args[0]);
            }
            catch (Exception)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("ERROR: Opening file ");
                Console.ForegroundColor = ConsoleColor.Gray;
                //Console.WriteLine("");
                //Console.WriteLine(usage);
                Environment.Exit(1);
                return;
            }

            #endregion

            #region Parse
            StreamWriter LEW = new StreamWriter(args[0] + ".lexer.errors.txt");
            StreamWriter PEW = new StreamWriter(args[0] + ".parser.errors.txt");

            ////// ////// Parse             ////// //////
            CoolLexer lexer = new CoolLexer(input_file, LEW, LEW);

            CommonTokenStream tokens = new CommonTokenStream(lexer);

            CoolParser parser = new CoolParser(tokens, PEW, PEW);

            Console.ForegroundColor = ConsoleColor.Yellow;
            IParseTree tree = parser.program();

            LEW.Close();
            PEW.Close();

            if (parser.NumberOfSyntaxErrors > 0)
            {
                //Console.WriteLine("{0,20}","[Parsing Error]");
                Console.WriteLine();
                StreamReader LER = new StreamReader(args[0] + ".lexer.errors.txt");
                StreamReader PER = new StreamReader(args[0] + ".parser.errors.txt");

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Lexer Errors:");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(LER.ReadToEnd());

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Parser Errors:");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(PER.ReadToEnd());

                LER.Close();
                PER.Close();
                Console.ForegroundColor = ConsoleColor.Gray;

                Environment.Exit(1);
            }
            Console.ForegroundColor = ConsoleColor.Gray;
            #endregion

            #region Check Semantics
            // ////// Check Semantics   ////// //////
            ASTBuilder  builder = new ASTBuilder();
            AST.Program p       = (AST.Program)builder.Visit(tree);

            if (p.Errors.Count > 0 || p.CheckSemantics().Count > 0)
            {
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Semantic errors: ");
                Console.ForegroundColor = ConsoleColor.Yellow;

                foreach (Error e in p.Errors)
                {
                    Console.WriteLine(e);
                }
                Console.ForegroundColor = ConsoleColor.Gray;

                //File.Delete(args[0] + ".lexer.errors.txt");
                //File.Delete(args[0] + ".parser.errors.txt");

                Environment.Exit(1);
                //Console.WriteLine();
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("[Cool code]\t");
                Console.ForegroundColor = ConsoleColor.Gray;
            }
            //Console.WriteLine();
            //return;
            #endregion

            #region Generate Code
            ////// ////// Code Generation      ////// //////
            // TODO Generate Code

            DotCodeGenerator dcg = new DotCodeGenerator(args[0]);
            dcg.Visit(tree);

            Builder b = new Builder(args[0]);
            try
            {
                b.Compile(tree);
                #endregion

                #region Exit
                ////// ////// Exit              ////// //////
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("{0,17}", "[Build succeded]");
                Console.ForegroundColor = ConsoleColor.Gray;
                #endregion
            }
            catch (Exception)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("{0,17}", "[Build failed]");
                Console.ForegroundColor = ConsoleColor.Gray;
            }

            File.Delete(args[0] + ".lexer.errors.txt");
            File.Delete(args[0] + ".parser.errors.txt");
        }