Wednesday, June 27, 2012

Recognition of reserved words and identifiers


Recognizing keywords and identifiers presents a problem. Usually, keywords like if or then are reserved, so they are not identifiers even though they look identifiers. Then although we typically use a transition diagram like that. fig to search for identifier lexemes this diagram will also recognize the keyword if then else of an running example.


return (get Token (), install des)
There are two ways that we can handle reserved words that look like identifiers.
      1.  Install the reserved word,in the symbol table initially .A field of the symbol-table entry indicates that  these strings are never  ordinary identifiers ,and tells which token they represent .when we find an identifier a call to install ID places it in the symbol table if it is not already there and returns a pointer t the symbol-table entry for the lexeme found. Of course, any identifier not in the symbol table during lexical analysis cannot    be a reserved word, so its token is id.
      2. Create separate transition diagram for each keyword: an example for the keyword then is shown fig .Note that such a transition diagram consist of states representing the situation after each successive letter of the keyword  is seen .followed by a text for a “non letter-or-digit” .i.e., any character that cannot be the continuation of an identifier.

1 comment: