Top Description Inners Fields Constructors Methods
org.antlr.v4.runtime.dfa

public Class DFAState

extends Object
Class Inheritance
Imports
org.antlr.v4.runtime.atn.ATN, .ATNConfigSet, .LexerActionExecutor, .ParserATNSimulator, .PredictionContext, .SemanticContext, org.antlr.v4.runtime.misc.MurmurHash, .NotNull, .Nullable, java.util.Arrays, .BitSet, .Collections, .LinkedHashMap, .Map

A DFA state represents a set of possible ATN configurations. As Aho, Sethi, Ullman p. 117 says "The DFA uses its state to keep track of all possible states the ATN can be in after reading each input symbol. That is to say, after reading input a1a2..an, the DFA is in a state that represents the subset T of the states of the ATN that are reachable from the ATN's start state along some path labeled a1a2..an." In conventional NFA→DFA conversion, therefore, the subset T would be a bitset representing the set of states the ATN could be in. We need to track the alt predicted by each state as well, however. More importantly, we need to maintain a stack of states, tracking the closure operations as they jump from rule to rule, emulating rule invocations (method calls). I have to add a stack to simulate the proper lookahead sequences for the underlying LL grammar from which the ATN was derived.

I use a set of ATNConfig objects not simple states. An ATNConfig is both a state (ala normal conversion) and a RuleContext describing the chain of rules (if any) followed to arrive at that state.

A DFA state may have multiple references to a particular state, but with different ATN contexts (with same or different alts) meaning that state was reached via a different set of rule invocations.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
DFAState.PredPrediction

Map a predicate to a predicted alternative.

Field Summary

Modifier and TypeField and Description
private AcceptStateInfo
public final ATNConfigSet
private volatile AbstractEdgeMap<DFAState>
contextEdges

These keys for these edges are the top level element of the global context.

private BitSet
contextSymbols

Symbols in this set require a global context transition before matching an input symbol.

private volatile AbstractEdgeMap<DFAState>
edges

edges.get(symbol) points to target of symbol.

public DFAState.PredPrediction[]
public int

Constructor Summary

AccessConstructor and Description
public
DFAState(DFA
The DFA.
dfa
,
ATNConfigSet
The set of ATN configurations defining this state.
configs
)

Constructs a new DFAState for a DFA.

public
DFAState(EmptyEdgeMap<DFAState>
The empty edge map.
emptyEdges
,
EmptyEdgeMap<DFAState>
The empty context edge map.
emptyContextEdges
,
ATNConfigSet
The set of ATN configurations defining this state.
configs
)

Constructs a new DFAState with explicit initial values for the outgoing edge and context edge maps.

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Two DFAState instances are equal if their ATN configuration sets are the same.
public final AcceptStateInfo
public Map<Integer, DFAState>
public synchronized DFAState
getContextTarget(int invokingState)

public Map<Integer, DFAState>
public final LexerActionExecutor
public final int
public DFAState
getTarget(int symbol)

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public final boolean
public final boolean
public final boolean
isContextSymbol(int symbol)

public final void
public void
public final void
setContextSymbol(int symbol)

public synchronized void
setContextTarget(int invokingState, DFAState target)

public void
setTarget(int symbol, DFAState target)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

acceptStateInfoback to summary
private AcceptStateInfo acceptStateInfo
configsback to summary
public final ATNConfigSet configs
Annotations
@NotNull
contextEdgesback to summary
private volatile AbstractEdgeMap<DFAState> contextEdges

These keys for these edges are the top level element of the global context.

Annotations
@NotNull
contextSymbolsback to summary
private BitSet contextSymbols

Symbols in this set require a global context transition before matching an input symbol.

Annotations
@Nullable
edgesback to summary
private volatile AbstractEdgeMap<DFAState> edges

edges.get(symbol) points to target of symbol.

Annotations
@NotNull
predicatesback to summary
public DFAState.PredPrediction[] predicates

