Exemple #1
0
        /// <summary>
        /// Parse the file and create the parse tree
        /// </summary>
        private static void ParseFile()
        {
            Console.WriteLine("\nParsing input file...");
            parseTree = parser.start();
            streamReader.Close();


            if (handler.errorsOccured())
            {
                handler.printErrors();
                Environment.Exit(1);
            }

            Console.WriteLine("Done.\n");
        }
Exemple #2
0
        static void Main(string[] args)
        {
            StreamReader pom = new System.IO.StreamReader("../../../sourceCode.txt");

            AntlrInputStream inputStream = new AntlrInputStream(pom);
            GrammarLexer     lexer       = new GrammarLexer(inputStream);

            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new GrammarErrorListener());
            CommonTokenStream c           = new CommonTokenStream(lexer);
            GrammarParser     helloParser = new GrammarParser(c);

            //IParseTree tree = helloParser.start();
            // ParseTreeWalker walker = new ParseTreeWalker();
            //walker.Walk(new TreeWalkerListener(), tree);
            helloParser.RemoveErrorListeners();
            helloParser.AddErrorListener(new GrammarErrorListener());


            Console.WriteLine("START");

            try
            {
                IParseTree tree = helloParser.start();
                Console.WriteLine("----------------Lexical analyzation OK----------------------");

                Visitor visitor = new Visitor();
                visitor.DoInitialJmp(1);
                int t = visitor.Visit(tree);
                visitor.numberInstructions();

                Console.WriteLine(visitor.GetSymbolTable().VarConstToString());
                Console.WriteLine("-----------------------------------------");
                PrintInstructions(visitor.GetInstructions());
                WriteInstructions(visitor.GetInstructions());
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }



            Console.ReadLine();
            // skvelej napad, jednopruchod znamena dolu i nahoru, takze dolu udelam jen neco a smerem nahoru zbytek
        }
Exemple #3
0
        public void TestGrammarError(String path, ErrorCode expectedError)
        {
            StreamReader pom = new System.IO.StreamReader(path);

            ErrorHandler handler = new ErrorHandler();


            AntlrInputStream inputStream = new AntlrInputStream(pom);
            GrammarLexer     lexer       = new GrammarLexer(inputStream);

            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new GrammarErrorListener(handler));
            CommonTokenStream c           = new CommonTokenStream(lexer);
            GrammarParser     helloParser = new GrammarParser(c);

            helloParser.RemoveErrorListeners();
            helloParser.AddErrorListener(new GrammarErrorListener(handler));

            try
            {
                IParseTree tree = helloParser.start();
                pom.Close();

                if (handler.errorsOccured())
                {
                    List <Error> grammarErrors = handler.GrammarErrors;

                    Assert.AreEqual(1, grammarErrors.Count, "More errors occured than expected");
                    Assert.AreEqual(ErrorCode.grammarError, grammarErrors[0].ErrorCode, "Invalid error type.");
                }
                else
                {
                    Assert.Fail("No grammar error occured");
                }
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
        }
Exemple #4
0
        public void TestOutputFromFile(String path, String output)
        {
            StreamReader pom = new System.IO.StreamReader(path);

            ErrorHandler handler = new ErrorHandler();


            AntlrInputStream inputStream = new AntlrInputStream(pom);
            GrammarLexer     lexer       = new GrammarLexer(inputStream);

            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(new GrammarErrorListener(handler));
            CommonTokenStream c           = new CommonTokenStream(lexer);
            GrammarParser     helloParser = new GrammarParser(c);

            helloParser.RemoveErrorListeners();
            helloParser.AddErrorListener(new GrammarErrorListener(handler));

            String path_file_ins = "insc1.txt";

            try
            {
                IParseTree tree = helloParser.start();
                pom.Close();

                if (handler.errorsOccured())
                {
                    Assert.Fail("Errors detected during lexical or syntactic analysis.");
                }

                Visitor visitor = new Visitor(handler);
                visitor.PrepareLibraryFunctions();
                visitor.DoInitialJmp();
                int t = visitor.Visit(tree);

                if (handler.errorsOccured())
                {
                    Assert.Fail("Errors detected during semantic analysis.");
                }

                visitor.numberInstructions();

                Program.WriteInstructions(visitor.GetInstructions(), path_file_ins);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }

            Process interpreter = new Process();

            interpreter.StartInfo.FileName               = "../../../refint_pl0_ext.exe";
            interpreter.StartInfo.Arguments              = path_file_ins + " -s -l";
            interpreter.StartInfo.UseShellExecute        = false;
            interpreter.StartInfo.RedirectStandardOutput = true;

            interpreter.Start();

            StreamReader reader = new StreamReader(interpreter.StandardOutput.BaseStream);

            if (!interpreter.WaitForExit(15000))
            {
                Assert.IsFalse(true, "Process might got stuck in infinite loop");
                interpreter.Kill();
            }


            String output_from_interpret = reader.ReadToEnd();

            Assert.AreEqual("START PL/0\r\n" + output + " END PL/0\r\n", output_from_interpret);
        }