Lexical analysis
Scanner DFAs
Run a scanner's DFA from its transition table with the table-driven loop, or as hand-coded switch statements.
- Lecture reference: Lexical Analysis IV
- Lecture reference: Lexical Analysis II
Scanner
DFA from
relop: < | <= | <> | > | >= | =. States 4 and 8 (marked *) retract: the character that led there is not part of the lexeme. Lecture reference: Lexical Analysis IV
getToken () call 1 of 1 · from position 0
Stop at the first accepting state (slide 15, as printed).
-
state = start; -
// accept: 1D bool array [state] -
// retract: 1D bool array [state] -
// T: 2D int array [state, char] -
// error: fn(state) -
while (!(accept[state] || error (state))) -
{ -
ch = getChar (); -
state = T[state, ch]; -
} -
if (accept[state]) { -
if (retract[state]) -
ungetChar (ch); -
return tokenFor (state); -
} -
handleError ();
Step 1 of 11
state = start: state is 0.
Input and tokens
- state
- 0
- ch
- —
- position
- 0
Tokens
None yet
DFA
Table T 9 × 4
| State | < | = | > | other | accept | retract |
|---|---|---|---|---|---|---|
| 0 , start | 1 | 5 | 6 | — | — | — |
| 1 | 4 | 2 | 3 | 4 | — | — |
| 2 , accepting | — | — | — | — | LE | — |
| 3 , accepting | — | — | — | — | NE | — |
| 4 , accepting | — | — | — | — | LT | ✓ |
| 5 , accepting | — | — | — | — | EQ | — |
| 6 | 8 | 7 | 8 | 8 | — | — |
| 7 , accepting | — | — | — | — | GE | — |
| 8 , accepting | — | — | — | — | GT | ✓ |
Rows are states, columns are symbol classes (label classes on which every state has the same next state share a column, but a named set such as digit never goes into an unnamed column). — in T: no transition, so T gives the error state. other: every other character; getChar () at the end of the input returns EOF, which is looked up there too. → start, ◎ accepting.