This list is computed by ParserATNSimulator#predicateDFAState.

Annotations
@Nullable
stateNumberback to summary
public int stateNumber

Constructor Detail

DFAStateback to summary
public DFAState(DFA dfa, ATNConfigSet configs)

Constructs a new DFAState for a DFA.

This constructor initializes the DFA state using empty edge maps provided by the specified DFA.

Parameters
dfa:DFA

The DFA.

configs:ATNConfigSet

The set of ATN configurations defining this state.

DFAStateback to summary
public DFAState(EmptyEdgeMap<DFAState> emptyEdges, EmptyEdgeMap<DFAState> emptyContextEdges, ATNConfigSet configs)

Constructs a new DFAState with explicit initial values for the outgoing edge and context edge maps.

The empty maps provided to this constructor contain information about the range of edge values which are allowed to be stored in the map. Since edges outside the allowed range are simply dropped from the DFA, this offers several potential benefits:

  • In the general case, empty edge maps are initialized to support the range of values expected to be seen during prediction, which is in turn used to minimize the memory overhead associated with a dynamically-constructed DFA.
  • As a special case, empty edge maps can be intentionally constructed so the DFA will not store edges with specific values. In the limit, a range which does not contain any allowed edges can be used to prevent the DFA from storing any edges, forcing the prediction algorithm to recompute all transitions as they are needed.
Parameters
emptyEdges:EmptyEdgeMap<DFAState>

The empty edge map.

emptyContextEdges:EmptyEdgeMap<DFAState>

The empty context edge map.

configs:ATNConfigSet

The set of ATN configurations defining this state.

Method Detail

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Two DFAState instances are equal if their ATN configuration sets are the same. This method is used to see if a state already exists.

Because the number of alternatives and number of ATN configurations are finite, there is a finite number of DFA states that can be processed. This is necessary to show that the algorithm terminates.

Cannot test the DFA state numbers here because in ParserATNSimulator#addDFAState we need to know if any other state exists that has this exact set of ATN configurations. The stateNumber is irrelevant.

Parameters
o:Object

Doc from java.lang.Object.equals.

the reference object with which to compare.

Returns:boolean

Doc from java.lang.Object.equals.

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getAcceptStateInfoback to summary
public final AcceptStateInfo getAcceptStateInfo()
getContextEdgeMapback to summary
public Map<Integer, DFAState> getContextEdgeMap()
getContextTargetback to summary
public synchronized DFAState getContextTarget(int invokingState)
getEdgeMapback to summary
public Map<Integer, DFAState> getEdgeMap()
getLexerActionExecutorback to summary
public final LexerActionExecutor getLexerActionExecutor()
getPredictionback to summary
public final int getPrediction()
getTargetback to summary
public DFAState getTarget(int symbol)
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
isAcceptStateback to summary
public final boolean isAcceptState()
isContextSensitiveback to summary
public final boolean isContextSensitive()
isContextSymbolback to summary
public final boolean isContextSymbol(int symbol)
setAcceptStateback to summary
public final void setAcceptState(AcceptStateInfo acceptStateInfo)
setContextSensitiveback to summary
public void setContextSensitive(ATN atn)
setContextSymbolback to summary
public final void setContextSymbol(int symbol)
setContextTargetback to summary
public synchronized void setContextTarget(int invokingState, DFAState target)
setTargetback to summary
public void setTarget(int symbol, DFAState target)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
org.antlr.v4.runtime.dfa back to summary

public Class DFAState.PredPrediction

extends Object
Class Inheritance

Map a predicate to a predicted alternative.

Field Summary

Modifier and TypeField and Description
public int
public SemanticContext

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

altback to summary
public int alt
predback to summary
public SemanticContext pred
Annotations
@NotNull

Constructor Detail

PredPredictionback to summary
public PredPrediction(SemanticContext pred, int alt)

Method Detail

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override