From 2495ce6b2088e3ed6c282c6f177a56e243d31739 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Tue, 24 Jul 2012 17:42:47 -0700 Subject: [PATCH] add SLL option to the ATN simulator and use in TestJavaLR. --- .../v4/runtime/atn/ParserATNSimulator.java | 7 ++++-- tool/playground/TestJavaLR.java | 25 ++++++++----------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java index 00fbf55e0..2599b7816 100755 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java @@ -267,6 +267,9 @@ public class ParserATNSimulator extends ATNSimulator { */ public boolean reportAmbiguities = true; + /** Do only local context prediction (SLL(k) style). */ + public boolean SLL = false; + /** Testing only! */ public ParserATNSimulator(@NotNull ATN atn) { this(null, atn); @@ -374,7 +377,7 @@ public class ParserATNSimulator extends ATNSimulator { loop: while ( true ) { if ( dfa_debug ) System.out.println("DFA state "+s.stateNumber+" LA(1)=="+getLookaheadName(input)); - if ( s.isCtxSensitive ) { + if ( s.isCtxSensitive && !SLL ) { if ( dfa_debug ) System.out.println("ctx sensitive state "+outerContext+" in "+s); boolean loopsSimulateTailRecursion = true; boolean fullCtx = false; @@ -562,7 +565,7 @@ public class ParserATNSimulator extends ATNSimulator { int k = input.index() - startIndex + 1; // how much input we used // System.out.println("used k="+k); if ( outerContext == ParserRuleContext.EMPTY || // in grammar start rule - !D.configs.dipsIntoOuterContext ) + !D.configs.dipsIntoOuterContext || SLL ) { if ( reportAmbiguities && !D.configs.hasSemanticContext ) { reportAmbiguity(dfa, D, startIndex, input.index(), diff --git a/tool/playground/TestJavaLR.java b/tool/playground/TestJavaLR.java index 20a53768d..977dd2e91 100644 --- a/tool/playground/TestJavaLR.java +++ b/tool/playground/TestJavaLR.java @@ -27,9 +27,9 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import org.antlr.runtime.debug.BlankDebugEventListener; import org.antlr.v4.runtime.ANTLRFileStream; import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.DiagnosticErrorListener; import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.atn.LexerATNSimulator; @@ -43,6 +43,9 @@ class TestJavaLR { public static JavaLRLexer lexer; public static JavaLRParser parser = null; public static boolean showTree = false; + public static boolean printTree = false; + public static boolean SLL = false; + public static boolean diag = false; public static void main(String[] args) { doAll(args); @@ -57,6 +60,9 @@ class TestJavaLR { // for each directory/file specified on the command line for(int i=0; i< args.length;i++) { if ( args[i].equals("-tree") ) showTree = true; + else if ( args[i].equals("-ptree") ) printTree = true; + else if ( args[i].equals("-SLL") ) SLL = true; + else if ( args[i].equals("-diag") ) diag = true; doFile(new File(args[i])); // parse it } } @@ -75,9 +81,6 @@ class TestJavaLR { System.out.println(ParserATNSimulator.predict_calls +" parser predict calls"); System.out.println(ParserATNSimulator.retry_with_context +" retry_with_context after SLL conflict"); System.out.println(ParserATNSimulator.retry_with_context_indicates_no_conflict +" retry sees no conflict"); - if ( profile ) { - System.out.println("num decisions "+profiler.numDecisions); - } } catch(Exception e) { System.err.println("exception: "+e); @@ -106,14 +109,6 @@ class TestJavaLR { } } - static class CountDecisions extends BlankDebugEventListener { - public int numDecisions = 0; - public void enterDecision(int decisionNumber) { - numDecisions++; - } - } - static CountDecisions profiler = new CountDecisions(); - // Here's where we do the real work... public static void parseFile(String f) throws Exception { @@ -135,15 +130,17 @@ class TestJavaLR { // Create a parser that reads from the scanner if ( parser==null ) { parser = new JavaLRParser(null); - if ( showTree ) parser.setBuildParseTree(true); // parser.setErrorHandler(new BailErrorStrategy()); // parser.getInterpreter().setContextSensitive(true); } parser.setTokenStream(tokens); + if ( diag ) parser.addErrorListener(new DiagnosticErrorListener()); + if ( SLL ) parser.getInterpreter().SLL = true; // start parsing at the compilationUnit rule ParserRuleContext tree = parser.compilationUnit(); - if ( showTree ) tree.inspect(parser); + if ( showTree ) tree.inspect(parser); + if ( printTree ) System.out.println(tree.toStringTree(parser)); //System.err.println("finished "+f); // System.out.println("cache size = "+DefaultErrorStrategy.cache.size()); }