The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+10 votes
1.6k views

In a compiler, keywords of a language are recognized during

(A) parsing of the program

(B) the code generation

(C) the lexical analysis of the program

(D) dataflow analysis

asked in Compiler Design by Veteran (96.5k points)
edited | 1.6k views

2 Answers

+23 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
 

answered by Boss (5.5k points)
reshown by
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 ???

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.

+1 vote
answer - C
answered by Boss (9.3k points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

28,947 questions
36,793 answers
91,077 comments
34,690 users