Top Description Fields Constructors Methods
org.antlr.tool

public Class FASerializer

extends Object
Class Inheritance
Imports
org.antlr.analysis.*, org.antlr.misc.Utils, java.util.*

An aspect of FA (finite automata) that knows how to dump them to serialized strings.

Field Summary

Modifier and TypeField and Description
protected Grammar
protected Set<State>
markedStates

To prevent infinite recursion when walking state machines, record which states we've visited.

protected int
stateCounter

Each state we walk will get a new state number for serialization purposes.

protected Map<State, Integer>
stateNumberTranslator

Rather than add a new instance variable to NFA and DFA just for serializing machines, map old state numbers to new state numbers by a State object → Integer new state number HashMap.

Constructor Summary

AccessConstructor and Description
public
FASerializer(Grammar grammar)

This aspect is associated with a grammar; used to get token names

Method Summary

Modifier and TypeMethod and Description
private String
public String
public String
serialize(State s, boolean renumber)

Return a string representation of a state machine.

protected void
walkFANormalizingStateNumbers(State s)

In stateNumberTranslator, get a map from State to new, normalized state number.

protected void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

grammarback to summary
protected Grammar grammar
markedStatesback to summary
protected Set<State> markedStates

To prevent infinite recursion when walking state machines, record which states we've visited. Make a new set every time you start walking in case you reuse this object. Multiple threads will trash this shared variable. Use a different FASerializer per thread.

stateCounterback to summary
protected int stateCounter

Each state we walk will get a new state number for serialization purposes. This is the variable that tracks state numbers.

stateNumberTranslatorback to summary
protected Map<State, Integer> stateNumberTranslator

Rather than add a new instance variable to NFA and DFA just for serializing machines, map old state numbers to new state numbers by a State object → Integer new state number HashMap.

Constructor Detail

FASerializerback to summary
public FASerializer(Grammar grammar)

This aspect is associated with a grammar; used to get token names

Method Detail

getStateStringback to summary
private String getStateString(int n, State s)
serializeback to summary
public String serialize(State s)
serializeback to summary
public String serialize(State s, boolean renumber)

Return a string representation of a state machine. Two identical NFAs or DFAs will have identical serialized representations. The state numbers inside the state are not used; instead, a new number is computed and because the serialization will walk the two machines using the same specific algorithm, then the state numbers will be identical. Accept states are distinguished from regular states.

walkFANormalizingStateNumbersback to summary
protected void walkFANormalizingStateNumbers(State s)

In stateNumberTranslator, get a map from State to new, normalized state number. Used by walkSerializingFA to make sure any two identical state machines will serialize the same way.

walkSerializingFAback to summary
protected void walkSerializingFA(List<String> lines, State s)