You are on page 1of 10

1))))

#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char words[50][25],str[25],ch;
char key[50][25],op[50][10],id[50][25];
int i,j,k=0,w=0,p=0,d=0;
int yes=0;
fp=fopen("ttoken.c","r");
clrscr();
printf("\n File: ttoken.c\n\n");
if(fp!=NULL)
{
do
{
j=0;
ch='d';
strcpy(str,"");
while(ch!=' ')
{
ch=getc(fp);
printf("%c",ch);
if(ch==EOF) break;
str[j++]=ch;
switch(ch)
{
case
case
case
case
case
case

'+':strcpy(op[p++],"+");break;
'-':strcpy(op[p++],"-");break;
'*':strcpy(op[p++],"*");break;
'/':strcpy(op[p++],"/");break;
'%':strcpy(op[p++],"%");break;
'=':strcpy(op[p++],"=");break;

}
if(ch==' '||ch=='\n'||ch=='=')
{
j--;
break;
}
if(ch==';'||ch=='('||ch==')'||ch==',')
{
j--;
break;
}
}
str[j]='\0';

strcpy(words[w++],str);

if(ch=='(')
strcpy(words[w++],"(");
if(ch==';')
strcpy(words[w++],";");
if(ch==')')
strcpy(words[w++],")");
if(ch=='=')
strcpy(words[w++],"=");
if(ch==',')
strcpy(words[w++],",");
j=0;
}while(ch!=EOF);
}
for(i=0;i<w;i++)
{//1
if((!strcmp(words[i],"for"))||(!strcmp(words[i],"if")))
{
strcpy(key[k++],words[i]);
}
if((!strcmp(words[i],"void"))||(!strcmp(words[i],"else")))
{
strcpy(key[k++],words[i]);
}
if((!strcmp(words[i],"char"))||(!strcmp(words[i],"int")))
{
strcpy(key[k++],words[i]);
yes=1;
for(j=i+1;strcmp(words[j],";");j++)
{
if(!strcmp(words[j],"="))
{
yes=0;
j++;
}
if(!strcmp(words[j],","))
{
yes=1;
j++;
}
if(yes)
strcpy(id[d++],words[j]);
}
}
if((!strcmp(words[i],"float"))||(!strcmp(words[i],"double")))
{
strcpy(key[k++],words[i]);
for(j=i+1;strcmp(words[j],";");j++)
{
if(!strcmp(words[j],"="))
{
yes=0;
j++;
}
if(!strcmp(words[j],","))

{
yes=1;
j++;
}
if(yes)
strcpy(id[d++],words[j]);
}
}
}//1
printf("\n\n* Identifiers:\n");
for(i=0;i<d;i++)
{
printf("%s ",id[i]);
}
printf("\n\n* Keywords:\n");
for(i=0;i<k;i++)
{
printf("%s ",key[i]);
}
printf("\n\n* Operators:\n");
for(i=0;i<p;i++)
{
printf("%s ",op[i]);
}
fclose(fp);
getch();
}

