use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.
the class TimeLexerSpeed method lex_new_java_utf8.
public void lex_new_java_utf8(int n, boolean clearLexerDFACache) throws Exception {
ClassLoader loader = TimeLexerSpeed.class.getClassLoader();
InputStream is = loader.getResourceAsStream(Parser_java_file);
try {
long size = getResourceSize(loader, Parser_java_file);
CharStream input = CharStreams.fromStream(is, Charset.forName("UTF-8"), Parser_java_file, size);
JavaLexer lexer = new JavaLexer(input);
double avg = tokenize(lexer, n, clearLexerDFACache);
String currentMethodName = new Exception().getStackTrace()[0].getMethodName();
if (output)
System.out.printf("%27s average time %5dus over %4d runs of %5d symbols%s\n", currentMethodName, (int) avg, n, input.size(), clearLexerDFACache ? " DFA cleared" : "");
} finally {
is.close();
}
}
use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.
the class ATN method clearDFA.
public final void clearDFA() {
decisionToDFA = new DFA[decisionToState.size()];
for (int i = 0; i < decisionToDFA.length; i++) {
decisionToDFA[i] = new DFA(decisionToState.get(i), i);
}
modeToDFA = new DFA[modeToStartState.size()];
for (int i = 0; i < modeToDFA.length; i++) {
modeToDFA[i] = new DFA(modeToStartState.get(i));
}
contextCache.clear();
LL1Table.clear();
}
use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.
the class ATN method defineMode.
public void defineMode(@NotNull String name, @NotNull TokensStartState s) {
modeNameToStartState.put(name, s);
modeToStartState.add(s);
modeToDFA = Arrays.copyOf(modeToDFA, modeToStartState.size());
modeToDFA[modeToDFA.length - 1] = new DFA(s);
defineDecisionState(s);
}
use of org.antlr.v4.runtime.dfa.DFA 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.runtime.dfa.DFA in project antlr4 by tunnelvisionlabs.
the class TestATNParserPrediction method testRecursiveLeftPrefix.
@Test
public void testRecursiveLeftPrefix() throws Exception {
LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n" + "LP : '(' ;\n" + "RP : ')' ;\n" + "INT : '0'..'9'+ ;\n");
Grammar g = new Grammar("parser grammar T;\n" + "tokens {A,B,C,LP,RP,INT}\n" + "a : e B | e C ;\n" + "e : LP e RP\n" + " | INT\n" + " ;");
int decision = 0;
checkPredictedAlt(lg, g, decision, "34b", 1);
checkPredictedAlt(lg, g, decision, "34c", 2);
checkPredictedAlt(lg, g, decision, "((34))b", 1);
checkPredictedAlt(lg, g, decision, "((34))c", 2);
// After matching these inputs for decision, what is DFA after each prediction?
String[] inputs = { "34b", "34c", "((34))b", "((34))c" };
String[] dfa = { "s0-INT->s1\n" + "s1-'b'->:s2=>1\n", "s0-INT->s1\n" + "s1-'b'->:s2=>1\n" + "s1-'c'->:s3=>2\n", "s0-'('->s4\n" + "s0-INT->s1\n" + "s1-'b'->:s2=>1\n" + "s1-'c'->:s3=>2\n" + "s4-'('->s5\n" + "s5-INT->s6\n" + "s6-')'->s7\n" + "s7-')'->s1\n", "s0-'('->s4\n" + "s0-INT->s1\n" + "s1-'b'->:s2=>1\n" + "s1-'c'->:s3=>2\n" + "s4-'('->s5\n" + "s5-INT->s6\n" + "s6-')'->s7\n" + "s7-')'->s1\n" };
checkDFAConstruction(lg, g, decision, inputs, dfa);
}
Aggregations