CS606 1
CS606 1
Solution
Question 1 (10 Marks)
Explain the role of a lexical analyzer in a compiler. What are the different phases it
involves? Use a small code snippet in C to illustrate how the lexical analyzer breaks
down the source code into tokens.
Solution
Introduction
Compilers begin with the lexical analyzer. Reading the source code and parsing it into a
string of tokens is its principal function. Tokens like keywords, identifiers, operators, and
literals serve as the foundation for later stages of the compiler. Verifying that the source
code follows the language's syntax rules at a basic level is done by the lexical analyzer.
The lexical analyzer would break this code into tokens as follows:
Lexeme Token Type
int Keyword
main Identifier
( Left Parenthesis
) Right Parenthesis
{ Left Brace
int Keyword
a Identifier
= Assignment Operator
10 Integer Constant
; Semicolon
float Keyword
b Identifier
= Assignment Operator
20.5 Float Constant
; Semicolon
a Identifier
= Assignment Operator
a Identifier
+ Addition Operator
b Identifier
; Semicolon
return Keyword
0 Integer Constant
; Semicolon
} Right Brace
Solution
The following table identifies the lexemes and their corresponding tokens for each part of
the code:
Lexeme Token
int KEYWORD
x IDENTIFIER
= ASSIGNMENT_OPERATOR
20 NUMERIC_LITERAL
; SEMICOLON
if KEYWORD
( LEFT_PARENTHESIS
x IDENTIFIER
> RELATIONAL_OPERATOR
10 NUMERIC_LITERAL
) RIGHT_PARENTHESIS
{ LEFT_BRACE
x IDENTIFIER
= ASSIGNMENT_OPERATOR
x IDENTIFIER
+ ARITHMETIC_OPERATOR
5 NUMERIC_LITERAL
; SEMICOLON
} RIGHT_BRACE