2))))
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char exp[50]="\0",con[50]="\0",kwd[50]="\0",id[50]="\0",sym[50]="\0",opr[50]="\0
";
char key[6][10]={"if","for","do","while","int","float"};
char ch;
char ptr[10][10]={"\0"};
int i=0,j=0,k=-1,n=-1,p=-1,s=-1;
clrscr();
puts("Enter the expression for lexical analysis");
gets(exp);
puts("\nThe tokens are");
do
{
ch=exp[i];
if(isalpha(ch))
{
k=-1;
ptr[++n][++k]=ch;
i++;

ch=exp[i];
if(isalpha(ch)||isdigit(ch))
{
while(isalpha(ch)||isdigit(ch))
{
ptr[n][++k]=ch;
i++;
ch=exp[i];
}
while(j<6)
{
if(strcmp(key[j],ptr[n])==0)
{
ptr[n][++k]=' ';
strcat(kwd,ptr[n]);
break;
}
if(j==5)
{
ptr[n][++k]=' ';
strcat(id,ptr[n]);
}
j++;
}
}
else
{
ptr[n][++k]=' ';
strcat(id,ptr[n]);
}
i--;
ch=exp[i];
j=0;
}
else if(isdigit(ch))
{
k=-1;
ptr[++n][++k]=ch;
i++;
ch=exp[i];
if(isdigit(ch))
{
while(isdigit(ch))
{
ptr[n][++k]=ch;
i++;
ch=exp[i];
}
}
i--;
ch=exp[i];
ptr[n][++k]=' ';
strcat(con,ptr[n]);
}
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||ch=='>'||ch=='<'||ch=='='||
ch=='!')
{
opr[++p]=ch;
i++;
ch=exp[i];

if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||ch=='>'||ch=='<'||ch=='='||ch=='
!')
{
opr[++p]=ch;
}
else
{
i--;
ch=exp[i];
opr[++p]=' ';
}
}
else
{
sym[++s]=ch;
sym[++s]=' ';
}
i++;
}while(exp[i]!='\0');
puts("\nKeyword:");
puts(kwd);
puts("\nIdentifier:");
puts(id);
puts("\nConstant:");
puts(con);
puts("\nOperator:");
puts(opr);
puts("\nSymbol:");
puts(sym);
getch();
}
3))))
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
struct str
{
char a[30];
}s;
void main()
{
FILE *fp;
int j,f=0,flag,gd,gm;
char p[15][15]={"stdio.h","include","main","math.h","string.h",
"graphics.h","void","int","float","char"};
char q[15][15]={"(",")","[","]","{","}",":",".","#",";",",","?"};
char r[15][15]={"+","-","*","/","<",">","%","="};
gd=DETECT;
initgraph(&gd,&gm,"");
gotoxy(28,2);
printf("Token Seperation");
rectangle(3,35,530,475);
rectangle(3,35,100,475);
rectangle(200,35,300,475);
rectangle(300,35,400,475);
rectangle(3,35,530,75);

gotoxy(3,4);
printf("KEYWORDS");
gotoxy(16,4);
printf("PUNCT.SYMS");
gotoxy(28,4);
printf("OPERATORS");
gotoxy(40,4);
printf("CONSTANTS");
gotoxy(53,4);
printf("IDENTIFIERS");
fp=fopen("add.c","r");
f=7;
do
{
fscanf(fp,"%s",s.a);
for(j=0;j<=15;j++)
{
if(strcmp(s.a,p[j])==0)
flag=0;
}
for(j=0;j<=15;j++)
{
if(strcmp(s.a,q[j])==0)
flag=1;
}
for(j=0;j<=15;j++)
{
if(strcmp(s.a,r[j])==0)
flag=2;
}
if(atoi(s.a)>0||atoi(s.a)<0)
flag=3;
printf("\n");
switch(flag)
{
case 0:
gotoxy(5,f);
printf("%s",s.a);
break;
case 1:
gotoxy(17,f);
printf("%s",s.a);
break;
case 2:
gotoxy(29,f);
printf("%s",s.a);
break;
case 3:
gotoxy(41,f);
printf("%s",s.a);
break;
default:
gotoxy(53,f);
printf("%s",s.a);
}
f++;
flag=-1;
}while(!feof(fp));
getch();
closegraph();

}
4)))
#include<stdio.h>
#include<string.h>
#include<fcntl.h> /*file operations*/
#include<ctype.h>/* isdigit() and isalpha() */

/*to check whether a string is already existing in a string array*/


int notexist(char target[20][20],char find[20],int x)
{
int i;
for(i=0;i<x;i++)
{
if(strcmp(target[i],find)==0)
return 0;
}
return 1;
}

