This commit is contained in:
Terence Parr 2012-07-29 10:50:14 -07:00
parent 79594a942e
commit 510c1c0dd9
1 changed files with 12 additions and 25 deletions

View File

@ -31,6 +31,7 @@ import org.antlr.v4.runtime.ANTLRFileStream;
import org.antlr.v4.runtime.BailErrorStrategy; import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener; import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.atn.LexerATNSimulator; import org.antlr.v4.runtime.atn.LexerATNSimulator;
@ -41,8 +42,6 @@ import java.io.File;
class TestJavaLR { class TestJavaLR {
public static long lexerTime = 0; public static long lexerTime = 0;
public static boolean profile = false; public static boolean profile = false;
public static JavaLRLexer lexer;
public static JavaLRParser parser = null;
public static boolean notree = false; public static boolean notree = false;
public static boolean gui = false; public static boolean gui = false;
public static boolean printTree = false; public static boolean printTree = false;
@ -126,37 +125,25 @@ class TestJavaLR {
throws Exception { throws Exception {
try { try {
// Create a scanner that reads from the input stream passed to us // Create a scanner that reads from the input stream passed to us
if ( lexer==null ) { Lexer lexer = new JavaLRLexer(new ANTLRFileStream(f));
lexer = new JavaLRLexer(null);
}
lexer.setInputStream(new ANTLRFileStream(f));
CommonTokenStream tokens = new CommonTokenStream(lexer); CommonTokenStream tokens = new CommonTokenStream(lexer);
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
tokens.fill(); // load all and check time tokens.fill(); // load all and check time
// System.out.println(tokens.getTokens());
long stop = System.currentTimeMillis(); long stop = System.currentTimeMillis();
lexerTime += stop-start; lexerTime += stop-start;
if ( true ) {
// Create a parser that reads from the scanner // Create a parser that reads from the scanner
if ( parser==null ) { JavaLRParser parser = new JavaLRParser(tokens);
parser = new JavaLRParser(null);
if ( diag ) parser.addErrorListener(new DiagnosticErrorListener()); if ( diag ) parser.addErrorListener(new DiagnosticErrorListener());
if ( bail ) parser.setErrorHandler(new BailErrorStrategy()); if ( bail ) parser.setErrorHandler(new BailErrorStrategy());
if ( SLL ) parser.getInterpreter().SLL = true; if ( SLL ) parser.getInterpreter().SLL = true;
}
parser.setTokenStream(tokens);
// start parsing at the compilationUnit rule // start parsing at the compilationUnit rule
ParserRuleContext<Token> t = parser.compilationUnit(); ParserRuleContext<Token> t = parser.compilationUnit();
if ( notree ) parser.setBuildParseTree(false); if ( notree ) parser.setBuildParseTree(false);
if ( gui ) t.inspect(parser); if ( gui ) t.inspect(parser);
if ( printTree ) System.out.println(t.toStringTree(parser)); if ( printTree ) System.out.println(t.toStringTree(parser));
//System.err.println("finished "+f);
// System.out.println("cache size = "+DefaultErrorStrategy.cache.size());
lexer=null; parser=null; // force rebuild
}
} }
catch (Exception e) { catch (Exception e) {
System.err.println("parser exception: "+e); System.err.println("parser exception: "+e);