Practical : 1
Subject : System Programming
Source Code :
Subject : System Programming
Aim : Write C++ program to implement the lexical analyzer.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
char keyword[32][10]={"void","int","char","float","signed","unsigned","long","double","enum","auto","register","return","struct","break","case","const","continue","default","do","else","extern","for","goto","if","short","sizeof","static","switch","typedef","union","volatile","while"};
char op[7]={"+->*/!="};
void main()
{
int
i,j=0,flag=0;
char ch;
char
str[20];
FILE *fp;
fp=fopen("file38.txt","r");
if(fp==NULL)
{
printf("there
is no content in file");
exit(0);
while((ch=fgetc(fp))!=EOF)
{
flag=0;
for(i=0;i<7;i++)
{
if(op[i]==ch)
{
flag=1;
}
}
if(flag==1)
{
printf("%c
is operator \n",ch);
}
else
if(ch==';')
{
printf("statement
is over\n");
break;
}
else
if((ch==' ' || ch=='\n') && j!=0)
{
str[j]='\0';
j=0;
for(i=0;i<32;i++)
{
if(strcmp(keyword[i],str)==0)
{
flag=1;
}
}
if(flag==1)
{
printf("%s
is keyword\n",str);
}
else
{
printf("%s
is identifier\n",str);
}
while(str[j]!=NULL)
{
str[j]='\0';
}
}
else
if(isdigit(ch))
{
printf("%c
is a constant\n",ch);
}
else
{
str[j]=ch;
j++;
}
}
fclose(fp);
}
Output :
0 comments:
Post a Comment