use of org.antlr.v4.automata.ParserATNFactory in project antlr4 by tunnelvisionlabs.
the class TestATNParserPrediction method checkPredictedAlt.
/**
* first check that the ATN predicts right alt.
* Then check adaptive prediction.
*/
public void checkPredictedAlt(LexerGrammar lg, Grammar g, int decision, String inputString, int expectedAlt) {
Tool.internalOption_ShowATNConfigsInDFA = true;
ATN lexatn = createATN(lg, true);
LexerATNSimulator lexInterp = new LexerATNSimulator(lexatn);
IntegerList types = getTokenTypesViaATN(inputString, lexInterp);
System.out.println(types);
semanticProcess(lg);
g.importVocab(lg);
semanticProcess(g);
ParserATNFactory f = new ParserATNFactory(g);
ATN atn = f.createATN();
DOTGenerator dot = new DOTGenerator(g);
Rule r = g.getRule("a");
if (r != null)
System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
r = g.getRule("b");
if (r != null)
System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
r = g.getRule("e");
if (r != null)
System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
r = g.getRule("ifstat");
if (r != null)
System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
r = g.getRule("block");
if (r != null)
System.out.println(dot.getDOT(atn.ruleToStartState[r.index]));
// Check ATN prediction
// ParserATNSimulator<Token> interp = new ParserATNSimulator<Token>(atn);
TokenStream input = new IntTokenStream(types);
ParserInterpreterForTesting interp = new ParserInterpreterForTesting(g, input);
DecisionState startState = atn.decisionToState.get(decision);
DFA dfa = new DFA(startState, decision);
int alt = interp.adaptivePredict(input, decision, ParserRuleContext.emptyContext());
System.out.println(dot.getDOT(dfa, false));
assertEquals(expectedAlt, alt);
// Check adaptive prediction
input.seek(0);
alt = interp.adaptivePredict(input, decision, null);
assertEquals(expectedAlt, alt);
// run 2x; first time creates DFA in atn
input.seek(0);
alt = interp.adaptivePredict(input, decision, null);
assertEquals(expectedAlt, alt);
}
use of org.antlr.v4.automata.ParserATNFactory in project antlr4 by tunnelvisionlabs.
the class TestCodeGeneration method getEvalInfoForString.
public List<String> getEvalInfoForString(String grammarString, String pattern) throws RecognitionException {
ErrorQueue equeue = new ErrorQueue();
Grammar g = new Grammar(grammarString);
List<String> evals = new ArrayList<String>();
if (g.ast != null && !g.ast.hasErrors) {
SemanticPipeline sem = new SemanticPipeline(g);
sem.process();
ATNFactory factory = new ParserATNFactory(g);
if (g.isLexer())
factory = new LexerATNFactory((LexerGrammar) g);
g.atn = factory.createATN();
CodeGenerator gen = new CodeGenerator(g);
ST outputFileST = gen.generateParser();
// STViz viz = outputFileST.inspect();
// try {
// viz.waitForClose();
// }
// catch (Exception e) {
// e.printStackTrace();
// }
boolean debug = false;
DebugInterpreter interp = new DebugInterpreter(outputFileST.groupThatCreatedThisInstance, outputFileST.impl.nativeGroup.errMgr, debug);
InstanceScope scope = new InstanceScope(null, outputFileST);
StringWriter sw = new StringWriter();
AutoIndentWriter out = new AutoIndentWriter(sw);
interp.exec(out, scope);
for (String e : interp.evals) {
if (e.contains(pattern)) {
evals.add(e);
}
}
}
if (equeue.size() > 0) {
System.err.println(equeue.toString());
}
return evals;
}
use of org.antlr.v4.automata.ParserATNFactory in project antlr4 by antlr.
the class TestATNConstruction method checkTokensRule.
void checkTokensRule(LexerGrammar g, String modeName, String expecting) {
// }
if (modeName == null)
modeName = "DEFAULT_MODE";
if (g.modes.get(modeName) == null) {
System.err.println("no such mode " + modeName);
return;
}
ParserATNFactory f = new LexerATNFactory(g);
ATN nfa = f.createATN();
ATNState startState = nfa.modeNameToStartState.get(modeName);
ATNPrinter serializer = new ATNPrinter(g, startState);
String result = serializer.asString();
// System.out.print(result);
assertEquals(expecting, result);
}
Aggregations