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

public Class DFAState

extends Object
Class Inheritance
Imports
org.antlr.v4.runtime.Token, org.antlr.v4.runtime.atn.ATN, .ATNConfig, .ATNConfigSet, .LexerActionExecutor, .ParserATNSimulator, .SemanticContext, org.antlr.v4.runtime.misc.MurmurHash, java.util.Arrays, .HashSet, .Set

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
public ATNConfigSet
public DFAState[]
edges

edges[symbol] points to target of symbol.

public boolean
public LexerActionExecutor
public DFAState.PredPrediction[]
predicates

During SLL parsing, this is a list of predicates associated with the ATN configurations of the DFA state.

public int
prediction

if accept state, what ttype do we match or alt do we predict?

public boolean
requiresFullContext

Indicates that this state was created during SLL prediction that discovered a conflict between the configurations in the state.

public int

Constructor Summary

AccessConstructor and Description
public
public
DFAState(int stateNumber)

public

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 Set<Integer>
getAltSet()

Get the set of all alts mentioned by all ATN configurations in this DFA state.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.
public String
toString()

Overrides java.lang.Object.toString.

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

Field Detail

configsback to summary
public ATNConfigSet configs
edgesback to summary
public DFAState[] edges

edges[symbol] points to target of symbol. Shift up by 1 so (-1) Token#EOF maps to edges[0].

isAcceptStateback to summary
public boolean isAcceptState
lexerActionExecutorback to summary
public LexerActionExecutor lexerActionExecutor
predicatesback to summary
public DFAState.PredPrediction[] predicates

During SLL parsing, this is a list of predicates associated with the ATN configurations of the DFA state. When we have predicates, requiresFullContext is false since full context prediction evaluates predicates on-the-fly. If this is not null, then prediction is ATN#INVALID_ALT_NUMBER.

We only use these for non-requiresFullContext but conflicting states. That means we know from the context (it's $ or we don't dip into outer context) that it's an ambiguity not a conflict.

This list is computed by ParserATNSimulator#predicateDFAState.

predictionback to summary
public int prediction

if accept state, what ttype do we match or alt do we predict? This is set to ATN#INVALID_ALT_NUMBER when predicates!=null or requiresFullContext.

requiresFullContextback to summary
public boolean requiresFullContext

Indicates that this state was created during SLL prediction that discovered a conflict between the configurations in the state. Future ParserATNSimulator#execATN invocations immediately jumped doing full context prediction if this field is true.

stateNumberback to summary
public int stateNumber

Constructor Detail

DFAStateback to summary
public DFAState()
DFAStateback to summary
public DFAState(int stateNumber)
DFAStateback to summary
public DFAState(ATNConfigSet configs)

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
getAltSetback to summary
public Set<Integer> getAltSet()

Get the set of all alts mentioned by all ATN configurations in this DFA state.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this 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
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

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