got TestATNInterpreter working

This commit is contained in:
Terence Parr 2012-03-19 18:47:41 -07:00
parent 48d663667b
commit 7e9a86a3e1
9 changed files with 37 additions and 29 deletions

View File

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

View File

@ -127,9 +127,15 @@ public class ATNConfigSet implements Set<ATNConfig> {
public boolean add(ATNConfig value) {
Key key = new Key(value);
PredictionContext existing = configToContext.get(key);
if ( existing==null ) return false;
if ( existing==null ) { // nothing there yet; easy, just add
configs.add(value);
configToContext.put(key, value.context);
return true;
}
// a previous (s,i,pi,_), merge with it and save result
PredictionContext merged = PredictionContext.merge(existing, value.context, true);
configToContext.put(key, merged);
configToContext.put(key, merged); // replace
// if already there, must be in configs already
return true;
}
@ -173,7 +179,7 @@ public class ATNConfigSet implements Set<ATNConfig> {
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(super.toString());
buf.append(configs.toString());
if ( hasSemanticContext ) buf.append(",hasSemanticContext="+hasSemanticContext);
if ( uniqueAlt!=ATN.INVALID_ALT_NUMBER ) buf.append(",uniqueAlt="+uniqueAlt);
if ( conflictingAlts!=null ) buf.append(",conflictingAlts="+conflictingAlts);
@ -190,7 +196,10 @@ public class ATNConfigSet implements Set<ATNConfig> {
}
@Override
public boolean addAll(Collection<? extends ATNConfig> c) {
public boolean addAll(Collection<? extends ATNConfig> coll) {
for (ATNConfig c : coll) {
add(c);
}
return false;
}

View File

@ -113,6 +113,13 @@ public class ArrayPredictionContext extends PredictionContext {
@Override
public String toString() {
return Arrays.toString(invokingStates);
StringBuilder buf = new StringBuilder();
buf.append("[");
for (int i=0; i< invokingStates.length; i++) {
buf.append(invokingStates[i]);
buf.append(parents[i].toString());
}
buf.append("[");
return buf.toString();
}
}

View File

@ -93,7 +93,7 @@ public class LL1Analyzer {
@NotNull Set<ATNConfig> lookBusy,
boolean seeThruPreds)
{
System.out.println("_LOOK("+s.stateNumber+", ctx="+ctx);
// System.out.println("_LOOK("+s.stateNumber+", ctx="+ctx);
ATNConfig c = new ATNConfig(s, 0, ctx);
if ( !lookBusy.add(c) ) return;
@ -108,7 +108,7 @@ public class LL1Analyzer {
ATNState invokingState = atn.states.get(p.invokingState);
RuleTransition rt = (RuleTransition)invokingState.transition(0);
ATNState retState = rt.followState;
System.out.println("popping back to "+retState);
// System.out.println("popping back to "+retState);
_LOOK(retState, p.parent, look, lookBusy, seeThruPreds);
}
return;
@ -135,7 +135,7 @@ public class LL1Analyzer {
look.addAll( IntervalSet.of(Token.MIN_USER_TOKEN_TYPE, atn.maxTokenType) );
}
else {
System.out.println("adding "+ t);
// System.out.println("adding "+ t);
IntervalSet set = t.label();
if (set != null) {
if (t instanceof NotSetTransition) {

View File

@ -239,7 +239,7 @@ import java.util.Set;
* holds the decision were evaluating
*/
public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
public static boolean debug = false;
public static boolean debug = true;
public static boolean dfa_debug = false;
public static boolean retry_debug = false;

View File

@ -1,6 +1,5 @@
package org.antlr.v4.runtime.atn;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.misc.Nullable;
@ -86,7 +85,8 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
}
public String toString(@Nullable Recognizer<?,?> recog) {
return toString(recog, ParserRuleContext.EMPTY);
return toString();
// return toString(recog, ParserRuleContext.EMPTY);
}
// recog null unless ParserRuleContext, in which case we use subclass toString(...)

View File

@ -17,7 +17,7 @@ public class SingletonPredictionContext extends PredictionContext {
return new Iterator<SingletonPredictionContext>() {
int i = 0;
@Override
public boolean hasNext() { return i>0; }
public boolean hasNext() { return i==0; }
@Override
public SingletonPredictionContext next() { i++; return self; }
@ -73,6 +73,6 @@ public class SingletonPredictionContext extends PredictionContext {
@Override
public String toString() {
return String.valueOf(invokingState);
return String.valueOf(invokingState)+" "+parent.toString();
}
}

View File

@ -1,13 +1,4 @@
grammar U;
s @after {System.out.println($ctx.toStringTree(this));} : e EOF ;
e : e '.' ID
| e '.' 'this'
| '-' e
| e '*' e
| e ('+'|'-') e
| INT
| ID
;
ID : 'a'..'z'+ ;
INT : '0'..'9'+ ;
WS : (' '|'\n') {skip();} ;
lexer grammar U;
A : 'a' ;
B : 'b' ;
C : 'c' ;

View File

@ -2,7 +2,6 @@ package org.antlr.v4.test;
import org.antlr.v4.automata.ParserATNFactory;
import org.antlr.v4.runtime.NoViableAltException;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.atn.ATN;
import org.antlr.v4.runtime.atn.ATNState;
import org.antlr.v4.runtime.atn.BlockStartState;
@ -285,7 +284,8 @@ public class TestATNInterpreter extends BaseTest {
ParserATNFactory f = new ParserATNFactory(g);
ATN atn = f.createATN();
TokenStream input = new IntTokenStream(types);
IntTokenStream input = new IntTokenStream(types);
System.out.println("input="+input.types);
ParserInterpreter interp = new ParserInterpreter(g, input);
ATNState startState = atn.ruleToStartState[g.getRule("a").index];
if ( startState.transition(0).target instanceof BlockStartState ) {