0% found this document useful (0 votes)
12 views3 pages

EX - No1 Question

The document describes designing a lexical analyzer for a simplified programming language that includes arrays, strings, conditional statements, loops, user defined types, and ignores comments. It provides sample input code that declares a struct, initializes an array, uses a for loop with if/else statements to print even and odd numbers, and returns 0 at the end of main. The expected output is a list of all the tokens identified in the code with their types and lexemes.

Uploaded by

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

EX - No1 Question

The document describes designing a lexical analyzer for a simplified programming language that includes arrays, strings, conditional statements, loops, user defined types, and ignores comments. It provides sample input code that declares a struct, initializes an array, uses a for loop with if/else statements to print even and odd numbers, and returns 0 at the end of main. The expected output is a list of all the tokens identified in the code with their types and lexemes.

Uploaded by

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

Exercise : No -1

Design a lexical analyzer for a simplified programming language that includes, arrays , strings ,
conditional statements (if-else) with comparison operators , loops (while and for) , user defined types
(structs) , ignore single-line and multi-line comments . Your task is to implement a lexical analyzer that
reads the input source code from the file then identifies and categorizes tokens (Keywords, Identifiers,
Constants, Operators, Delimiters), and handles nested constructs like nested functions, loops, and
conditional statements.

INPUT FORMAT :

#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
int numbers[] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
if (numbers[i] % 2 == 0) {
printf("Even: %d\n", numbers[i]);
} else {
printf("Odd: %d\n", numbers[i]);
}
}
return 0;
}

Output :
Type: KEYWORD, Lexeme: #include Type: DELIMITER, Lexeme: {
Type: DELIMITER, Lexeme: < Type: CONSTANT, Lexeme: 5
Type: IDENTIFIER, Lexeme: stdio.h Type: DELIMITER, Lexeme: ]
Type: DELIMITER, Lexeme: > Type: OPERATOR, Lexeme: =
Type: KEYWORD, Lexeme: struct Type: DELIMITER, Lexeme: {
Type: IDENTIFIER, Lexeme: Point Type: CONSTANT, Lexeme: 1
Type: DELIMITER, Lexeme: { Type: DELIMITER, Lexeme: ,
Type: KEYWORD, Lexeme: int Type: CONSTANT, Lexeme: 2
Type: IDENTIFIER, Lexeme: x Type: DELIMITER, Lexeme: ,
Type: DELIMITER, Lexeme: ; Type: CONSTANT, Lexeme: 3
Type: KEYWORD, Lexeme: int Type: DELIMITER, Lexeme: ,
Type: IDENTIFIER, Lexeme: y Type: CONSTANT, Lexeme: 4
Type: DELIMITER, Lexeme: ; Type: DELIMITER, Lexeme: ,
Type: DELIMITER, Lexeme: }; Type: CONSTANT, Lexeme: 5
Type: KEYWORD, Lexeme: int Type: DELIMITER, Lexeme: }
Type: KEYWORD, Lexeme: main Type: DELIMITER, Lexeme: ;
Type: DELIMITER, Lexeme: ( Type: KEYWORD, Lexeme: for
Type: DELIMITER, Lexeme: ) Type: DELIMITER, Lexeme: (
Type: DELIMITER, Lexeme: { Type: KEYWORD, Lexeme: int
Type: KEYWORD, Lexeme: int Type: IDENTIFIER, Lexeme: i
Type: IDENTIFIER, Lexeme: numbers Type: OPERATOR, Lexeme: =
Type: DELIMITER, Lexeme: [ Type: CONSTANT, Lexeme: 0
Type: IDENTIFIER, Lexeme: i Type: DELIMITER, Lexeme: ;
Type: OPERATOR, Lexeme: < Type: DELIMITER, Lexeme: (
Type: CONSTANT, Lexeme: 5 Type: STRING_LITERAL, Lexeme: "Odd: %d\n"
Type: DELIMITER, Lexeme: ; Type: DELIMITER, Lexeme: ,
Type: IDENTIFIER, Lexeme: i Type: IDENTIFIER, Lexeme: numbers
Type: OPERATOR, Lexeme: ++ Type: DELIMITER, Lexeme: [
Type: DELIMITER, Lexeme: ) Type: IDENTIFIER, Lexeme: i
Type: DELIMITER, Lexeme: { Type: DELIMITER, Lexeme: ]
Type: KEYWORD, Lexeme: if Type: DELIMITER, Lexeme: )
Type: DELIMITER, Lexeme: ( Type: DELIMITER, Lexeme: ;
Type: IDENTIFIER, Lexeme: numbers Type: DELIMITER, Lexeme: }
Type: DELIMITER, Lexeme: [ Type: DELIMITER, Lexeme: }
Type: IDENTIFIER, Lexeme: i Type: KEYWORD, Lexeme: return
Type: DELIMITER, Lexeme: ] Type: CONSTANT, Lexeme: 0
Type: OPERATOR, Lexeme: % Type: DELIMITER, Lexeme: ;
Type: CONSTANT, Lexeme: 2 Type: DELIMITER, Lexeme: }
Type: OPERATOR, Lexeme: == Type: IDENTIFIER, Lexeme: printf
Type: CONSTANT, Lexeme: 0 Type: END_OF_FILE, Lexeme:
Type: DELIMITER, Lexeme: )
Type: DELIMITER, Lexeme: {
Type: IDENTIFIER, Lexeme: printf
Type: DELIMITER, Lexeme: (
Type: STRING_LITERAL, Lexeme: "Even: %d\n"
Type: DELIMITER, Lexeme: ,
Type: IDENTIFIER, Lexeme: numbers
Type: DELIMITER, Lexeme: [
Type: IDENTIFIER, Lexeme: i
Type: DELIMITER, Lexeme: ]
Type: DELIMITER, Lexeme: )
Type: DELIMITER, Lexeme: ;
Type: DELIMITER, Lexeme: }
Type: KEYWORD, Lexeme: else

You might also like