1
$\begingroup$

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?

$\endgroup$
1
$\begingroup$

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).

$\endgroup$
  • $\begingroup$ 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 . $\endgroup$ – Sagar P Nov 29 '17 at 12:32
  • 1
    $\begingroup$ Yes, that part would be handled by type checker. $\endgroup$ – fade2black Nov 29 '17 at 12:36
  • $\begingroup$ 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? $\endgroup$ – Sagar P Nov 29 '17 at 12:38
  • 1
    $\begingroup$ 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 $\endgroup$ – fade2black Nov 29 '17 at 12:50
  • $\begingroup$ Thanks a lot !! Could you please answer this compiler question too (based on symbol table )cs.stackexchange.com/questions/84471/… $\endgroup$ – Sagar P Nov 29 '17 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.