public Program ParseProgram(string program)
 {
     var scanner = new MiniJavaScanner(new StringReader(program));
     var errorLog = new ErrorLogger();
     var parser = new Parser(scanner, errorLog, true);
     Program ast;
     if (parser.TryParse(out ast))
     {
         return ast;
     }
     return null;
 }
Пример #2
0
 /* Performs lexical and semantic analysis. Returns null if either phase fails.
  *
  * Syntax analysis is always attempted regardless of lexical errors.
  * All errors are logged in the error log.
  */
 private Program ConstructAbstractSyntaxTree()
 {
     var scanner = new MiniJavaScanner(_program);
     var parser = new Parser(scanner, _errorLog);
     Program program;
     if (parser.TryParse(out program))
     {
         return program;
     }
     else
     {
         return null;
     }
 }
        public static SemanticsChecker SetUpTypeAndReferenceChecker(string program, out IErrorReporter errorLog)
        {
            var reader = new StringReader(program);
            var scanner = new MiniJavaScanner(reader);
            var errors = new ErrorLogger();
            var parser = new Parser(scanner, errors, true);
            Program syntaxTree;
            parser.TryParse(out syntaxTree);
            reader.Close();
            Assert.That(errors.Errors, Is.Empty);

            var symbolTableBuilder = new SymbolTableBuilder(syntaxTree, errors);
            Assert.That(errors.Errors, Is.Empty);

            Assert.DoesNotThrow(() => symbolTableBuilder.BuildSymbolTable());
            errorLog = new ErrorLogger();

            return new SemanticsChecker(syntaxTree, errorLog);
        }
        private SymbolTableBuilder.ExitCode BuildSymbolTableFor(string program, out GlobalScope symbolTable)
        {
            var reader = new StringReader(program);
            var scanner = new MiniJavaScanner(reader);
            _errors = new ErrorLogger();
            var parser = new Parser(scanner, _errors, true);
            Program syntaxTree;
            parser.TryParse(out syntaxTree);
            reader.Close();
            Assert.That(_errors.Errors, Is.Empty);

            var symbolTableBuilder = new SymbolTableBuilder(syntaxTree, _errors);
            Assert.That(_errors.Errors, Is.Empty);

            var success = symbolTableBuilder.BuildSymbolTable();
            symbolTable = syntaxTree.Scope as GlobalScope;
            return success;
        }