URDINESH: TOKEN SEPARATION Program In C
Showing posts with label TOKEN SEPARATION Program In C. Show all posts
Showing posts with label TOKEN SEPARATION Program In C. Show all posts

Wednesday, May 7, 2014

TOKEN SEPARATION Program In C

TOKEN SEPARATION
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
FILE *fp;
char kw[25][10]={"include","int","void","float","char","if","else","while","switch"};
char optr[15]={'+','-','*','/','%','='};
char sym[20]={'{','}','[',']','.',',','"','<','>',':',';'};
char dt[5][8]={"int","float","char"};
char fname[10],c[10],x,y; int i;
void main()
{
            int ch,op;
            void display(); void operators();
            void constants(); void specialsymbols(); clrscr();
            printf("\nToken Separation:\n");
            printf("-----------------\n\n");
printf("Menus:\n\n1.Display Program\n2.Display Keywords\n3.Display Variables\n4.Display Operators\n5.Display Constants\n6.Display Special Symbols\n7.Quit\n\n");
            label:
            printf("Enter your choice: ");
            scanf("%d",&ch);
            switch(ch)
            {
              case 1:
                display(); break;
              case 2:
                printf("\nEnter the file name with extension : ");
                scanf("%s",fname);
                fp=fopen(fname,"r");
                if(fp==NULL)  printf("ERROR...");
                else
                {
                  do
                  {
                   fscanf(fp,"%s",&c);
                   for(i=0;i<9;i++)
                   {
                        if(strcmp(c,kw[i])==0)   printf("%s  ",c);
                   }
                  }while(!feof(fp));
                }
                fclose(fp);
                break;
              case 3:
                printf("\nEnter the file name with extension : ");
                scanf("%s",fname);
                fp=fopen(fname,"r");
                if(fp==NULL)  printf("ERROR...");
                else
                {
                  do
                  {
                        fscanf(fp,"%s",&c);
                        for(i=0;i<3;i++)
                        {
                          if(strcmp(c,dt[i])==0)
                          {
                            while((x=fgetc(fp))!=';')
                              printf("%c",x);
                            break;
                          }
                        }
                  }while(!feof(fp));
                }
                fclose(fp);
                break;
              case 4:
                operators(); break;
              case 5:
                constants(); break;
              case 6:
                specialsymbols(); break;
              case 7:
                exit(0); break;
              default:
                printf("Invalid Option...Try Again...\n"); break;
            }
            printf("\nDo you want to continue (1/0): ");
            scanf("%d",&op);
            if(op==1)
              goto label;
            else
              exit(0);
            getch();
}
void display()
{
            printf("\nEnter the file name with extension : ");
            scanf("%s",fname);
            fp=fopen(fname,"r");
            if(fp==NULL)  printf("ERROR...");
            else
            {
              fscanf(fp,"%c",&y);
              while(!feof(fp))
              {
                printf("%c",y); fscanf(fp,"%c",&y);
              }
            }
            fclose(fp);
}
void operators()
{
            printf("\nEnter the file name with extension : ");
            scanf("%s",fname);
            fp=fopen(fname,"r");
            if(fp==NULL)  printf("ERROR...");
            else
            {
              while(fread(&y,sizeof(char),1,fp))
              {
                for(i=0;i<6;i++)
                  if(y==optr[i])   printf("%c,",y);
              }
            }
            fclose(fp);
}
void constants()
{
            printf("\nEnter the file name with extension : ");
            scanf("%s",fname);
            fp=fopen(fname,"r");
            if(fp==NULL)  printf("ERROR...");
            else
            {
              while(fread(&y,sizeof(char),1,fp))
              {
                if(isdigit(y))  printf("%c,",y);
              }
            }
            fclose(fp);
}
void specialsymbols()
{
            printf("\nEnter the file name with extension : ");
            scanf("%s",fname);
            fp=fopen(fname,"r");
            if(fp==NULL)  printf("ERROR...");
            else
            {
              while(fread(&y,sizeof(char),1,fp))
              {
                for(i=0;i<11;i++)
                  if(y==sym[i])  printf("%c,",y);
              }
            }
            fclose(fp);
}



OUTPUT:


Token Separation:
-----------------------
Menus:
1.Display Program
2.Display Keywords
3.Display Variables
4.Display Operators
5.Display Constants
6.Display Special Symbols
7.Quit

Enter your choice: 1
Enter the file name with extension : add.c
//Addition
#include<stdio.h>
#include<conio.h>
void main()
{
        int c; float x,y; char s[10],a;
        clrscr();
        c=123+456;
        printf("Added value is...%d",c);
        getch();
}
Do you want to continue (1/0): 1

Enter your choice: 2
Enter the file name with extension : add.c
void  int  float  char
Do you want to continue (1/0): 1

Enter your choice: 3
Enter the file name with extension : add.c
 c x,y s[10],a
Do you want to continue (1/0): 1

Enter your choice: 4
Enter the file name with extension : add.c
/,/,=,+,%,
Do you want to continue (1/0): 1

Enter your choice: 5
Enter the file name with extension : add.c
1,0,1,2,3,4,5,6,
Do you want to continue (1/0): 1

Enter your choice: 6
Enter the file name with extension : add.c
<,.,>,<,.,>,{,;,,,;,[,],,,;,;,;,",.,.,.,",,,;,;,},
Do you want to continue (1/0): 1

Enter your choice: 7

Followers