From 58ef729be58d986017215528d6141d9697b64e24 Mon Sep 17 00:00:00 2001 From: Terence Parr Date: Tue, 21 Feb 2012 12:47:34 -0800 Subject: [PATCH] renamed next to target. factored out to methods, commented code. --- .../v4/runtime/atn/LexerATNSimulator.java | 79 +++++++++++++------ 1 file changed, 55 insertions(+), 24 deletions(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java b/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java index f29319fa9..749a0a0a1 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java +++ b/runtime/Java/src/org/antlr/v4/runtime/atn/LexerATNSimulator.java @@ -297,42 +297,47 @@ public class LexerATNSimulator extends ATNSimulator { traceLookahead1(); int t = input.LA(1); - DFAState s = ds0; + DFAState s = ds0; // s is current/from DFA state while ( true ) { // while more work if ( debug ) { System.out.format("in reach starting closure: %s\n", closure); } - DFAState next = null; + // As we move src->trg, src->trg, we keep track of the previous trg to + // avoid looking up the DFA state again, which is expensive. + // If the previous target was already part of the DFA, we might + // be able to avoid doing a reach operation upon t. If s!=null, + // it means that semantic predicates didn't prevent us from + // creating a DFA state. Once we know s!=null, we check to see if + // the DFA state has an edge already for t. If so, we can just reuse + // it's configuration set; there's no point in re-computing it. + // This is kind of like doing DFA simulation within the ATN + // simulation because DFA simulation is really just a way to avoid + // computing reach/closure sets. Technically, once we know that + // we have a previously added DFA state, we could jump over to + // the DFA simulator. But, that would mean popping back and forth + // a lot and making things more complicated algorithmically. + // This optimization makes a lot of sense for loops within DFA. + // A character will take us back to an existing DFA state + // that already has lots of edges out of it. e.g., .* in comments. + DFAState target = null; if (s != null) { if ( s.edges != null && t < s.edges.length && t > CharStream.EOF ) { closure = s.configset; - next = s.edges[t]; - if (next != null) { - reach = next.configset; + target = s.edges[t]; + if (target != null) { + reach = target.configset; } } } - if (next == null) { - for (ATNConfig c : closure) { - if ( debug ) { - System.out.format("testing %s at %s\n", getTokenName(t), c.toString(recog, true)); - } - - int n = c.state.getNumberOfTransitions(); - for (int ti=0; ti