Exemple #1
0
        static void Main(string[] args)
        {
            const string sourcecode       = "source-code.txt";
            const string tokenset         = "token-set.txt";
            const string intermediatecode = "intermediate-code.txt";


            string[] code = Filling.Read(sourcecode);

            LexicalAnalyzer LA = new LexicalAnalyzer(code);

            LA.analyze(tokenset);


            string[]       tokens     = Filling.Read(tokenset);
            SyntaxAnalyzer SA         = new SyntaxAnalyzer(tokens);
            int            tokenIndex = SA.analyze();

            //int tokenIndex = 0;

            if (tokenIndex == -1)
            {
                List <Token> _tokens = new List <Token>();
                foreach (string token in tokens)
                {
                    _tokens.Add(new Token(token));
                }

                TestSemanticTree.MainSemanticTree.parse(_tokens.ToArray());
                List <ErrorRecord> errors = TestSemanticTree.MainSemanticTree.errors;
                if (errors.ToArray().Length == 0)
                {
                    ICGTree.MainSyntaxTree.analyze(intermediatecode, _tokens.ToArray());
                }
                else
                {
                    foreach (ErrorRecord error in errors.ToArray())
                    {
                        Console.WriteLine(error.identifier + "on line# " + error._token.line + "(" + error.type + ")");
                    }
                }
            }
            else
            {
                Token  token = new Token(tokens[(tokenIndex < tokens.Length) ? tokenIndex : tokenIndex - 1]);
                string line  = code[token.line - 1];
                int    index = line.IndexOf(token.valuepart);
                string error = string.Empty;
                while (index > 0)
                {
                    index--;
                    error += " ";
                }
                error += "^";
                Console.WriteLine(line);
                Console.WriteLine(error);
            }
        }
        static void Main(string[] args)
        {
            const string sourcecode = "source-code.txt";
            const string tokenset = "token-set.txt";
            const string intermediatecode = "intermediate-code.txt";

            string[] code = Filling.Read(sourcecode);

            LexicalAnalyzer LA = new LexicalAnalyzer(code);
            LA.analyze(tokenset);

            string[] tokens = Filling.Read(tokenset);
            SyntaxAnalyzer SA = new SyntaxAnalyzer(tokens);
            int tokenIndex = SA.analyze();

            //int tokenIndex = 0;

            if (tokenIndex == -1)
            {
                List<Token> _tokens = new List<Token>();
                foreach (string token in tokens)
                    _tokens.Add(new Token(token));

                TestSemanticTree.MainSemanticTree.parse(_tokens.ToArray());
                List<ErrorRecord> errors= TestSemanticTree.MainSemanticTree.errors;
                if (errors.ToArray().Length == 0)
                {
                    ICGTree.MainSyntaxTree.analyze(intermediatecode, _tokens.ToArray());
                }
                else
                {
                    foreach (ErrorRecord error in errors.ToArray())
                        Console.WriteLine(error.identifier + "on line# " + error._token.line + "(" + error.type + ")");
                }

            }
            else
            {
                Token token = new Token(tokens[(tokenIndex < tokens.Length) ? tokenIndex : tokenIndex - 1]);
                string line = code[token.line - 1];
                int index = line.IndexOf(token.valuepart);
                string error = string.Empty;
                while (index > 0)
                {
                    index--;
                    error += " ";
                }
                error += "^";
                Console.WriteLine(line);
                Console.WriteLine(error);
            }
        }
Exemple #3
0
        static void Main(string[] args)
        {
            string testFile      = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"test_code.txt");
            string outputFile    = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"output.txt");
            string outputFileCsv = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"output.csv");

            var sourceCode = File.ReadAllText(testFile);

            var lexicalAnalyzer = new LexicalAnalyzer();
            var tokens          = lexicalAnalyzer.Analyze(sourceCode);

            var syntaxAnalyzer = new SyntaxAnalyzer();
            var token          = syntaxAnalyzer.Analyze(tokens);

            if (token == null)
            {
                Console.WriteLine("Source code parsed Successfully!");
            }
            else
            {
                Console.WriteLine($"Error! Invalid character \"{token.Value}\" found on Line number \"{token.LineNumber}\"");
            }

            //Write to file
            //var fileText = "LINE NUMBER,CLASS NAME,VALUE\r\n";
            //foreach (var token in tokens)
            //{
            //    fileText += token.ToCSVString();
            //}
            //File.WriteAllText(outputFileCsv, fileText);
            //fileText = "";
            //foreach (var token in tokens)
            //{
            //    fileText += token.ToString();
            //}
            //File.WriteAllText(outputFile, fileText);

            //Print to console
            foreach (var t in tokens)
            {
                Console.WriteLine(t.ToString());
            }

            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Usage: filename is not entered");
                return;
            }
            else
            {
                SyntaxAnalyzer syntaxAnalyzer = new SyntaxAnalyzer(args[0]);

                syntaxAnalyzer.Prog();

                if (token == Token.eoft)
                {
                    Console.WriteLine("Mini Java compiler has successfully finished compiling.");
                }
                else
                {
                    ExceptionHandler.ThrowUnusedTokensException();
                }
            }
        }
Exemple #5
0
        static void Main(string[] args)
        {
            //var path = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.ToString(), "input.txt");
            //Debugging path
            //string text = System.IO.File.ReadAllText("~\\..\\..\\..\\..\\input.txt");

            /*
             * dotnet publish Compiler.sln -c Release -r win10-x64
             * Go to bin\Release
             */
            //Release path
            string text = System.IO.File.ReadAllText("input.txt");

            Console.WriteLine("Input string:\n" + text + "\n\n");
            var SAnalyzer = new SyntaxAnalyzer();

            Console.WriteLine("Production Rules:");
            SAnalyzer.OutputProductionRules();
            Console.WriteLine("\nParsing...");
            Console.WriteLine('\n' + (SAnalyzer.Parse(text) ? "The input is valid" : "The input failed the syntax analysis"));

            Console.WriteLine("Enter any key to end program.");
            Console.ReadKey();
        }