Javadoc
Add a transition from state d to targetState with label in normal case.
if COLLAPSE_ALL_INCIDENT_EDGES, however, try to merge all edges from
d to targetState; this means merging their labels. Another optimization
is to reduce to a single EOT edge any set of edges from d to targetState
where there exists an EOT state. EOT is like the wildcard so don't
bother to test any other edges. Example:
NUM_INT
: '1'..'9' ('0'..'9')* ('l'|'L')?
| '0' ('x'|'X') ('0'..'9'|'a'..'f'|'A'..'F')+ ('l'|'L')?
| '0' ('0'..'7')* ('l'|'L')?
;
The normal decision to predict alts 1, 2, 3 is:
if ( (input.LA(1)>='1' && input.LA(1)<='9') ) {
alt7=1;
}
else if ( input.LA(1)=='0' ) {
if ( input.LA(2)=='X'||input.LA(2)=='x' ) {
alt7=2;
}
else if ( (input.LA(2)>='0' && input.LA(2)<='7') ) {
alt7=3;
}
else if ( input.LA(2)=='L'||input.LA(2)=='l' ) {
alt7=3;
}
else {
alt7=3;
}
}
else error
Clearly, alt 3 is predicted with extra work since it tests 0..7
and [lL] before finally realizing that any character is actually
ok at k=2.
A better decision is as follows:
if ( (input.LA(1)>='1' && input.LA(1)<='9') ) {
alt7=1;
}
else if ( input.LA(1)=='0' ) {
if ( input.LA(2)=='X'||input.LA(2)=='x' ) {
alt7=2;
}
else {
alt7=3;
}
}
The DFA originally has 3 edges going to the state the predicts alt 3,
but upon seeing the EOT edge (the "else"-clause), this method
replaces the old merged label (which would have (0..7|l|L)) with EOT.
The code generator then leaves alt 3 predicted with a simple else-
clause. :)
The only time the EOT optimization makes no sense is in the Tokens
rule. We want EOT to truly mean you have matched an entire token
so don't bother actually rewinding to execute that rule unless there
are actions in that rule. For now, since I am not preventing
backtracking from Tokens rule, I will simply allow the optimization.