forked from jasder/antlr
got TestATNInterpreter working
This commit is contained in:
parent
48d663667b
commit
7e9a86a3e1
|
@ -170,8 +170,9 @@ public class ATNConfig {
|
||||||
buf.append(alt);
|
buf.append(alt);
|
||||||
}
|
}
|
||||||
if ( context!=null ) {
|
if ( context!=null ) {
|
||||||
buf.append(",");
|
buf.append(",[");
|
||||||
buf.append(context.toString(recog));
|
buf.append(context.toString(recog));
|
||||||
|
buf.append("]");
|
||||||
}
|
}
|
||||||
if ( semanticContext!=null && semanticContext != SemanticContext.NONE ) {
|
if ( semanticContext!=null && semanticContext != SemanticContext.NONE ) {
|
||||||
buf.append(",");
|
buf.append(",");
|
||||||
|
|
|
@ -127,9 +127,15 @@ public class ATNConfigSet implements Set<ATNConfig> {
|
||||||
public boolean add(ATNConfig value) {
|
public boolean add(ATNConfig value) {
|
||||||
Key key = new Key(value);
|
Key key = new Key(value);
|
||||||
PredictionContext existing = configToContext.get(key);
|
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);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,7 +179,7 @@ public class ATNConfigSet implements Set<ATNConfig> {
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder buf = new StringBuilder();
|
StringBuilder buf = new StringBuilder();
|
||||||
buf.append(super.toString());
|
buf.append(configs.toString());
|
||||||
if ( hasSemanticContext ) buf.append(",hasSemanticContext="+hasSemanticContext);
|
if ( hasSemanticContext ) buf.append(",hasSemanticContext="+hasSemanticContext);
|
||||||
if ( uniqueAlt!=ATN.INVALID_ALT_NUMBER ) buf.append(",uniqueAlt="+uniqueAlt);
|
if ( uniqueAlt!=ATN.INVALID_ALT_NUMBER ) buf.append(",uniqueAlt="+uniqueAlt);
|
||||||
if ( conflictingAlts!=null ) buf.append(",conflictingAlts="+conflictingAlts);
|
if ( conflictingAlts!=null ) buf.append(",conflictingAlts="+conflictingAlts);
|
||||||
|
@ -190,7 +196,10 @@ public class ATNConfigSet implements Set<ATNConfig> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean addAll(Collection<? extends ATNConfig> c) {
|
public boolean addAll(Collection<? extends ATNConfig> coll) {
|
||||||
|
for (ATNConfig c : coll) {
|
||||||
|
add(c);
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -113,6 +113,13 @@ public class ArrayPredictionContext extends PredictionContext {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,7 +93,7 @@ public class LL1Analyzer {
|
||||||
@NotNull Set<ATNConfig> lookBusy,
|
@NotNull Set<ATNConfig> lookBusy,
|
||||||
boolean seeThruPreds)
|
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);
|
ATNConfig c = new ATNConfig(s, 0, ctx);
|
||||||
if ( !lookBusy.add(c) ) return;
|
if ( !lookBusy.add(c) ) return;
|
||||||
|
|
||||||
|
@ -108,7 +108,7 @@ public class LL1Analyzer {
|
||||||
ATNState invokingState = atn.states.get(p.invokingState);
|
ATNState invokingState = atn.states.get(p.invokingState);
|
||||||
RuleTransition rt = (RuleTransition)invokingState.transition(0);
|
RuleTransition rt = (RuleTransition)invokingState.transition(0);
|
||||||
ATNState retState = rt.followState;
|
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);
|
_LOOK(retState, p.parent, look, lookBusy, seeThruPreds);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -135,7 +135,7 @@ public class LL1Analyzer {
|
||||||
look.addAll( IntervalSet.of(Token.MIN_USER_TOKEN_TYPE, atn.maxTokenType) );
|
look.addAll( IntervalSet.of(Token.MIN_USER_TOKEN_TYPE, atn.maxTokenType) );
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
System.out.println("adding "+ t);
|
// System.out.println("adding "+ t);
|
||||||
IntervalSet set = t.label();
|
IntervalSet set = t.label();
|
||||||
if (set != null) {
|
if (set != null) {
|
||||||
if (t instanceof NotSetTransition) {
|
if (t instanceof NotSetTransition) {
|
||||||
|
|
|
@ -239,7 +239,7 @@ import java.util.Set;
|
||||||
* holds the decision were evaluating
|
* holds the decision were evaluating
|
||||||
*/
|
*/
|
||||||
public class ParserATNSimulator<Symbol extends Token> extends ATNSimulator {
|
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 dfa_debug = false;
|
||||||
public static boolean retry_debug = false;
|
public static boolean retry_debug = false;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package org.antlr.v4.runtime.atn;
|
package org.antlr.v4.runtime.atn;
|
||||||
|
|
||||||
import org.antlr.v4.runtime.ParserRuleContext;
|
|
||||||
import org.antlr.v4.runtime.Recognizer;
|
import org.antlr.v4.runtime.Recognizer;
|
||||||
import org.antlr.v4.runtime.RuleContext;
|
import org.antlr.v4.runtime.RuleContext;
|
||||||
import org.antlr.v4.runtime.misc.Nullable;
|
import org.antlr.v4.runtime.misc.Nullable;
|
||||||
|
@ -86,7 +85,8 @@ public abstract class PredictionContext implements Iterable<SingletonPredictionC
|
||||||
}
|
}
|
||||||
|
|
||||||
public String toString(@Nullable Recognizer<?,?> recog) {
|
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(...)
|
// recog null unless ParserRuleContext, in which case we use subclass toString(...)
|
||||||
|
|
|
@ -17,7 +17,7 @@ public class SingletonPredictionContext extends PredictionContext {
|
||||||
return new Iterator<SingletonPredictionContext>() {
|
return new Iterator<SingletonPredictionContext>() {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() { return i>0; }
|
public boolean hasNext() { return i==0; }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SingletonPredictionContext next() { i++; return self; }
|
public SingletonPredictionContext next() { i++; return self; }
|
||||||
|
@ -73,6 +73,6 @@ public class SingletonPredictionContext extends PredictionContext {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.valueOf(invokingState);
|
return String.valueOf(invokingState)+" "+parent.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,4 @@
|
||||||
grammar U;
|
lexer grammar U;
|
||||||
s @after {System.out.println($ctx.toStringTree(this));} : e EOF ;
|
A : 'a' ;
|
||||||
e : e '.' ID
|
B : 'b' ;
|
||||||
| e '.' 'this'
|
C : 'c' ;
|
||||||
| '-' e
|
|
||||||
| e '*' e
|
|
||||||
| e ('+'|'-') e
|
|
||||||
| INT
|
|
||||||
| ID
|
|
||||||
;
|
|
||||||
ID : 'a'..'z'+ ;
|
|
||||||
INT : '0'..'9'+ ;
|
|
||||||
WS : (' '|'\n') {skip();} ;
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ package org.antlr.v4.test;
|
||||||
|
|
||||||
import org.antlr.v4.automata.ParserATNFactory;
|
import org.antlr.v4.automata.ParserATNFactory;
|
||||||
import org.antlr.v4.runtime.NoViableAltException;
|
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.ATN;
|
||||||
import org.antlr.v4.runtime.atn.ATNState;
|
import org.antlr.v4.runtime.atn.ATNState;
|
||||||
import org.antlr.v4.runtime.atn.BlockStartState;
|
import org.antlr.v4.runtime.atn.BlockStartState;
|
||||||
|
@ -285,7 +284,8 @@ public class TestATNInterpreter extends BaseTest {
|
||||||
ParserATNFactory f = new ParserATNFactory(g);
|
ParserATNFactory f = new ParserATNFactory(g);
|
||||||
ATN atn = f.createATN();
|
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);
|
ParserInterpreter interp = new ParserInterpreter(g, input);
|
||||||
ATNState startState = atn.ruleToStartState[g.getRule("a").index];
|
ATNState startState = atn.ruleToStartState[g.getRule("a").index];
|
||||||
if ( startState.transition(0).target instanceof BlockStartState ) {
|
if ( startState.transition(0).target instanceof BlockStartState ) {
|
||||||
|
|
Loading…
Reference in New Issue