EMPTY gives "" not "$" now. rename closure->closure_. had return in wrong spot in closure for-loop. fix fromRuleContext.

This commit is contained in:
Terence Parr 2012-03-20 19:06:52 -07:00
parent 0f969af947
commit b035ceec9e
7 changed files with 39 additions and 33 deletions

View File

@ -171,7 +171,7 @@ public class ATNConfig {
}
if ( context!=null ) {
buf.append(",[");
buf.append(context.toString(recog));
buf.append(context.toString());
buf.append("]");
}
if ( semanticContext!=null && semanticContext != SemanticContext.NONE ) {

View File

@ -132,12 +132,18 @@ public class ArrayPredictionContext extends PredictionContext {
@Override
public String toString() {
if ( isEmpty() ) return "[]";
StringBuilder buf = new StringBuilder();
buf.append("[");
for (int i=0; i<invokingStates.length; i++) {
if ( i>0 ) buf.append(",");
buf.append(invokingStates[i]);
buf.append(parents[i].toString());
if ( parents[i]!=null ) {
buf.append(parents[i].toString());
}
else {
buf.append("null");
}
}
buf.append("]");
return buf.toString();

View File

@ -35,6 +35,6 @@ public class EmptyPredictionContext extends SingletonPredictionContext {
@Override
public String toString() {
return "$";
return "";
}
}

View File

@ -930,15 +930,16 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
boolean greedy, boolean loopsSimulateTailRecursion)
{
final int initialDepth = 0;
closure(config, configs, closureBusy, collectPredicates, greedy, loopsSimulateTailRecursion, initialDepth);
closure_(config, configs, closureBusy, collectPredicates, greedy,
loopsSimulateTailRecursion, initialDepth);
}
protected void closure(@NotNull ATNConfig config,
@NotNull ATNConfigSet configs,
@NotNull Set<ATNConfig> closureBusy,
boolean collectPredicates,
boolean greedy, boolean loopsSimulateTailRecursion,
int depth)
protected void closure_(@NotNull ATNConfig config,
@NotNull ATNConfigSet configs,
@NotNull Set<ATNConfig> closureBusy,
boolean collectPredicates,
boolean greedy, boolean loopsSimulateTailRecursion,
int depth)
{
if ( debug ) System.out.println("closure("+config.toString(parser,true)+")");
@ -968,11 +969,11 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
// Make sure we track that we are now out of context.
c.reachesIntoOuterContext = config.reachesIntoOuterContext;
assert depth > Integer.MIN_VALUE;
closure(c, configs, closureBusy, collectPredicates, greedy,
loopsSimulateTailRecursion, depth - 1);
closure_(c, configs, closureBusy, collectPredicates, greedy,
loopsSimulateTailRecursion, depth - 1);
// }
return;
}
return;
}
else {
// else if we have no context info, just chase follow links (if greedy)
@ -990,10 +991,11 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
if ( debug ) System.out.println("Loop back; push "+config.state.stateNumber+", stack="+config.context);
}
else if ( config.state.getClass()==LoopEndState.class ) {
if ( debug ) System.out.println("Loop end; pop, stack="+config.context);
if ( debug ) System.out.print("Loop end; pop, stack="+config.context);
LoopEndState end = (LoopEndState)config.state;
// pop all the way back until we don't see the loopback state anymore
config.context = config.context.popAll(end.loopBackStateNumber, configs.fullCtx);
if ( debug ) System.out.println(" becomes "+config.context);
}
}
@ -1036,7 +1038,8 @@ public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
}
}
closure(c, configs, closureBusy, continueCollecting, greedy, loopsSimulateTailRecursion, newDepth);
closure_(c, configs, closureBusy, continueCollecting, greedy,
loopsSimulateTailRecursion, newDepth);
}
}
}

View File

@ -28,7 +28,10 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
public static PredictionContext fromRuleContext(RuleContext outerContext) {
if ( outerContext==null ) outerContext = RuleContext.EMPTY;
if ( outerContext==RuleContext.EMPTY ) {
// if we are in RuleContext of start rule, s, then PredictionContext
// is EMPTY. Nobody called us. (if we are empty, return empty)
if ( outerContext.parent==null || outerContext==RuleContext.EMPTY ) {
return PredictionContext.EMPTY;
}
@ -36,9 +39,7 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
// context of just [s $].
PredictionContext parent = EMPTY;
if ( outerContext.parent != null &&
outerContext.parent.invokingState!=EmptyPredictionContext.EMPTY_INVOKING_STATE )
{
if ( outerContext.parent != null ) {
parent = PredictionContext.fromRuleContext(outerContext.parent);
}
@ -65,11 +66,10 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
protected static int calculateParentHashCode(PredictionContext[] parents) {
int hashCode = 1;
for (PredictionContext context : parents) {
if ( context==null ) {
System.out.println("WHAT!? "+parents.toString());
for (PredictionContext p : parents) {
if ( p!=null ) { // can be null for full ctx stack in ArrayPredictionContext
hashCode = hashCode * 31 ^ p.hashCode();
}
hashCode = hashCode * 31 ^ context.hashCode();
}
return hashCode;
@ -339,8 +339,8 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
mergedInvokingStates[k] = a.invokingStates[i];
i++;
}
else {
mergedParents[k] = a.parents[j];
else { // b > a
mergedParents[k] = b.parents[j];
mergedInvokingStates[k] = b.invokingStates[j];
j++;
}

View File

@ -82,6 +82,8 @@ public class SingletonPredictionContext extends PredictionContext {
@Override
public String toString() {
return String.valueOf(invokingState)+" "+parent.toString();
String up = parent.toString();
if ( up.length()==0 ) return String.valueOf(invokingState);
return String.valueOf(invokingState)+" "+up;
}
}

View File

@ -1,8 +1,3 @@
grammar T;
s : a+ ;
a : {false}? ID {System.out.println("alt 1");}
| {true}? ID {System.out.println("alt 2");}
;
ID : 'a'..'z'+ ;
INT : '0'..'9'+;
WS : (' '|'\n') {skip();} ;
s : 'a' 'b' ;
WS : [ \n]+ -> skip ;