in Compiler Design edited by
13,249 views
37 votes
37 votes

In a compiler, keywords of a language are recognized during

  1. parsing of the program
  2. the code generation
  3. the lexical analysis of the program
  4. dataflow analysis
in Compiler Design edited by
13.2k views

2 Answers

54 votes
54 votes
Best answer

Typically, the lexical analysis phase of compilation breaks the input text up into sequences of lexemes that each belongs to some particular token type that's useful in later analysis. Consequently, keywords are usually first recognized during lexical analysis in order to make parsing easier. Since parsers tend to be implemented by writing context-free grammars of tokens rather than of lexemes (that is, the category of the lexeme rather than the contents of the lexeme), it is significantly easier to build a parser when keywords are marked during lexing. Any identifier is also a token so it is recognized in lexical Analysis .
Hence, option C is True.
[email protected] http://stackoverflow.com/questions/5202709/phases-of-a-compiler

edited by

3 Comments

Errors in keywords are realised in parsing .. right ??? Ex If I have typed "retrun" instead of "return" then lexical analysis phase will think that as identifier and send it to parser .... right ???
2
2

It is hard for a lexical analyzer to tell, without the aid of other components, that there is a source-code error. For instance, if the string fi is encountered for the first time in a C program in the context:

fi ( a == f(x) ) ...

a lexical analyzer cannot tell whether fi is a misspelling of the keyword if or an undeclared function identifier. Since fi is a valid lexeme for the token id, the lexical analyzer must return the token id to the parser and let some other phase of the compiler - probably the parser in this case - handle an error due to transposition of the letters.

9
9

lexical analyzer will treat fi as normal token because it does not match with any keyword of c .when syntax analyzer(parser) will ask for new token lexical analyzer​​​​​​​ will give this token with some id then parser will try to make parse tree using predefined rules(grammar ) of C language if it will not able to identify that what it is .then this will be reported to error handler about type of error and column and row no given by lexical analyzer​​​​​​​.

6
6
2 votes
2 votes
In most modern languages, keywords (identifiers that have special meaning to the compiler)
are reserved (meaning user cannot redefine/remove/overload the predefined meaning).
Typically, the symbol table is preloaded with the keywords.

The lexical analyzer first
identifies keywords as identifiers and before inserting them into the symbol table, checks
against the preloaded keywords to correctly classify them as identifiers or keywords.

Therefore, option c is correct.
Answer:

Related questions