use of org.antlr.analysis.Transition in project antlrworks by antlr.
the class FAFactory method isAlternativeTransitionEndingAtSameState.
public boolean isAlternativeTransitionEndingAtSameState(NFAState state) {
NFAState endState = endStateOfAlternative((NFAState) state.transition(0).target);
for (int t = 1; t < state.getNumberOfTransitions(); t++) {
Transition transition = state.transition(t);
NFAState newEndState = endStateOfAlternative((NFAState) transition.target);
if (!endState.equals(newEndState))
return false;
}
return true;
}
use of org.antlr.analysis.Transition in project antlrworks by antlr.
the class FAFactory method buildRecursiveState.
public FAState buildRecursiveState(NFAState state, Set<NFAState> currentPath) {
if (processedStates.get(state) != null) {
FAState js = processedStates.get(state);
if (currentPath.contains(state)) {
// Set this temporary flag to indicate to the parent method that
// the transition to be created has to be flagged as "loop".
js.loop = true;
}
return js;
}
FAState js = new FAState(state);
processedStates.put(state, js);
currentPath.add(state);
if (state.isAcceptState()) {
// Stop as soon as we reach an accepted state
return js;
}
for (int t = 0; t < state.getNumberOfTransitions(); t++) {
FAState parentState = js;
Transition transition = state.transition(t);
NFAState target = (NFAState) transition.target;
if (targetStateIsInAnotherRule(transition)) {
target = targetStateOfTransition(transition);
parentState = createRuleReferenceState(parentState, transition, null);
}
if (transition.isEpsilon()) {
buildRecursiveSkipState(parentState, target, new HashSet<NFAState>(currentPath), new ArrayList<Integer>());
} else {
FAState targetState = buildRecursiveState(target, new HashSet<NFAState>(currentPath));
if (targetState.loop) {
// Handle "loop" transition by creating a "normal" transition and assigning a flag
// to this transition so when drawing it, we can draw the arrow at the right place
// (in the reverse direction)
targetState.addTransition(new FATransition(transition.label.toString(g), parentState), true);
targetState.loop = false;
} else
parentState.addTransition(new FATransition(transition.label.toString(g), targetState));
}
}
return js;
}
use of org.antlr.analysis.Transition in project antlrworks by antlr.
the class FAAnalysis method recursiveAnalysis.
public void recursiveAnalysis(NFAState state) {
if (processedStates.contains(state))
return;
processedStates.add(state);
for (int t = 0; t < state.getNumberOfTransitions(); t++) {
Transition transition = state.transition(t);
addIncomingTransitionToState((NFAState) transition.target);
recursiveAnalysis((NFAState) transition.target);
}
}
use of org.antlr.analysis.Transition in project antlrworks by antlr.
the class FAFactory method buildRecursiveSkipState.
/** This method is used to skip redundant state following epsilon transition.
*
*/
public void buildRecursiveSkipState(FAState parentState, NFAState state, Set<NFAState> currentPath, List<Integer> skippedStates) {
if (canBeSkipped(state)) {
// If the state can be skipped, apply recursively the same method for each transition(s)
// providing the parent state.
// Record each skipped state. They will be added later to the transition that replace them
Integer skippedState = state.stateNumber;
skippedStates.add(skippedState);
skippedStatesMap.put(skippedState, parentState);
for (int t = 0; t < state.getNumberOfTransitions(); t++) {
Transition transition = state.transition(t);
if (targetStateIsInAnotherRule(transition)) {
NFAState target = targetStateOfTransition(transition);
FAState ruleRefState = createRuleReferenceState(parentState, transition, skippedStates);
buildRecursiveSkipState(ruleRefState, target, currentPath, new ArrayList<Integer>(skippedStates));
} else
buildRecursiveSkipState(parentState, (NFAState) transition.target, currentPath, new ArrayList<Integer>(skippedStates));
}
} else {
// The state cannot be skipped. Build the remaining of the NFA...
FAState targetState = buildRecursiveState(state, currentPath);
// (this is the simplification ;-))
if (targetState.loop) {
// See comment above (in the previous method)
targetState.addTransition(new FATransition(parentState, skippedStates), true);
targetState.loop = false;
} else
parentState.addTransition(new FATransition(targetState, skippedStates));
}
}
Aggregations