adding full LL prediction in DFAState from ctx -> predicted alt. Drops from 17s to 12s and costs only 4M more ram. woot!

This commit is contained in:
Terence Parr 2012-07-29 13:30:18 -07:00
parent 230b687b03
commit 9bcfc83678
2 changed files with 20 additions and 0 deletions

View File

@ -290,6 +290,12 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
computeStartState(dfa.atnStartState, outerContext,
greedy, loopsSimulateTailRecursion,
fullCtx);
PredictionContext predictionCtx = PredictionContext.fromRuleContext(outerContext);
predictionCtx = getCachedContext(predictionCtx);
Integer predI = s.contextToPredictedAlt.get(predictionCtx);
if ( predI!=null ) {
return predI;
}
retry_with_context_from_dfa++;
ATNConfigSet fullCtxSet =
execATNWithFullContext(dfa, s, s0_closure,
@ -297,6 +303,7 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
outerContext,
ATN.INVALID_ALT_NUMBER,
greedy);
s.contextToPredictedAlt.put(predictionCtx, fullCtxSet.uniqueAlt);
return fullCtxSet.uniqueAlt;
}
if ( s.isAcceptState ) {
@ -482,6 +489,12 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
}
else {
if ( debug ) System.out.println("RETRY with outerContext="+outerContext);
PredictionContext predictionCtx = PredictionContext.fromRuleContext(outerContext);
predictionCtx = getCachedContext(predictionCtx);
Integer predI = D.contextToPredictedAlt.get(predictionCtx);
if ( predI!=null ) {
return predI;
}
loopsSimulateTailRecursion = true;
ATNConfigSet s0_closure =
computeStartState(dfa.atnStartState,
@ -497,6 +510,7 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
// not accept state: isCtxSensitive
D.isCtxSensitive = true; // always force DFA to ATN simulate
D.prediction = predictedAlt = fullCtxSet.uniqueAlt;
D.contextToPredictedAlt.put(predictionCtx, predictedAlt);
return predictedAlt; // all done with preds, etc...
}
}

View File

@ -31,11 +31,14 @@ package org.antlr.v4.runtime.dfa;
import org.antlr.v4.runtime.atn.ATNConfig;
import org.antlr.v4.runtime.atn.ATNConfigSet;
import org.antlr.v4.runtime.atn.PredictionContext;
import org.antlr.v4.runtime.atn.SemanticContext;
import org.antlr.v4.runtime.misc.Nullable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/** A DFA state represents a set of possible ATN configurations.
@ -103,6 +106,9 @@ public class DFAState {
@Nullable
public List<PredPrediction> predicates;
public Map<PredictionContext,Integer> contextToPredictedAlt =
new HashMap<PredictionContext, Integer>();
/** Map a predicate to a predicted alternative */
public static class PredPrediction {
public SemanticContext pred; // never null; at least SemanticContext.NONE