/* $ pooya laryan $ $ compiler - scanner design $ */ #include #include #include #include #include #include int removeblank(char [][15],int); int chk(char [][15],int); int scanner(char [],char []); int isnumber(char []); void main(void) { char line[255],tokens[100][15],*pl,key,tokentype[25]; int i=0,j=0; fstream file; file.open("c:\\input.orb",ios::in|ios::out); if(!file) exit(0); clrscr(); while(!file.eof()) { file.getline(line,255); pl=line; j=0; for(int ln=0;ln<=strlen(line);ln++) { if(*pl!=0 && *pl!=32) { // if *pl is not Blank if(*pl=='*' || *pl=='/' || *pl=='+' || *pl=='-' || *pl==')' || *pl=='(' || *pl==':' || *pl=='.' || *pl==',' || *pl==';' || *pl=='>' || *pl=='<' || *pl=='=' ) { char temp=*pl; tokens[i][j]='\0'; i++; tokens[i][0]=temp; tokens[i][1]='\0'; i++; j=0; } else { tokens[i][j]=*pl; j++; } } pl++; }// end of line tokens[i][j]='\0'; i++; } // end of file set eof file.close(); i=removeblank(tokens,i); i=chk(tokens,i); j=0; while(j<=i-1) { scanner(tokens[j],tokentype); cout<>>>>"; } } } //gotoxy(40,5); cout<') if(table[i+1][0]=='=') { table[i][1]=table[i+1][0]; table[i][2]='\0'; i++; for(int j=i;j<=size;j++) strcpy(table[j],table[j+1]); size--; } return size; } //------------------------------------------------------ int scanner(char word[],char tokentype[]) { if(strcmp(word,"+")==0 || strcmp(word,"*")==0 || strcmp(word,"-")==0 || strcmp(word,"/")==0 || strcmp(word,"mod")==0 || strcmp(word,"div")==0 || strcmp(word,">")==0 || strcmp(word,">=")==0 || strcmp(word,"<")==0 || strcmp(word,"<=")==0 || strcmp(word,"=")==0 || strcmp(word,":=")==0 || strcmp(word,",")==0 || strcmp(word,";")==0 || strcmp(word,".")==0 || strcmp(word,")")==0 || strcmp(word,"(")==0 || strcmp(word,":")==0) strcpy(tokentype,"Operator"); else if(strcmp(word,"integer")==0 || strcmp(word,"real")==0 ) strcpy(tokentype,"Variable type"); else if(strcmp(word,"if")==0 || strcmp(word,"else")==0 || strcmp(word,"then")==0 || strcmp(word,"elseif")==0 || strcmp(word,"do")==0 || strcmp(word,"while")==0 || strcmp(word,"end")==0 || strcmp(word,"array")==0 || strcmp(word,"procedure")==0 || strcmp(word,"function")==0 || strcmp(word,"const")==0 || strcmp(word,"var")==0 || strcmp(word,"begin")==0 || strcmp(word,"type")==0 || strcmp(word,"write")==0 || strcmp(word,"writeln")==0 || strcmp(word,"read")==0 || strcmp(word,"readln")==0) strcpy(tokentype,"Special Word"); else if(isnumber(word)==0) strcpy(tokentype,"Identifier"); else strcpy(tokentype,"Integer Number"); } //------------------------------------------------ int isnumber(char word[]) { for(int i=0;i<=strlen(word)-1;i++) if(word[i]>='a' && word[i]<='z') return 0; return 1; }