0% found this document useful (0 votes)
10 views4 pages

EX:No: 1A Implementation of Token Separation

Required

Uploaded by

naveenraj.kmns
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

EX:No: 1A Implementation of Token Separation

Required

Uploaded by

naveenraj.kmns
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EX:No: 1A IMPLEMENTATION OF TOKEN SEPARATION

AIM:
To write a C program to implement the token separation operation.

ALGORITHM:

Step 1: Start the program.


Step 2: Store the possible keywords in an array key[][] and their corresponding byte value in b[].
Step 3: Declare all the variables.
Step 4: Declare the file pointer fp for file operation.
Step 5: Open a file t.c in write mode.
Step 6: Enter valid data into sym.c file until “z” symbol encountered. Then close the file.
Step 7: Open t.c file in read mode. Read the character one by one.
Step 8: If not End of file using switch case check for special symbols.Print the special symbol.
Step 9: Check whether the string is alphabet or alphanumeric using isalpha() and isalnum()
functions.
Step 10: If the string is alphabet assign it to variable “a” and compare with keywords in array
using strcmp() function.
Step 11: If string is keyword print the keyword else print the string is identifier
Step 12: Check for the character is constant value using isdigit() function and print it is a number
Step 13: Stop the program.

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

Enter a valid data: void main()


{
int a=9;
}z
TOKEN Seperat ion

TokenNo TokenName Tokent ype

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.

You might also like