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 · slides 13–20
  • Lecture reference: Lexical Analysis II · slide 13

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 · slide 16

getToken () call 1 of 1 · from position 0

Stop at the first accepting state (slide 15, as printed).

  1. state = start;
  2. // accept: 1D bool array [state]
  3. // retract: 1D bool array [state]
  4. // T: 2D int array [state, char]
  5. // error: fn(state)
  6. while (!(accept[state] || error (state)))
  7. {
  8. ch = getChar ();
  9. state = T[state, ch];
  10. }
  11. if (accept[state]) {
  12. if (retract[state])
  13. ungetChar (ch);
  14. return tokenFor (state);
  15. }
  16. 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

Transition table T with accept and retract
State<=>otheracceptretract
0 , start 156———
1 4234——
2 , accepting————LE—
3 , accepting————NE—
4 , accepting————LT✓
5 , accepting————EQ—
6 8788——
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.