|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.sc3d.apt.sss.v3.NDFA
public class NDFA
Represents a non-determinstic finite automaton for parsing a particular grammar. Any Grammar can be converted into an NDFA using the 'make()' method. Unless your requirements are unusual, you probably don't need to use NDFAs directly, as Parser wraps it all up for you.
An NDFA consists of:
Every sentence of the grammar corresponds to sequence of transitions that leads from the initial state to the final state. If the construction steps represented by the transitions are performed in order, the effect is to construct the parse-tree of the sentence. Alternatively, reading only the 'shift' steps (see below) lists the Tokens of the sentence in order.
Thus to parse a sentence it suffices to find a suitable path from the initial to the final state. To resolve ambiguity in the grammar, the transitions from each state are ordered from most preferred to least preferred.
For the purposes of this class, a depth-first construction of a Tree is one in which every non-terminal is constructed before its child Trees, and in which children are constructed in left-to-right order. Thus at an intermediate stage of the construction there may be many Trees, and the right-hand Tree at each level may not yet have all of its children.
Each step in a depth-first construction takes such a list of Trees and applies a simple modification to it. The effect of all the steps together is to turn an empty list into a list containing exactly one tree, which is the final parse-tree.
In addition to no-op transitions, which do nothing, there are three kinds of Transition:
States are represented by instances of NDFA.State, and transitions are represented by instances of NDFA.Transition. Every State knows the Transitions that lead from it, and every Transition knows the State it leads to. It is therefore possible to follow paths through an NDFA forwards only. Every NDFA knows its initial and final States, but States do not know to which NDFA they belong. It is therefore possible for NDFAs to share States.
The various different kinds of Transition are all represented by instances of Transition, and are distinguished according to which fields are set to non-null values. For example, a Transition is a no-op if all of its fields (apart from 'to') are 'null'.
Nested Class Summary | |
---|---|
static class |
NDFA.State
Represents a state of an NDFA. |
static class |
NDFA.Transition
Represents a transition of an NDFA. |
Field Summary | |
---|---|
NDFA.State |
finalState
The final State of this NDFA. |
NDFA.State |
initialState
The initial State of this NDFA. |
Constructor Summary | |
---|---|
NDFA(NDFA.State initialState,
NDFA.State finalState)
Constructs an NDFA, given its initial and final States. |
Method Summary | |
---|---|
static NDFA |
make(NDFA.State init,
NDFA.State fin,
Grammar grammar)
Constructs and returns an NDFA representing 'grammar'. |
static NDFA |
makeProduction(NDFA.State init,
NDFA.State fin,
Grammar.Production production)
Constructs and returns an NDFA representing 'production'. |
java.lang.String |
toString()
Returns a String representation of this NDFA, including all States reachable from 'this.initialState'. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public final NDFA.State initialState
public final NDFA.State finalState
Constructor Detail |
---|
public NDFA(NDFA.State initialState, NDFA.State finalState)
Method Detail |
---|
public static NDFA make(NDFA.State init, NDFA.State fin, Grammar grammar)
The returned NDFA will have the property that any two different paths with the same start and end points both contain shift transitions. In particular, every cycle includes at least one shift transition.
The initial and final States for the new NDFA are passed as arguments. All other States are freshly constructed. If you want the initial and/or final States freshly constructed, just pass 'new State()'. Outgoing transitions are added to the initial State and for repeatable NonTerminals also to the final State.
init
- the desired initial State.fin
- the desired final State.grammar
- the Grammar to represent.public static NDFA makeProduction(NDFA.State init, NDFA.State fin, Grammar.Production production)
The returned NDFA will has the property that any two different paths with the same start and end points both contain shift transitions. In particular, every cycle includes at least one shift transition.
The initial and final States for the new NDFA are passed as arguments. All other States are freshly constructed. If you want the initial and/or final States freshly constructed, just pass 'new State()'. Outgoing transitions are added to the initial State but not to the final State.
init
- the desired initial State.fin
- the desired final State.production
- the Grammar.Production to represent.public java.lang.String toString()
toString
in class java.lang.Object
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |