0% found this document useful (0 votes)
21 views2 pages

Practical - 1: Aim:-Program To Implement Lexical Analyzer. Code

project of compiler design 6Th sem
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)
21 views2 pages

Practical - 1: Aim:-Program To Implement Lexical Analyzer. Code

project of compiler design 6Th sem
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/ 2

Faculty of Engineering & Technology

Subject -Name: compiler design Laboratory


Subject -C ode:303105350
B.Tech – 3r d Year – 6t h Sem

Practical - 1

Aim :- Program to implement Lexical Analyzer.

Code :-
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void lexicalAnalyzer(char *code) {
char *keywords[] = {"if", "else", "while", "for", "int", "float","return", "char", "void"};
int n_keywords = sizeof(keywords) / sizeof(keywords[0]);
char token[100];
int i = 0, j = 0;
while (code[i] != '\0')
{
if (isspace(code[i]))
{
i++;
continue;
}
if (strchr("+-*/=><", code[i]))
{ printf("Operator: %c\n", code[i]);
i++;
continue;
}
if (strchr("();{}[],", code[i]))
{ printf("Delimiter: %c\n", code[i]);
i++;
continue;
}
if (isalnum(code[i])) {
j = 0;
while (isalnum(code[i])) token[j++] = code[i++];
token[j] = '\0';
int isKeyword = 0;
for (int k = 0; k < n_keywords; k++) {
if (strcmp(token, keywords[k]) == 0)
{ printf("Keyword: %s\n", token);
isKeyword = 1;
break;
}
}
if (!isKeyword) {
if (isdigit(token[0]))
printf("Number: %s\n", token);
else
printf("Identifier: %s\n", token);

Enrollment no :- 2203031050375 Pg no. 1


Faculty of Engineering & Technology
Subject -Name: compiler design Laboratory
Subject -C ode:303105350
B.Tech – 3r d Year – 6t h Sem

}
}
}
}
int main() {
char code[256];
printf(“Razique\n”);
printf(“2203031050375”);
printf("\nEnter code: ");
fgets(code, sizeof(code), stdin); printf("\
nLexical Analysis Result:\n");
lexicalAnalyzer(code);
return 0;
}

Output:-

Enrollment no :- 2203031050375 Pg no. 2

You might also like