You are on page 1of 7

Ex.

No: 14
IMPLEMENTATION OF LEXICAL ANALYSIS IN C
Date:

AIM:
To write a C Program to implement a Lexical analyzer.
ALGORITHM:
1) Start the program.
2) Declare all the variables and file pointers.
3) Display the input program.
4) Separate the keyword in the program and display it.
5) Display the header files of the input program.
6) Separate the operators of the input program and display it.
7) Print the punctuation marks.
8) Print the constant that are present in input program.
9) Print the identifiers of the input program.
10) Stop the program.













PROGRAM CODING:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
void main()
{
FILE *fp;
int i,j;
char arr[100],k;
char kw[10][10]={"int","float","double","end","main","void","include","printf","scanf"};
char hf[2][10]={"stdio.h","conio.h"};
char op[5]={'+','-','*','/','%'};
char punc[6]={'(',')','{','}',','};
clrscr();
fp=fopen("input.c","r");
printf("Input Program\n");
while(!feof(fp))
{
arr[0]=fgetc(fp);
printf("%c",arr[0]);
}
fclose(fp);
printf("\nSymbol table\n");
fp=fopen("input.c","r");
printf("\nKeywords");
while(!feof(fp))
{
arr[0]=fgetc(fp);

fscanf(fp,"%s",arr);
for(i=0;i<10;i++)
{
if(strcmp(arr,kw[i])==0)
{
printf("\t%s",arr);
}
}
}
fclose(fp);
fp=fopen("input.c","r");
printf("\nHeader files");
while(!feof(fp))
{
arr[0]=fgetc(fp);
fscanf(fp,"%s",arr);
for(i=0;i<2;i++)
{
if(strcmp(arr,hf[i])==0)
{
printf("\t%s",arr);
}
}}
fclose(fp);
fp=fopen("input.c","r");
printf("\nOperators");
while(!feof(fp))
{
arr[0]=fgetc(fp);
for(i=0;i<5;i++)
{
if(arr[0]==op[i])
{
printf("\t%c",arr[0]);
}
}
}
fclose(fp);
fp=fopen("input.c","r");
printf("\npunctuation");
while(!feof(fp))
{
arr[0]=fgetc(fp);
for(i=0;i<6;i++)
{
if(arr[0]==punc[i])
{
printf("\t%c",arr[0]);
}
}
}
fclose(fp);
fp=fopen("input.c","r");
printf("\nConstants");
while(!feof(fp))
{
arr[0]=fgetc(fp);
if(isdigit(arr[0]))
{
printf(" %c ",arr[0]);
}
}
fclose(fp);
fp=fopen("input.c","r");
printf("\nidentifier ");
while(!feof(fp))
{
fscanf(fp,"%s",arr);
for(i=0;i<2;i++)
{
if(strcmp(arr,kw[i])==0)
{
fscanf(fp,"%s",arr);
j=0;
while(j<strlen(arr) && arr[j]!=';')
{
printf("%c",arr[j]);
j++;
}}}}
fclose(fp);
getch();
}











INPUT: (INPUT.C)
#include<stdio.h>
#include<conio.h>
void main()
{
Int a,b,c;
a=10;
b=5;
c=a+b;
printf(The sum is %d,c);
getch();
}
OUTPUT:


RESULT:
Thus the above the program is executed and the required output is obtained.

Ex. No: 15
IMPLEMENTATION OF LEXICAL ANALYSIS IN C
Date:

You might also like