scanner.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
#include<stdio.h> #include<conio.h> #include<ctype.h> #include<string.h> #include<iostream.h> #include<stdlib.h> int isoperator(char ch) { if(ch=='+') return 1; elseif(ch=='-') return 2; elseif(ch=='*') return 3; elseif(ch=='/') return 4; elseif(ch=='=') return 5; elsereturn 0; } /*int issymbol(int symcounter,char symbol[][10],char currstr[]) { for(int i=0;i<symcounter;i++) { if(strcmp(symbol[i],currstr)==0) return i; } return -1; }*/ void main() { char instr[50]; char currstr[50]; char symbol[10][10]; int scantab[6][5]={ 1, 2, 3,-1,-1, -1,-1,-1,-1,-1, -1, 2, 2, 2,-1, -1,-1, 3,-1, 4, -1,-1, 5,-1,-1, -1,-1, 5,-1,-1 }; int counter=0,j; int currstate=0,symcounter=0; char currsym; int i=0; int count=0; clrscr(); printf("ENTER THE STRING : "); scanf("%[^\n]",&instr); while(instr[counter]!='\0') { count=0; while(instr[counter++]==' ') {} counter--; while(instr[counter]!=' ' && instr[counter]!='\0') currstr[count++]=instr[counter++]; // printf("%d",count); currstr[count]='\0'; // printf("%s",currstr); currstate=0; int i=0; while(i<strlen(currstr)) { if(isoperator(currstr[i])>0) j=0; elseif(isalpha(currstr[i])) j=1; elseif(isdigit(currstr[i])) j=2; elseif(currstr[i]=='_') j=3; elseif(currstr[i]=='.') j=4; else { printf("ERROR IN INPUT ! PLEASE TRY AGAIN WITH SUITABLE STRING :"); getch(); exit(0); } currstate=scantab[currstate][j]; i++; } if(currstate==1) //printf(" <op,%d>",isoperator(currstr[--i])); printf(" <op> "); elseif(currstate==2) { /* int temp; temp=issymbol(symcounter,symbol,currstr); if(temp>=0) printf(" <id#%d>",temp+1); else { strcpy(symbol[symcounter],currstr); symcounter++; printf(" <id#%d>",symcounter); }*/ printf(" <id> "); } elseif(currstate==3) printf(" <int>"); elseif(currstate==5) printf(" <real>"); elseif(currstate==-1) printf(" error .."); } getch(); } |