From 3d2cbe087d7de643b7a1ad155552727d43d89a83 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Tue, 31 Jul 2012 18:54:00 -0700 Subject: [PATCH] refactoring --- .../v4/runtime/atn/ParserATNSimulator.java | 42 ++++++------------- 1 file changed, 13 insertions(+), 29 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 890978986..7a76547a9 100755 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/ParserATNSimulator.java @@ -552,7 +552,7 @@ public class ParserATNSimulator extends ATNSimulator { PredictionContext predictionCtx = PredictionContext.fromRuleContext(outerContext); predictionCtx = getCachedContext(predictionCtx); D.isCtxSensitive = true; // always force DFA to ATN simulate - D.prediction = predictedAlt = fullCtxSet.uniqueAlt; // TODO: why set prediction? + predictedAlt = fullCtxSet.uniqueAlt; D.prediction = ATN.INVALID_ALT_NUMBER; D.contextToPredictedAlt.put(predictionCtx, predictedAlt); addDFAEdge(dfa, previousD, t, D); @@ -590,17 +590,21 @@ public class ParserATNSimulator extends ATNSimulator { if ( D.isAcceptState && D.configs.hasSemanticContext ) { // We need to test all predicates, even in DFA states that - // uniquely predict alternative. We can only get a conflict - // when we're sure that it's an ambiguity not conflict. + // uniquely predict alternative. int nalts = decState.getNumberOfTransitions(); - List predPredictions = - predicateDFAState(D, D.configs, outerContext, nalts); - // TODO: get rid of side-effects? sets predicted 2x - predicateDFAState(D, D.configs, outerContext, nalts); // alters D - if ( predPredictions!=null ) { + // Update DFA so reach becomes accept state with (predicate,alt) + // pairs if preds found for conflicting alts + IntervalSet conflictingAlts = getConflictingAltsFromConfigSet(D.configs); + SemanticContext[] altToPred = getPredsForAmbigAlts(conflictingAlts, D.configs, nalts); + if ( altToPred!=null ) { + D.predicates = getPredicatePredictions(conflictingAlts, altToPred); + } + D.prediction = ATN.INVALID_ALT_NUMBER; // make sure we use preds + + if ( D.predicates!=null ) { int stopIndex = input.index(); input.seek(startIndex); - IntervalSet alts = evalSemanticContext(predPredictions, outerContext, true); + IntervalSet alts = evalSemanticContext(D.predicates, outerContext, true); D.prediction = ATN.INVALID_ALT_NUMBER; // indicate we have preds addDFAEdge(dfa, previousD, t, D); switch (alts.size()) { @@ -789,26 +793,6 @@ public class ParserATNSimulator extends ATNSimulator { return null; } - /** collect and set D's semantic context */ - public List predicateDFAState(DFAState D, - ATNConfigSet configs, - RuleContext outerContext, - int nalts) - { - IntervalSet conflictingAlts = getConflictingAltsFromConfigSet(configs); - if ( debug ) System.out.println("predicateDFAState "+D); - SemanticContext[] altToPred = getPredsForAmbigAlts(conflictingAlts, configs, nalts); - // altToPred[uniqueAlt] is now our validating predicate (if any) - List predPredictions = null; - if ( altToPred!=null ) { - // Update DFA so reach becomes accept state with predicate - predPredictions = getPredicatePredictions(conflictingAlts, altToPred); - D.predicates = predPredictions; - D.prediction = ATN.INVALID_ALT_NUMBER; // make sure we use preds - } - return predPredictions; - } - public SemanticContext[] getPredsForAmbigAlts(@NotNull IntervalSet ambigAlts, @NotNull ATNConfigSet configs, int nalts)