Beispiel #1
0
        private static void Main(string[] args)
        {
            if (!args.Any())
            {
                Console.WriteLine("There is no file to compile.");
                return;
            }

            var fileName = args.First();
            if (!File.Exists(fileName))
            {
                Console.WriteLine("There is no file to compile.");
                return;
            }

            string program;
            using (var fr = new StreamReader(File.Open(fileName, FileMode.Open)))
            {
                program = fr.ReadToEnd();
            }

            var reader = new Reader("input.txt");
            var grammar = reader.ReadGrammar();

            var lexical = new LexicalAnalyzer(grammar, "LA.xml");
            var tokens = lexical.Convert(program);

            var syntax = new SyntaxAnalyzer(tokens);
            var tree = syntax.Analyze();

            if (ReportError(syntax.Errors))
                return;

            var semantics = new SemanticAnalyzer(tree);
            semantics.DecorateAndValidateTree();

            if (ReportError(semantics.Errors))
                return;

            var gen = new CodeGenerator(tree);
            gen.Generate(Path.GetFileNameWithoutExtension(fileName) + ".exe");
        }
Beispiel #2
0
        private ITree Compile()
        {
            var reader = new Reader("input.txt");
            var grammar = reader.ReadGrammar();

            var lexical = new LexicalAnalyzer(grammar, "LA.xml");
            var tokens = lexical.Convert(Code.Text);

            var syntax = new SyntaxAnalyzer(tokens);
            var tree = syntax.Analyze();

            if (syntax.Errors.Any())
            {
                Errors = syntax.Errors;
                return null;
            }

            var semantics = new SemanticAnalyzer(tree);
            semantics.DecorateAndValidateTree();

            if (semantics.Errors.Any())
            {
                Errors = semantics.Errors;
                return null;
            }

            return tree;
        }