Experiment No: 1 Date: _______________
Name: Keer Mishra Roll No: 2301320100077
Experiment 1:
Design and implement a lexical analyzer for given language using C and the lexical analyzer should ignore
redundant spaces, tabs and new lines
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char keywords[6][10] = {"int", "float", "if", "else", "while", "return"};
int isKeyword(char str[]) {
for (int i = 0; i < 6; i++) {
if (strcmp(keywords[i], str) == 0)
return 1;
}
return 0;
}
int main() {
char ch, buffer[20];
int i = 0;
FILE *fp;
fp = fopen("[Link]", "r");
if (fp == NULL) {
printf("Cannot open file\n");
return 0;
}
while ((ch = fgetc(fp)) != EOF) {
/* Ignore all whitespace (space, tab, newline) */
if (isspace(ch))
continue;
/* Identifier or Keyword */
if (isalpha(ch)) {
buffer[i++] = ch;
while (isalnum(ch = fgetc(fp))) {
buffer[i++] = ch;
}
buffer[i] = '\0';
i = 0;
fseek(fp, -1, SEEK_CUR);
if (isKeyword(buffer))
printf("Keyword: %s\n", buffer);
else
printf("Identifier: %s\n", buffer);
}
Faculty Name Sign with Date
ti
/* Number */
else if (isdigit(ch)) {
buffer[i++] = ch;
while (isdigit(ch = fgetc(fp))) {
buffer[i++] = ch;
}
buffer[i] = '\0';
i = 0;
fseek(fp, -1, SEEK_CUR);
printf("Number: %s\n", buffer);
}
/* Operators */
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '=') {
printf("Operator: %c\n", ch);
}
/* Delimiters */
else if (ch == ';' || ch == ',' || ch == '(' || ch == ')' ||
ch == '{' || ch == '}') {
printf("Delimiter: %c\n", ch);
}
/* Invalid characters */
else {
printf("Invalid character: %c\n", ch);
}
}
fclose(fp);
return 0;
}
[Link]
int a = 10;
oat b = 20;
if(a < b)
{
a = a + b;
}
Output:
Faculty signature Sign with date
fl
Experiment No: 2 Date:
Name: Keer Mishra Roll No: 2301320100077
Experiment 2
Objec ve: Implemen ng Lexical Analyzer using LEX Tool
Theory:
LEX is a tool used to generate lexical analyzers automa cally.
It uses regular expressions to de ne pa erns for tokens.
A LEX program has three sec ons:
1. De ni on Sec on
2. Rules Sec on
3. User Code Sec on
LEX generates a C le ([Link].c) which contains the func on yylex() that performs lexical analysis.
Whitespace like space, tab and newline is ignored because it does not a ect program meaning.
Overall working ow:
LEX Source File (.l)
↓
lex command
↓
[Link].c (C program generate)
↓
gcc compile
↓
Executable Scanner
↓
Input Program → Tokens Output
Faculty Name Sign with Date
fi
ti
ti
ti
ti
ti
ti
fl
fi
ti
ti
fi
tt
ti
ti
ff
LEX Program Code:
lexer.l
%{
#include <stdio.h>
%}
%%
"int"|" oat"|"if"|"else"|"while"|"return"
{ prin ("Keyword: %s\n", yytext); }
[0-9]+
{ prin ("Number: %s\n", yytext); }
[a-zA-Z][a-zA-Z0-9]*
{ prin ("Iden er: %s\n", yytext); }
[+\-*/=]
{ prin ("Operator: %s\n", yytext); }
[;(),{}]
{ prin ("Delimiter: %s\n", yytext); }
[ \t\n]+
{ /* Ignore whitespace */ }
.
{ prin ("Invalid character: %s\n", yytext); }
%%
int main()
{
prin ("Enter the input:\n");
yylex();
return 0;
}
Faculty Name Sign with Date
tf
fl
tf
tf
tf
tf
tf
tf
ti
fi
Procedure to Execute:
1. Save le as lexer.l
2. Open terminal
3. Run:
lex lexer.l
4. Compile:
gcc [Link].c -o lexer -ll
5. Execute:
./lexer
Sample Input:
int a = 10;
Output:
Keyword: int
Iden er: a
Operator: =
Number: 10
Delimiter: ;
Faculty Name Sign with Date
ti
fi
fi