Evaluate preds in SLL before falling back to full context, avoid full context prediction if unique alternative results

This commit is contained in:
Sam Harwell 2012-11-18 14:16:28 -06:00
parent 691532190c
commit aba4034051
2 changed files with 22 additions and 16 deletions

View File

@ -418,6 +418,17 @@ public class ParserATNSimulator extends ATNSimulator {
while ( true ) {
if ( dfa_debug ) System.out.println("DFA state "+s.stateNumber+" LA(1)=="+getLookaheadName(input));
if ( s.requiresFullContext && mode != PredictionMode.SLL ) {
// IF PREDS, MIGHT RESOLVE TO SINGLE ALT => SLL (or syntax error)
if ( s.predicates!=null ) {
if ( debug ) System.out.println("DFA state has preds in DFA sim LL failover");
input.seek(startIndex);
BitSet alts = evalSemanticContext(s.predicates, outerContext, true);
if ( alts.cardinality()==1 ) {
if ( debug ) System.out.println("Full LL avoided");
return alts.nextSetBit(0);
}
}
if ( dfa_debug ) System.out.println("ctx sensitive state "+outerContext+" in "+s);
boolean fullCtx = true;
ATNConfigSet s0_closure =
@ -647,6 +658,17 @@ public class ParserATNSimulator extends ATNSimulator {
// Falls through to check predicates below
}
else {
// IF PREDS, MIGHT RESOLVE TO SINGLE ALT => SLL (or syntax error)
if ( D.configs.hasSemanticContext ) {
predicateDFAState(D, decState);
input.seek(startIndex);
BitSet alts = evalSemanticContext(D.predicates, outerContext, true);
if ( alts.cardinality()==1 ) {
if ( debug ) System.out.println("Full LL avoided");
return alts.nextSetBit(0);
}
}
// RETRY WITH FULL LL CONTEXT
if ( debug ) System.out.println("RETRY with outerContext="+outerContext);
ATNConfigSet s0_closure =

View File

@ -1,6 +1,5 @@
package org.antlr.v4.test;
import org.junit.Ignore;
import org.junit.Test;
/** Test parser execution.
@ -232,21 +231,6 @@ public class TestParserExec extends BaseTest {
* This test is meant to test the expected solution to antlr/antlr4#42.
* https://github.com/antlr/antlr4/issues/42
*/
@Ignore("Sam's works here but mine doesn't since I fail over to LL even "
+ "though SLL + preds evals to single alt; i could avoid but code "
+ "complexity wasn't worth it. see branch SLL-w-preds-avoids-LL")
/* Would need to add to execDFA, execATN:
// IF PREDS, MIGHT RESOLVE TO SINGLE ALT => SLL (or syntax error)
if ( s.predicates!=null ) {
if ( debug ) System.out.println("DFA state has preds in DFA sim LL failover");
input.seek(startIndex);
IntervalSet alts = evalSemanticContext(s.predicates, outerContext, true);
if ( alts.size()==1 ) {
if ( debug ) System.out.println("Full LL avoided");
return alts.getMinElement();
}
}
*/
@Test
public void testPredicatedIfIfElse() throws Exception {
String grammar =