Chapter 3: Lexical Analysis

How to handle keywords?

. Consider token DIV and MOD with lexemes div and mod.

. Initialize symbol table with insert( "div" , DIV ) and insert( "mod" , MOD).

. Any subsequent lookup returns a nonzero value, therefore, cannot be used as an identifier .

To handle keywords, we consider the keywords themselves as lexemes. We store all the entries corresponding to keywords in the symbol table while initializing it and do lookup whenever we see a new lexeme. Now, whenever a lookup is done, if a nonzero value is returned, it means that there already exists a corresponding entry in the Symbol Table. So, if someone tries to use a keyword as an identifier, it will not be allowed as an identifier with this name already exists in the Symbol Table. For instance, consider the tokens DIV and MOD with lexemes "div" and "mod". We initialize symbol table with insert("div", DIV) and insert("mod", MOD). Any subsequent lookup now would return a nonzero value, and therefore, neither "div" nor "mod" can be used as an identifier.