use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by antlr.
the class DOTGenerator method getDOT.
public String getDOT(DFA dfa, boolean isLexer) {
if (dfa.s0 == null)
return null;
ST dot = stlib.getInstanceOf("dfa");
dot.add("name", "DFA" + dfa.decision);
dot.add("startState", dfa.s0.stateNumber);
// dot.add("useBox", Tool.internalOption_ShowATNConfigsInDFA);
dot.add("rankdir", rankdir);
// define stop states first; seems to be a bug in DOT where doublecircle
for (DFAState d : dfa.states.keySet()) {
if (!d.isAcceptState)
continue;
ST st = stlib.getInstanceOf("stopstate");
st.add("name", "s" + d.stateNumber);
st.add("label", getStateLabel(d));
dot.add("states", st);
}
for (DFAState d : dfa.states.keySet()) {
if (d.isAcceptState)
continue;
if (d.stateNumber == Integer.MAX_VALUE)
continue;
ST st = stlib.getInstanceOf("state");
st.add("name", "s" + d.stateNumber);
st.add("label", getStateLabel(d));
dot.add("states", st);
}
for (DFAState d : dfa.states.keySet()) {
if (d.edges != null) {
for (int i = 0; i < d.edges.length; i++) {
DFAState target = d.edges[i];
if (target == null)
continue;
if (target.stateNumber == Integer.MAX_VALUE)
continue;
// we shift up for EOF as -1 for parser
int ttype = i - 1;
String label = String.valueOf(ttype);
if (isLexer)
label = "'" + getEdgeLabel(new StringBuilder().appendCodePoint(i).toString()) + "'";
else if (grammar != null)
label = grammar.getTokenDisplayName(ttype);
ST st = stlib.getInstanceOf("edge");
st.add("label", label);
st.add("src", "s" + d.stateNumber);
st.add("target", "s" + target.stateNumber);
st.add("arrowhead", arrowhead);
dot.add("edges", st);
}
}
}
String output = dot.render();
return Utils.sortLinesInString(output);
}
use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by antlr.
the class TimeLexerSpeed method lex_legacy_grapheme_utf8.
public void lex_legacy_grapheme_utf8(String fileName, int n, boolean clearLexerDFACache) throws Exception {
try (InputStream is = TimeLexerSpeed.class.getClassLoader().getResourceAsStream(PerfDir + "/" + fileName);
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr)) {
CharStream input = new ANTLRInputStream(br);
graphemesLexer lexer = new graphemesLexer(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 from %s%s\n", currentMethodName, (int) avg, n, input.size(), fileName, clearLexerDFACache ? " DFA cleared" : "");
}
}
use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by antlr.
the class TimeLexerSpeed method tokenize.
public double tokenize(Lexer lexer, int n, boolean clearLexerDFACache) {
// always wipe the DFA before we begin tests so previous tests
// don't affect this run!
lexer.getInterpreter().clearDFA();
long[] times = new long[n];
for (int i = 0; i < n; i++) {
lexer.reset();
if (clearLexerDFACache) {
lexer.getInterpreter().clearDFA();
}
long start = System.nanoTime();
CommonTokenStream tokens = new CommonTokenStream(lexer);
// lex whole file.
tokens.fill();
// int size = lexer.getInputStream().size();
long stop = System.nanoTime();
times[i] = (stop - start) / 1000;
// if ( output ) System.out.printf("Tokenized %d char in %dus\n", size, times[i]);
}
Arrays.sort(times);
// drop highest 20% of times
times = Arrays.copyOfRange(times, 0, times.length - (int) (n * .2));
return avg(times);
}
use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by antlr.
the class Parser method getDFAStrings.
/**
* For debugging and other purposes.
*/
public List<String> getDFAStrings() {
synchronized (_interp.decisionToDFA) {
List<String> s = new ArrayList<String>();
for (int d = 0; d < _interp.decisionToDFA.length; d++) {
DFA dfa = _interp.decisionToDFA[d];
s.add(dfa.toString(getVocabulary()));
}
return s;
}
}
use of org.antlr.v4.runtime.dfa.DFA in project antlr4 by antlr.
the class Parser method dumpDFA.
/**
* For debugging and other purposes.
*/
public void dumpDFA() {
synchronized (_interp.decisionToDFA) {
boolean seenOne = false;
for (int d = 0; d < _interp.decisionToDFA.length; d++) {
DFA dfa = _interp.decisionToDFA[d];
if (!dfa.states.isEmpty()) {
if (seenOne)
System.out.println();
System.out.println("Decision " + dfa.decision + ":");
System.out.print(dfa.toString(getVocabulary()));
seenOne = true;
}
}
}
}
Aggregations