0% found this document useful (0 votes)
6 views4 pages

Cs 606

Uploaded by

michealcole2812
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Cs 606

Uploaded by

michealcole2812
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Student ID:bc210201541

Assignment 2:

Solution 1:

Role of a Lexical Analyzer in a Compiler

The lexical analyzer is the first phase of a compiler. It reads the source code character by
character and groups the characters into meaningful sequences called tokens. These tokens
represent fundamental syntactic elements like keywords, operators, identifiers, constants, etc.
The lexical analyzer performs the following roles:

1. Tokenization: Converts the input source code into tokens.


2. Eliminating Whitespaces and Comments: Ignores unnecessary spaces, tabs, and
comments.
3. Error Reporting: Detects and reports lexical errors, such as invalid symbols.
4. Symbol Table Interaction: Passes identifiers and their information to the symbol table.

Phases of Lexical Analysis

1. Input Buffering: Reads the input program in chunks to handle large files efficiently.
2. Scanning: Scans the characters and groups them into tokens.
3. Pattern Matching: Matches sequences of characters to pre-defined patterns (regular
expressions) for tokens like keywords, operators, or identifiers.
4. Token Generation: Creates tokens (e.g., id, num, keyword) and passes them to the
syntax analyzer.

Example Code Snippet in C

C Code:

int main() {
int x = 10;
float y = 20.5;
printf("Sum: %f", x + y);
return 0;
}

Breakdown by Lexical Analyzer:

Token Type Lexeme


int Keyword int
main Identifier main
( Punctuation (
Token Type Lexeme
) Punctuation )
{ Punctuation {
int Keyword int
x Identifier x
= Operator =
10 Constant 10
; Punctuation ;
float Keyword float
y Identifier y
= Operator =
20.5 Constant 20.5
; Punctuation ;
printf Identifier printf
( Punctuation (
"Sum: %f" String Literal "Sum: %f"
, Punctuation ,
x Identifier x
+ Operator +
y Identifier y
) Punctuation )
; Punctuation ;
return Keyword return
0 Constant 0
; Punctuation ;
} Punctuation }

Conclusion

The lexical analyzer's output serves as input to the syntax analyzer, forming the foundation for
the rest of the compilation process. Its primary focus is ensuring that the source code is
transformed into a structured sequence of tokens for further processing.

Solution 2:

Given Code Snippet:


int x = 20;
if (x > 10) {
x = x + 5;
}

Breakdown into Lexemes and Tokens:

Line 1: int x = 20;

Lexeme Token Description

int Keyword Specifies a data type

x Identifier Variable name

= Operator Assignment operator

20 Constant Integer literal

; Punctuation Statement terminator

Line 2: if (x > 10) {

Lexeme Token Description

if Keyword Conditional statement

( Punctuation Open parenthesis

x Identifier Variable name

> Operator Relational operator

10 Constant Integer literal

) Punctuation Close parenthesis

{ Punctuation Open curly brace

Line 3: x = x + 5;

Lexeme Token Description

x Identifier Variable name


Lexeme Token Description

= Operator Assignment operator

x Identifier Variable name

+ Operator Arithmetic operator

5 Constant Integer literal

; Punctuation Statement terminator

Line 4: }

Lexeme Token Description

} Punctuation Close curly brace

Summary of Tokens:

1. Keywords: int, if
2. Identifiers: x (occurs multiple times)
3. Operators: =, >, +
4. Constants: 20, 10, 5
5. Punctuation: (, ), {, }, ;

Purpose:

The lexical analyzer will generate these tokens from the input code, removing unnecessary
whitespaces and comments, and then pass the tokens to the syntax analyzer for parsing.

You might also like