# Lexical analysis and reserved words

My book says lexical analysis detects reserved words.

How can lexical analysis detect whether we are using reserved words in incorrect places ?

For eg :- while(a>0) and int while = 5.

How can lexical anaylsis differentiate the usage of while in both the above examples based on the context?

Briefly, lexical analyzer reads source code character by character and generates tokens. It does not care about in what order tokens are generated. Relationship between tokens are checked by syntax analyzer and specified by a context free grammar. Let me give you a simple example. Consider the following piece of code

 int while = 5


Lexical analyzer emmits the following tokens:

KEYWORD (INT)
KEYWORD (WHILE)
ASSIGN (=)
NUMBER (5)

Then then syntax analyzer checks these tokens, detects (based on the corresponding CFG) that the keyword WHILE cannot be lvalue, and reports the syntax error. In fact the syntax analyzer expects IDENTIFIER after the token KEYWORD (INT).

• Thanks for the answer. I have one more question, suppose we are doing invalid indirection(using pointers) , then it would be a semantic error and not syntax error right ? As syntax error is only concerned with the structure and not the actual meaning . – Sagar P Nov 29 '17 at 12:32
• Yes, that part would be handled by type checker. – fade2black Nov 29 '17 at 12:36
• As syntax analyzer expects a identifier after int, how will CFG exactly identify that it is reserved keyword? I mean while keyword also follows the naming convention of identifier. Do we have separate error productions for each reserved word? – Sagar P Nov 29 '17 at 12:38
• Keywords are detected during lexical analysis. You can treat keywords as special identifiers, i.e., they are first detected as an identifier and then the lexical analyzer additionally checks if it is a keyword (for example by looking up to a symbol table). The keywords may be specified directly in the CFG. For example the While statement could be specified as STMT -> KEYWORD(while) LPAR EXP RPAR COMPOUND_STMT – fade2black Nov 29 '17 at 12:50
• Thanks a lot !! Could you please answer this compiler question too (based on symbol table )cs.stackexchange.com/questions/84471/… – Sagar P Nov 29 '17 at 12:55