Beispiel #1
0
        //-----------------------------------------------------------
        void Run(string[] args)
        {
            PrintAppHeader();
            Console.WriteLine();
            PrintReleaseIncludes();
            Console.WriteLine();

            if (args.Length != 1)
            {
                Console.Error.WriteLine(
                    "Please specify the name of the input file.");
                Environment.Exit(1);
            }

            try {
                var inputPath = args[0];
                var input     = File.ReadAllText(inputPath);
                var parser    = new Parser(new Scanner(input).Start().GetEnumerator());
                var program   = parser.Program();
                Console.WriteLine("Syntax OK.");

                var semantic = new SemanticAnalyzer();
                semantic.Visit((dynamic)program);

                Console.WriteLine("Semantics OK.");
                Console.WriteLine();
                Console.WriteLine("Symbol Table");
                Console.WriteLine("============");
                foreach (var entry in semantic.symbolTable)
                {
                    Console.WriteLine(entry);
                }
                Console.WriteLine("Procedures Table");
                Console.WriteLine("============");
                foreach (var entry in semantic.procedureTable)
                {
                    Console.WriteLine(entry);
                }
                Console.WriteLine("Local Symbol Tables");
                Console.WriteLine("============");
                foreach (var entry in semantic.localSTables)
                {
                    Console.WriteLine(entry);
                }
            } catch (Exception e) {
                if (e is FileNotFoundException ||
                    e is SyntaxError ||
                    e is SemanticError)
                {
                    Console.Error.WriteLine(e.Message);
                    Environment.Exit(1);
                }

                throw;
            }
        }
        //-----------------------------------------------------------
        void Run(string[] args)
        {
            PrintAppHeader();
            Console.WriteLine();
            PrintReleaseIncludes();
            Console.WriteLine();

            if (args.Length == 0)
            {
                Console.Error.WriteLine("Please specify the name of the input file.");
                Environment.Exit(1);
            }

            foreach (string inputPath in args)
            {
                try
                {
                    // Syntactic Analysis Validation
                    var input  = File.ReadAllText(inputPath);
                    var parser = new Parser(new Scanner(input).Start().GetEnumerator());
                    var ast    = parser.Program();
                    Console.WriteLine("Syntax OK.");

                    // Semantic Analysis Validation
                    var semantic = new SemanticAnalyzer();
                    semantic.Visit((dynamic)ast);
                    Console.WriteLine("Semantics OK.");

                    // Assembly Code Generation & File Validation
                    var codeGenerator = new CILGenerator(semantic.symbolTable, semantic.procedureTable);
                    codeGenerator.Visit((dynamic)ast);
                    var code       = codeGenerator.ToString();
                    var outputPath = inputPath.Replace(".chimera", ".il");

                    File.WriteAllText(
                        outputPath,
                        code);
                    Console.WriteLine(
                        $"Generated CIL code to '{outputPath}'.");
                }
                catch (Exception e)
                {
                    if (e is FileNotFoundException ||
                        e is SyntaxError ||
                        e is SemanticError)
                    {
                        Console.Error.WriteLine(e.Message);
                        Environment.Exit(1);
                    }

                    throw;
                }
            }
        }
Beispiel #3
0
        //-----------------------------------------------------------
        void Run(string[] args)
        {
            PrintAppHeader();
            Console.WriteLine();
            PrintReleaseIncludes();
            Console.WriteLine();

            if (args.Length != 2)
            {
                Console.Error.WriteLine("Please specify the name of the input and output files.");
                Environment.Exit(1);
            }

            try {
                var inputPath  = args[0];
                var outputPath = args[1];
                Console.WriteLine("Filename: " + inputPath);
                var input   = File.ReadAllText(inputPath);
                var parser  = new Parser(new Scanner(input).Start().GetEnumerator());
                var program = parser.Program();

                Console.WriteLine(program.ToStringTree());

                Console.WriteLine("Syntax OK.");
                //semantic
                var semantic = new SemanticAnalyzer();
                semantic.Visit((dynamic)program);
                Console.WriteLine("Semantics OK.");

                var tables = semantic.getTables();
                semantic.printTables();

                /*foreach(var i in tables){
                 *  Console.WriteLine(i);
                 * }*/
                var codeGenerator = new CILGenerator(tables);
                File.WriteAllText(outputPath, codeGenerator.Visit((dynamic)program));
                Console.WriteLine("Generated CIL code to '" + outputPath + "'.");
                Console.WriteLine();
                //semantic.printTables();
            } catch (Exception e) {
                if (e is FileNotFoundException || e is SyntaxError || e is SemanticError)
                {
                    Console.Error.WriteLine(e.Message);
                    Environment.Exit(1);
                }

                throw;
            }
        }
        //-----------------------------------------------------------
        void Run(string[] args)
        {
            PrintAppHeader();
            Console.WriteLine();
            PrintReleaseIncludes();
            Console.WriteLine();

            if (args.Length == 0)
            {
                Console.Error.WriteLine(
                    "Please specify the name of at least one input file.");
                Environment.Exit(1);
            }

            foreach (string inputPath in args)
            {
                try
                {
                    var input  = File.ReadAllText(inputPath);
                    var parser = new Parser(new Scanner(input).Start().GetEnumerator());

                    var ast = parser.Program();

                    var semantic = new SemanticAnalyzer();
                    semantic.Visit((dynamic)ast);
                    Console.WriteLine("Semantics OK.");
                    Console.WriteLine();
                    Console.WriteLine(semantic.symbolTable);
                    Console.WriteLine();
                    Console.WriteLine(semantic.procedureTable);
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine($"Exception on file: '{inputPath}'");
                    if (e is FileNotFoundException ||
                        e is SyntaxError ||
                        e is SemanticError)
                    {
                        Console.Error.WriteLine(e.Message);
                        Console.WriteLine("-----------");
                        Console.WriteLine(e.StackTrace);
                        Environment.Exit(1);
                    }

                    throw;
                }
            }
        }
Beispiel #5
0
        //-----------------------------------------------------------
        void Run(string[] args)
        {
            PrintAppHeader();
            Console.WriteLine();
            PrintReleaseIncludes();
            Console.WriteLine();

            if (args.Length != 2)
            {
                Console.Error.WriteLine("Please specify the name of the input and output files.");
                Environment.Exit(1);
            }

            try
            {
                var inputPath  = args[0];
                var outputPath = args[1];
                var input      = File.ReadAllText(inputPath);

                /* Lexical analysis * /
                 * Console.WriteLine(String.Format(
                 *  "===== Tokens from: \"{0}\" =====", inputPath)
                 * );
                 * var count = 1;
                 * foreach (var tok in new Scanner(input).Start()) {
                 *  Console.WriteLine(String.Format("[{0}] {1}",
                 *                                  count++, tok)
                 *  );
                 * }
                 *
                 * /* Lexical + syntactic analysis* /
                 * var parser = new Parser(new Scanner(input).Start().GetEnumerator());
                 * parser.Program();
                 * Console.WriteLine("Syntax OK.");
                 *
                 * /* Lexical + syntactic analysis + AST construction* /
                 * var parser = new Parser(new Scanner(input).Start().GetEnumerator());
                 * var program = parser.Program();
                 * Console.Write(program.ToStringTree());
                 *
                 * /* Lexical + syntactic analysis + AST construction + Semantic Analyzer*/
                var parser  = new Parser(new Scanner(input).Start().GetEnumerator());
                var program = parser.Program();
                Console.WriteLine("---START SYNTAX TREE---");
                Console.Write(program.ToStringTree());//
                Console.WriteLine("---END SYNTAX TREE---");

                var semantic = new SemanticAnalyzer();
                semantic.Visit((dynamic)program);
                //Console.WriteLine(semantic.GSTable);
                //Console.WriteLine(semantic.GPTable);
                Console.WriteLine("Semantics OK.");


                var codeGenerator = new CILGenerator(semantic.GSTable, semantic.GPTable);
                var code          = codeGenerator.Visit((dynamic)program);
                File.WriteAllText(outputPath, code);
                Console.WriteLine(code);
                Console.WriteLine("Generated CIL code to '" + outputPath + "'.");
                Console.WriteLine();
            }
            catch (FileNotFoundException e)
            {
                Console.Error.WriteLine(e.Message);
                Environment.Exit(1);
            }
            catch (SyntaxError e)
            {
                Console.Error.WriteLine(e.Message);
                Environment.Exit(2);
            }
            catch (SemanticError e)
            {
                Console.Error.WriteLine(e.Message);
                Environment.Exit(3);
            }
        }
Beispiel #6
0
        void Run(string[] args)
        {
            PrintAppHeader();
            Console.WriteLine();
            PrintReleaseIncludes();
            Console.WriteLine();

            if (args.Length != 1)
            {
                Console.Error.WriteLine(
                    "Please specify the name of the input file.");
                Environment.Exit(1);
            }
            //Comentar de aqui hasta

            /*try
             * {
             *  var inputPath = args[0];
             *  var input = File.ReadAllText(inputPath);
             *
             *  Console.WriteLine(String.Format(
             *      "===== Tokens from: \"{0}\" =====", inputPath)
             *  );
             *  var count = 1;
             *  foreach (var tok in new Scanner(input).Start())
             *  {
             *      Console.WriteLine(String.Format("[{0}] {1}",
             *                                      count++, tok)
             *      );
             *  }
             *
             * }
             * catch (FileNotFoundException e)
             * {
             *  Console.Error.WriteLine(e.Message);
             *  Environment.Exit(1);
             * }
             *
             * try
             * {
             *  var inputPath = args[0];
             *  var input = File.ReadAllText(inputPath);
             *  var parser = new Parser(new Scanner(input).Start().GetEnumerator());
             *  parser.Program();
             *  Console.WriteLine("Syntax OK.");
             *
             * }
             * catch (Exception e)
             * {
             *
             *  if (e is FileNotFoundException || e is SyntaxError)
             *  {
             *      Console.Error.WriteLine(e.Message);
             *      Environment.Exit(1);
             *  }
             *
             *  throw;
             * }*/
            //Aca
            try
            {
                var inputPath = args[0];
                var input     = File.ReadAllText(inputPath);
                var parser    = new Parser(new Scanner(input).Start().GetEnumerator());
                var program   = parser.Program();
                //Console.Write(program.ToStringTree());
                Console.WriteLine("Syntax OK.");

                var semantic = new SemanticAnalyzer();

                semantic.Visit((dynamic)program);

                Console.WriteLine("Semantics OK.");
                Console.WriteLine();

                // mi solución
                Console.WriteLine(semantic.GloabalDeclaratonT);
                //Console.WriteLine(semantic.LocalDeclarationT);
                foreach (var entry in semantic.ListLocalDeclarationTable)
                {
                    Console.WriteLine(entry);
                }
                Console.WriteLine(semantic.ProcedureDeclarationT);
            }
            catch (Exception e)
            {
                if (e is FileNotFoundException || e is SyntaxError || e is SemanticError)
                {
                    Console.Error.WriteLine(e.Message);
                    Environment.Exit(1);
                }
                throw;
            }
        }