EX:No: 1A Implementation of Token Separation
EX:No: 1A Implementation of Token Separation
AIM:
To write a C program to implement the token separation operation.
ALGORITHM:
PROGRAM
#include<stdio.h>
#include<string.h>
char key[5][20]={"void","if","while","int","main"};
int main()
{
int i,n=1;
int label;
char a[20];
char str;
FILE *fp;
fp=fopen("t.c","w");
print f("\n Enter a valid data:");
while((str=getchar())!='z')
{
fputc(str,fp);
}
fclose(fp);
fp=fopen("t.c","r");
print f("\t\tTOKEN Seperat ion\n");
print f("\n TokenNo TokenName Tokentype\n");
while((str=fgetc(fp))!=EOF)
{
i=0;label=0;
switch(str)
{
case '{':
print f("\n\t%d\t%c\t a Special S ymbol",n++,str);
break;
case '}':
print f("\n\t%d\t%c\t a Special S ymbol",n++,str);
break;
case '(':
print f("\n\t%d\t%c\t a Special Symbol",n++,str);
break;
case ')':
print f("\n\t%d\t%c\t a Special Symbol",n++,str);
break;
case ';':
print f("\n\t%d\t%c\t a Special S ymbol",n++,str);
break;
case '*':
print f("\n\t%d\t%c\t a Mult iplicat ion Operator",n++,str);
break;
case '=':
print f("\n\t%d\t%c\t a assignment operator",n++,str);
break;
case '+':
print f("\n\t%d\t%c\t a addit ion operator",n++,str);
break;
case '#':
print f("\n\t%d\t%c\t a pre-processor",n++,str);
break;
default :
if( isalpha(str))
{
do
{
a[i]=str;
i++;
str=fgetc(fp);
}
while(isalpha(str)||isalnum(str));
a[i]='\0';
fseek(fp,-1,1);
for(i=0;i<5;i++)
{
if(strcmp(a,key[i])==0)
{
print f("\n\t%d\t%s\t a keyword",n++,a);
label=1;
break;
}
}
if(label==0)
{
print f("\n\t%d\t%s\t an ident ifier",n++,a);
}
}
else if(isdigit(str))
{
do
{
a[i]=str;
i++;
str=fgetc(fp);
}
while(isdigit(str));
a[i]='\0';
fseek(fp,-1,1);
print f("\n\t%d\t%s\t a constant",n++,a);
}
}
}
fclose(fp);
}
OUTPUT
[ucea-cse@telnet ~]$ cc token.c
[ucea-cse@telnet ~]$ ./a.out
1 void a keyword
2 main a keyword
3 ( a Special Symbol
4 ) a Special Symbol
5 { a Special Symbol
6 int a keyword
7 a an ident ifier
8 = a assignment operator
9 9 a constant
10 ; a Special Symbol
11 } a Special Symbol
RESULT:
Thus the C program to implement the token separation operation has been successfully
implemented and verified.