int main()
{
int fd,size,i,flag,j=0,k=0,l=0,c=1,d=1;
char *tok,*opr,*spl;
char file[10];
char buffer[200]={""};
printf("\n****************************TOKEN SEPARATION**************************
**********");
/*token possibilities*/
char operator[]={"+=*/%-"};
char splchar[]={";,{(})"};
char tokens[20][20]={"int","float","char","const"};
char nos[20][20]={""};
char var[20][20]={""};
char con[20][20]={""};
/*token counts*/
int oprcount[]={0,0,0,0,0,0};
int splcount[]={0,0,0,0,0,0};
int tokcount[]={0,0,0,0};

/*file operation*/
printf("\nENTER THE FILE CONTAINING THE PROGRAM ");
scanf("%s",file);
fd=open(file,O_RDONLY);
if(fd<0)
{
printf("\nERROR OCCURRED WHILE OPENING THE FILE");
}
size=read(fd,buffer,200);
printf("\n**********************************************************************

**********");
close(fd);
/*checking for special characters*/
spl=strpbrk (buffer,splchar);
while (spl!= NULL)
{
for(i=0;i<=5;i++)
{
if(*spl==splchar[i])
splcount[i]++;
}
spl=strpbrk(spl+1,splchar);
}

/*checking for operators*/


opr=strpbrk (buffer,operator);
while (opr!= NULL)
{
for(i=0;i<=5;i++)
{
if(*opr==operator[i])
oprcount[i]++;
}
opr=strpbrk(opr+1,operator);
}

/*checking for keywords, numbers and identifiers*/


tok=strtok(buffer," ,+=;()}{\n");
while(tok!=NULL)
{
flag=0;
/*to flag it if it is a keyword or #define*/
if((strcmp(tok,"#define")==0)||(strcmp(tok,"const")==0))
{
if(strcmp(tok,"const")==0)
{
tokcount[3]++;
}
c--;
goto jump;
}
if(c==0)
/*constants check*/
{
for(i=0;i<=4;i++)
{
if(strcmp(tok,tokens[i])==0) /*keyword check*/
{
tokcount[i]++;
goto jump;
}
}
strcpy(con[l],tok);
l++;

/*const check*/

c++;
flag++;
}

for(i=0;i<=4;i++)
{
if(strcmp(tok,tokens[i])==0)
{
tokcount[i]++;
flag++;
break;
}
}

/*keyword check*/

if(flag==0)
{
if(isdigit(tok[0])&& notexist(nos,tok,j))
/* numbers check*/
{
strcpy(nos[j],tok);
j++;
}
else if(isalpha(tok[0])&& notexist(var,tok,k) && notexist(con,tok,l))
tifer check*/
{
strcpy(var[k],tok);
k++;
}
}
jump:
tok=strtok(NULL," ,+=;()}{\n");
}
/*displaying the special characters*/
printf("\nSPECIAL CHARACTERS:");
for(i=0;i<=5;i++)
{
if(splcount[i]>0)
printf(" %c",splchar[i]);
}

/*displaying the operators*/


printf("\nOPERATORS:");
for(i=0;i<=5;i++)
{
if(oprcount[i]>0)
printf(" %c",operator[i]);
}
/*displaying the numbers*/
printf("\nNUMBERS: ");
for(i=0;i<=j;i++)
{
printf("%s ",nos[i]);

/* iden

}
/*displaying the identifiers*/
printf("\nIDENTIFIERS: ");
for(i=0;i<=k;i++)
{
printf("%s ",var[i]);
}
/*displaying the constants*/
printf("\nCONSTANTS: ");
for(i=0;i<=l;i++)
{
printf("%s ",con[i]);
}
/*displaying the keywords*/
printf("\nKEYWORDS: ");
for(i=0;i<=4;i++)
{
if(tokcount[i]>0)
printf("%s ",tokens[i]);
}
printf("\n**********************************************************************
**********");
printf("\n");
return 0;
}

You might also like