Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var input = new AntlrFileStream("C:/Users/Leandro/Desktop/Compiler/CoolCompiler/input.cl");
            var lexer = new CoolLexer(input);

            /*var errors = new List<string>();
             * lexer.RemoveErrorListeners();*/


            var tokens = new CommonTokenStream(lexer);

            var parser = new CoolParser(tokens);

            var tree = parser.program();

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

            Console.WriteLine( );


            /* var a = new Dictionary<int,int>();
             * a.Add(1,2);
             * a.Add(3, 4);
             * a.Add(5, 6);
             *
             * foreach (var x in ast.Attributes)
             * {
             *   Console.WriteLine( "{"+
             *   x.Key.ToString() + ":" + x.Value.ToString() + "}" );
             * }
             */

            Console.WriteLine(tree.ToStringTree(parser));
        }
Exemplo n.º 2
0
        public static CoolParser.ProgramContext Tree(CommonTokenStream tokens)
        {
            var errors = new List <string>();
            var parser = new CoolParser(tokens);

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

            var tree = parser.program();


            if (errors.Any())
            {
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Red;
                foreach (var item in errors)
                {
                    Console.WriteLine(item);
                }
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.WriteLine();
                return(null);
            }
            return(tree);
        }
Exemplo n.º 3
0
        private static ASTNode ParseInput(string inputPath)
        {
            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 ASTGenerator();
            ASTNode ast        = astBuilder.Visit(tree);

            return(ast);
        }
Exemplo n.º 4
0
        public static AST_Root BUILD(string text)
        {
            AntlrInputStream  input  = new AntlrInputStream(text);
            CoolLexer         lexer  = new CoolLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            CoolParser        parser = new CoolParser(tokens);

            AntlrInputStream     input2  = new AntlrInputStream(text);
            CoolLexer            lexer2  = new CoolLexer(input2);
            CommonTokenStream    tokens2 = new CommonTokenStream(lexer2);
            CoolParser           parser2 = new CoolParser(tokens2);
            BuildAST_Cool_Errors v2      = new BuildAST_Cool_Errors();

            v2.Visit(parser2.program());
            if (!v2.Ok)
            {
                return(null);
            }

            BuildAST_Cool_Visitor v = new BuildAST_Cool_Visitor();

            var s = (AST_Root)v.Visit(parser.program());

            return(s);
        }
Exemplo n.º 5
0
        private static void Debug_ITree(string text)
        {
            AntlrInputStream  input  = new AntlrInputStream(text);
            CoolLexer         lexer  = new CoolLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            CoolParser        parser = new CoolParser(tokens);

            Console.WriteLine(parser.program().GetText());
        }
Exemplo n.º 6
0
        public static CoolParser CalcParser(string text)
        {
            AntlrInputStream  input  = new AntlrInputStream(text);
            CoolLexer         lexer  = new CoolLexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            CoolParser        parser = new CoolParser(tokens);

            return(parser);
        }
Exemplo n.º 7
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);
        }
        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);
        }
Exemplo n.º 9
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();
        }
Exemplo n.º 10
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");
        }