You are on page 1of 7

REGNO:31509621016 NAME:J.

HARIKRISHNAN

EX.NO: DATE:

TOKEN SEPERATION

AIM:
To separate the tokens (5 types) of tokens in a given C program file.

ALGORITHM:
Step1: Read the character from the file. Step2: If the character is an alphabet, goto step 5. Step3: Check whether the character is an operator a. If yes, identify the character as an operator. b. Otherwise, identify it as special character. Step4: Go to step 8. Step5: Read character until we encounter a non-alphabetical character and form a string. Step6: If the string is define of const identify the variable next to it as a constant and go to step 8. Otherwise go to step 7. Step7: Check whether, the string is keyword. If yes, identify the string as keyword. Otherwise, identify the string as identifier. Step8: If end of file is reached stop program, otherwise goto step 1.

REGNO:31509621016 NAME:J.HARIKRISHNAN

PROGRAM:
#include<stdio.h> int i; FILE *fp; void keywords(); void operators(); void scharacter(); void constant(); void identifier(); char keyword[10][15]={"int","float","char","short","struct","static","switch", "case","const"}; char operator[10]={'+','-','*','/','=','!'}; char specialchar[10]={'#',';','{','}','(',')'}; char ident[10][15]={"int","float","char"}; main() { int ch; printf("\n\t TOKEN SEPARATION"); printf("\n\t ****************"); do { printf("\n\t 1)KEYWORDS"); printf("\n\t 2)OPERATORS"); printf("\n\t 3)SPECIAL CHARACTER"); printf("\n\t 4)CONSTANT"); printf("\n\t 5)IDENTIFIER"); printf("\n\t 6)EXIT"); printf("\n\t Enter your choice:"); scanf("%d",&ch); switch(ch) { case 1: keywords(); break;

REGNO:31509621016 NAME:J.HARIKRISHNAN
case 2: operators(); break; case 3: scharacter(); break; case 4: constant(); break; case 5: identifier(); break; case 6: printf("\n\t End of the program"); break; } }while(ch!=6); } void keywords() { char c[10]; int flag[9]={0}; fp=fopen("add.c","r"); fscanf(fp,"%s",&c); printf("\n\t Keywords are:"); while(!feof(fp)) { for(i=0;i<9;i++) if(strcmp(c,keyword[i])==0&&flag[i]==0) { flag[i]=1; printf("%s\t",c); } fscanf(fp,"%s",&c); } fclose(fp); }

REGNO:31509621016 NAME:J.HARIKRISHNAN
void operators() { char c; int flag[6]={0}; fp=fopen("add.c","r"); printf("\n\t Operators are:"); while((c=getc(fp))!=EOF) { for(i=0;i<6;i++) if(c==operator[i]&&flag[i]==0) { flag[i]=1; printf("%c\t",c); } } fclose(fp); } void scharacter() { char c; int flag[6]={0}; fp=fopen("add.c","r"); printf("\n\t Special Characters are:"); while((c=getc(fp))!=EOF) { for(i=0;i<6;i++) if(c==specialchar[i]&&flag[i]==0) { flag[i]=1; printf("%c\t",c); } } fclose(fp); } void constant() { char c[10]; fp=fopen("add.c","r"); printf("\n\t Constants are:");

REGNO:31509621016 NAME:J.HARIKRISHNAN
while(!feof(fp)) { fscanf(fp,"%s",&c); if(strcmp(c,"#define")==0) { fscanf(fp,"%s",&c); printf("%s\t",c); } if(strcmp(c,"const")==0) { fscanf(fp,"%s",&c); fscanf(fp,"%s",&c); printf("%s\t",c); } } fclose(fp); } void identifier() { char c[10],ch[15]="bb"; fp=fopen("add.c","r"); printf("\n\t Identifiers are:"); while(!feof(fp)) { fscanf(fp,"%s",&c); for(i=0;i<3;i++) if(strcmp(c,ident[i])==0) if(strcmp(ch,"const")!=0) { fscanf(fp,"%s",&c); while(strcmp(c,";")!=0) { if(strcmp(c,",")!=0) printf("%s\t",c); fscanf(fp,"%s",&c); } } strcpy(ch,c); }

REGNO:31509621016 NAME:J.HARIKRISHNAN
}

SAMPLE INPUT:
#include<stdio.h> main() { int a , b , c ; const int d ; printf("\n\t Enter the value of a and b:"); scanf("%d",&a,&b); c=a+b; printf("%d",&c); }

OUTPUT:
TOKEN SEPARATION ********************* 1)KEYWORDS 2)OPERATORS 3)SPECIAL CHARACTER 4)CONSTANT 5)IDENTIFIER 6)EXIT Enter your choice: 1 Keywords are: int Enter your choice: 2 Operators are:= Enter your choice: 3 + const

REGNO:31509621016 NAME:J.HARIKRISHNAN
Special Characters are:# Enter your choice: 4 Constants are:d Enter your choice: 5 Identifiers are: a b c ( ) { ; }

Enter your choice: 6 End of the program

RESULT:
Thus the five types of tokens in the given program is separated successfully.

You might also like