Class DFA
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal ATNStateFrom which ATN state did we create this DFA?final intprivate static final EmptyEdgeMap<DFAState>This field initializes the outgoing edge map for the special starting DFA state created for precedence DFAs.private final EmptyEdgeMap<DFAState>This is the backing field forgetEmptyContextEdgeMap().private final EmptyEdgeMap<DFAState>This is the backing field forgetEmptyEdgeMap().private final intThis is the backing field forgetMaxDfaEdge().private final intThis is the backing field forgetMinDfaEdge().private final AtomicIntegerprivate final booleantrueif this DFA is for a precedence decision; otherwise,false.final AtomicReference<DFAState>This is the start state for SLL prediction.final AtomicReference<DFAState>This is the start state for full context prediction.final ConcurrentMap<DFAState,DFAState> A set of all DFA states. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidclear()Clears all learned DFA states while reusing thisDFAinstance.Gets an empty edge map initialized with the minimum and maximum context values allowed to be stored in this DFA.Gets an empty edge map initialized with the minimum and maximum symbol values allowed to be stored in this DFA.final intGets the maximum input symbol value which can be stored in this DFA.final intGets the minimum input symbol value which can be stored in this DFA.final DFAStategetPrecedenceStartState(int precedence, boolean fullContext) Get the start state for a specific precedence value.booleanReturnstrueif this DFA has learned at least one full-context start path.booleanisEmpty()Returnstrueif this DFA has not yet learned any useful start state information.final booleanGets whether this DFA is a precedence DFA.final voidsetPrecedenceDfa(boolean precedenceDfa) Deprecated.This method no longer performs any action.final voidsetPrecedenceStartState(int precedence, boolean fullContext, DFAState startState) Set the start state for a specific precedence value.toString()Deprecated.UsetoString(Vocabulary)instead.Deprecated.UsetoString(Vocabulary, String[])instead.toString(Vocabulary vocabulary) toString(Vocabulary vocabulary, String[] ruleNames)
-
Field Details
-
states
A set of all DFA states. UseMapso we can get old state back (Setonly allows you to see if it's there).Note that this collection of states holds the DFA states for both SLL and LL prediction. Only the start state needs to be differentiated for these cases, which is tracked by the
s0ands0fullfields. -
s0
This is the start state for SLL prediction.When
isPrecedenceDfa()istrue, this state is not used directly. Rather,getPrecedenceStartState(int, boolean)is used to obtain the true SLL start state by traversing an outgoing edge corresponding to the current precedence level in the parser. -
s0full
This is the start state for full context prediction.- See Also:
-
decision
public final int decision -
atnStartState
From which ATN state did we create this DFA? -
nextStateNumber
-
minDfaEdge
private final int minDfaEdgeThis is the backing field forgetMinDfaEdge(). -
maxDfaEdge
private final int maxDfaEdgeThis is the backing field forgetMaxDfaEdge(). -
EMPTY_PRECEDENCE_EDGES
This field initializes the outgoing edge map for the special starting DFA state created for precedence DFAs.The range of allowed values assigned to this map represent the precedence levels for which an edge will be stored in the DFA. If a precedence level is encountered outside this range, the DFA will not be able to hold an edge pointing to the start state for the decision, so prediction for this precedence level will always result in recomputing the start state. Should this occur, the existing start state can still be located within
statesso outgoing edges starting from the start state will not be dropped. -
emptyEdgeMap
This is the backing field forgetEmptyEdgeMap(). -
emptyContextEdgeMap
This is the backing field forgetEmptyContextEdgeMap(). -
precedenceDfa
private final boolean precedenceDfatrueif this DFA is for a precedence decision; otherwise,false. This is the backing field forisPrecedenceDfa().
-
-
Constructor Details
-
DFA
Constructs aDFAinstance associated with a lexer mode.The start state for a
DFAconstructed with this method should be aTokensStartState, which is the start state for a lexer mode. The prediction made by this DFA determines the lexer rule which matches the current input.- Parameters:
atnStartState- The start state for the mode.
-
DFA
Constructs aDFAinstance associated with a decision.- Parameters:
atnStartState- The decision associated with this DFA.decision- The decision number.
-
-
Method Details
-
clear
public final void clear()Clears all learned DFA states while reusing thisDFAinstance.Used by
ATN.clearDFA()so repeated clear/rebuild cycles (for example cold-path benchmarks or long-running services that flush prediction caches) avoid reallocating oneDFAper decision and mode. Precedence DFAs re-seed their synthetic start states; other DFAs resets0/s0fulltonull. -
getMinDfaEdge
public final int getMinDfaEdge()Gets the minimum input symbol value which can be stored in this DFA.- Returns:
- The minimum input symbol which can be stored in this DFA.
- See Also:
-
getMaxDfaEdge
public final int getMaxDfaEdge()Gets the maximum input symbol value which can be stored in this DFA.- Returns:
- The maximum input symbol which can be stored in this DFA.
- See Also:
-
getEmptyEdgeMap
Gets an empty edge map initialized with the minimum and maximum symbol values allowed to be stored in this DFA.Setting a range of allowed symbol values for a DFA bounds the memory overhead for storing the map of outgoing edges. The various implementations of
EdgeMapuse this range to determine the best memory savings will be obtained from sparse storage (e.g.SingletonEdgeMaporSparseEdgeMap) or dense storage (ArrayEdgeMap). Symbols values outside the range are supported during prediction, but since DFA edges are never created for these symbols they will always recompute the target state through a match and closure operation.Empty edge maps are immutable objects which track the allowed range of input symbols that can be stored as edges in the DFA. By storing an empty edge map instance in the DFA, new instances of
DFAStatecreated for the DFA can be initialized with a non-null outgoing edge map with the proper symbol range without incurring extra allocations.- Returns:
- The empty edge map used for initializing
DFAStateinstances associated with this DFA.
-
getEmptyContextEdgeMap
Gets an empty edge map initialized with the minimum and maximum context values allowed to be stored in this DFA.The value assigned to a context edge within the DFA is an ATN state number, so the range of allowed values for the context edge map is
ATNState.INVALID_STATE_NUMBERthrough the number of states stored inATN.statesfor the ATN.This empty edge map serves a purpose similar to
getEmptyEdgeMap(). It is used for initializingDFAStateinstances without incurring memory overhead for the (especially) common case where no outgoing context edges are added to the DFA state.- Returns:
- The empty context edge map used for initializing
DFAStateinstances associated with this DFA.
-
isPrecedenceDfa
public final boolean isPrecedenceDfa()Gets whether this DFA is a precedence DFA. Precedence DFAs use a special start states0which is not stored instates. TheDFAState.edgesarray for this start state contains outgoing edges supplying individual start states corresponding to specific precedence values.- Returns:
trueif this is a precedence DFA; otherwise,false.- See Also:
-
getPrecedenceStartState
Get the start state for a specific precedence value.- Parameters:
precedence- The current precedence.- Returns:
- The start state corresponding to the specified precedence, or
nullif no start state exists for the specified precedence. - Throws:
IllegalStateException- if this is not a precedence DFA.- See Also:
-
setPrecedenceStartState
Set the start state for a specific precedence value.- Parameters:
precedence- The current precedence.startState- The start state corresponding to the specified precedence.- Throws:
IllegalStateException- if this is not a precedence DFA.- See Also:
-
setPrecedenceDfa
Deprecated.This method no longer performs any action.Sets whether this is a precedence DFA.- Parameters:
precedenceDfa-trueif this is a precedence DFA; otherwise,false- Throws:
UnsupportedOperationException- ifprecedenceDfadoes not match the value ofisPrecedenceDfa()for the current DFA.
-
isEmpty
public boolean isEmpty()Returnstrueif this DFA has not yet learned any useful start state information.For ordinary (non-precedence) DFAs this is true when both
s0ands0fullare stillnull. For precedence DFAs the synthetic start states always exist, so emptiness is defined by whether those states have any outgoing symbol edges yet.PERF: Precedence-DFA emptiness is answered via
DFAState.isEdgesEmpty()— never viaDFAState.getEdgeMap().isEmpty(), which would allocate a full boxed map on everyParserATNSimulator.adaptivePredict(org.antlr.v4.runtime.TokenStream, int, org.antlr.v4.runtime.ParserRuleContext)call for left-recursive expression decisions (the common case in languages such as Java and Groovy). -
isContextSensitive
public boolean isContextSensitive()Returnstrueif this DFA has learned at least one full-context start path.PERF: Precedence DFAs use
DFAState.isEdgesEmpty()ons0fullinstead of materializingDFAState.getEdgeMap(). -
addState
-
toString
-
toString
Deprecated.UsetoString(Vocabulary)instead. -
toString
-
toString
Deprecated.UsetoString(Vocabulary, String[])instead. -
toString
-
toLexerString
-