Title Program to Identify Tokens In A Given Line viz. Identifier, Literal, Alphabet, Special Symbol(_,-) etc. Author: Ranjeet Singh Visitor Submitted Source Code Author Email: indiancosmonaut@yahoo.com Description: This program is used to identify the tokens in a given string. Hits: 2392 Since 25th November, 2003 Code: Select and Copy the Code #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> #define COLOR1 {textcolor(6); textbackground(1);} #define COLOR2 {textcolor(5); textbackground(15);} #define COLOR3 textcolor(6); #define COLOR4 textcolor(9); void main() { char book[100]; char grid[10][30]; int k = 0; int row = 0; int col = 0; int i = 0; int clock = 0; int j = 0; int shiva = 0; clrscr(); cout<<"Enter a string : "; gets(book); k = strlen(book); book[k] = 32; book[k+1] = '#'; for(i=0;book[i]!='#';i++) { while(book[i]!=32) { grid[row][col] = book[i]; col++; i++; } grid[row][col++] = '\0'; col = 0; row++; } cout<<endl; cout<<"The 2-D Matrix is ...\n\n"; for(i=0;i<row;i++) { COLOR3; cprintf("|"); for(j=0;grid[i][j]!='\0';j++) { cout<< grid[i][j] ; cprintf("|"); } COLOR4; cprintf("null char"); COLOR3; cprintf("|"); cout<<endl; } cout<<"\nThe classifications are ...\n\n"; for(i=0;i<row;i++) { clock = strlen(grid[i]); if(clock == 1) { j = 0; if((grid[i][j] == '_') || (grid[i][j] == '-')) { COLOR1; cprintf(grid[i]); COLOR2; cprintf(" : < Special Symbol >"); cout<<endl; } if((grid[i][j] >= 48) && (grid[i][j] <= 57)) { COLOR1; cprintf(grid[i]); COLOR2; cprintf(" : < A Digit >"); cout<<endl; } if((grid[i][j] >=65 ) && (grid[i][j] <=90 ) || (grid[i][j] >=97 ) && (grid[i][j] <=122 )) { COLOR1; cprintf(grid[i]); COLOR2; cprintf(" : < An Alphabet >"); cout<<endl; } } if((clock <= 20 ) && (clock != 1)) { j = 0; if((grid[i][j] >= 65 ) && (grid[i][j] <= 90 ) || (grid[i][j] >= 97 ) && (grid[i][j] <= 122 )) { for(j=1;j<clock;j++) { if((grid[i][j] >= 65 ) && (grid[i][j] <= 90 ) || (grid[i][j] >= 97 ) && (grid[i][j] <= 122 ) || (grid[i][j] == '_') || (grid[i][j] >= 48 ) && (grid[i][j] <= 57 )) { shiva++; } if(shiva+1 == clock) { COLOR1; cprintf(grid[i]); COLOR2; cprintf(" : < An Identifier >"); cout<<endl; shiva = 0; } } } } if((clock <= 15) && (clock != 1)) { for(j=0;j<clock;j++) { if((grid[i][j] >= 48) && (grid[i][j] <= 57)) { shiva++; } if(shiva == clock) { COLOR1; cprintf(grid[i]); COLOR2; cprintf(" : < A Literal >"); cout<<endl; shiva = 0; } } } } getch(); }