] > Table-Driven Scanners

2.13 Table-Driven Scanners

state = q0  
IF( char undefined ){ char = nextChar(); }  
WHILE( char != EOF ){  
  IF( state == q0 ){  
     str = "";  
     IF( char == ’ ’ || char == ’\n’ ){  
        char = nextChar();  
        CONTINUE;  
  }  }  
  nextState = table[state, charMap[char] ];  
  IF( nextState defined ){  
     str += char;  
     state = nextState;  
     char = nextChar();  
  }  
  ELSE break;  
}  
IF( ! final(state) ){ error(); }

             |------/------|
<blank > |-|||             ||
        |-|   A–Z|   ||-A–Z, |
       - |-q0o --- | q1oo ||||0–9  ||
           | 0–9|    ||-      ||
           |-----| q2oo |||0–9    ||
           |  (,)|      |        ||
           |-----| q3oo             ||
           | +,-|                 ||
           |-----| q4oo               |
           |                       |
           |--*--| q5oo      else|-     |
           |  /           --||*||| | |||
           ------| q6oo --*||-q7o ||||||q8o ||-*
                               else     |-

IF( state ==  q1 ){  
  i = lookid(str)  
  IF( i < 4 ) return(i,)  
  ELSE        return(8,i)  
}

symbol table

0 LET

1 GOTO

2 IF

3 END

 
id

IF( state ==  q2 ){  
  value = parseInt(str)  
  i = lookint( value )  
  return(9,i)  
}

constant table

0
1

int

Note: lookid and lookint introduce the given arguments into the tables, if the values are not yet included in the tables

transition table








  0
A – Z
1
0 – 9
2
(,)
3
+,-
4
*
5
/







q0q1q2q3q4q5q6







q1q1q1







q2   q2







q3







q4







q5







q6         q7  







q7q7q7q7q7q8q7







q8q7q7q7q7q8q0







charMap













AZ09()+-*/












0 011223345