/**************************************************************************/ /* CSE2304 Prac3A Sample Source Code */ /**************************************************************************/ #include #include #include "global.h" #include "routines.h" /* this function initializes the necessary routines and variables */ void init( ) { int i; char * reserved_words_parameter_file="reserved_words"; char * operators_parameter_file="operators"; char * punctuations_parameter_file="punctuations"; flag_single_quote=OFF; flag_double_quote=OFF; flag_number=OFF; flag_letter=OFF; for(i=0;i<100;i++) buffer[i]='\0'; position=0; reserved_words=init_list_reserved_words(reserved_words_parameter_file); operators=init_list_operators(operators_parameter_file); punctuations=init_list_punctuations(punctuations_parameter_file); white_space=init_white_space(); counter_string=0; counter_char=0; counter_identifier=0; counter_number=0; counter_comment=0; } /* this function parses the read character and does necessary classification and updates the statistics * it's the central part of the program */ void process(int input) { int is_reserved_word, is_operators; int idx; int i; /***************** when the status of the flag_double_quote is ON *********/ if((flag_double_quote==ON)&&(input!='\"')) { return; } /***************** when the status of the flag_single_quote is ON *********/ if((flag_single_quote==ON)&&(input!='\'')) { return; } /***********************when the status of the flag_comment is ON *******/ if(flag_comment==ON) { if((input=='*')&&(position==0)) { buffer[0]='*'; position++; return; } if((input=='/')&&(buffer[0]=='*')&&(position==1)) { flag_comment=OFF; position=0; for(i=0;i<100;i++) buffer[i]='\0'; return; } else return; } /**********************when the status of the flag_number is ON ************/ if((flag_number==ON) && !(((input>=48)&&(input<=57))||(input==46)||(input=='e')||(input=='-')||(input=='+'))) /* please check ASCII table for the values I use here */ { flag_number=OFF; update_numbers(); } if((flag_number==ON) &&(input=='e')) { for(i=0;i=65)&&(input<=90))||((input>=97)&&(input<=122))||(input=='_')||((input>=48)&&(input<=57)))) /* what if input is not a lexical letter ? */ { flag_letter=OFF; is_reserved_word=search_reserved_words(reserved_words,buffer,&idx); if(is_reserved_word==ON) update_reserved_words(reserved_words,idx); else update_identifiers(); for(i=0;i<100;i++) buffer[i]='\0'; position=0; } if((flag_letter==ON)&&((input=='_')||((input>=48)&&(input<=57)))) { buffer[position++]=input; return; } /******************when the status of the flag_operator is ON **************/ if(flag_operator==ON)/* what if input is not a operator letter ? */ { if(!is_operator_char(input)) { flag_operator=OFF; is_operators=search_operators(operators,buffer,&idx); if(is_operators==ON) { update_operators(operators, idx); for(i=0;i<100;i++) buffer[i]='\0'; position=0; } else if ((buffer[0]='/')&&(input=='*')&&(position==1)) { flag_comment=ON; buffer[0]=0; position=0; return; } } else if(input=='*') { if(buffer[0]=='/') /* the previous input is '/' */ { flag_operator=OFF; flag_comment=ON; for(i=0;i<100;i++) buffer[i]='\0'; position=0; return; } } else { buffer[position++]=input; return; } } /* when the current symbol is white space symbol */ if((flag_single_quote==OFF)&&(flag_double_quote==OFF)&&(input==' ')) { position=0; for(i=0;i<100;i++) buffer[i]='\0'; // search_white_space(white_space,input,&idx); // update_white_space(white_space,idx); return; } /* if the current symbol is single quote mark */ else if(input=='\'') /* process the '\' */ { if(flag_single_quote==OFF) { flag_single_quote=ON; return; } else { update_characters(); flag_single_quote=OFF; return; } } /* if the current symbol is double quote mark */ else if(input=='\"') /* process the '\"' */ { if(flag_double_quote==OFF) { flag_double_quote=ON; return; } else { update_strings(); flag_double_quote=OFF; return; } } if(((input>=48)&&(input<=57))||(input==46)) /* see if input is a number */ { if(flag_number==OFF) { flag_number=ON; for(i=0;i<100;i++) buffer[i]='\0'; position=0; buffer[position++]=input; return; } else { buffer[position++]=input; return; } } if(((input>=65)&&(input<=90))||((input>=97)&&(input<=122))||(input=='_'))/* see if input is a lexical letter */ { if(flag_letter==OFF) { flag_letter=ON; buffer[position++]=input; return; } else { buffer[position++]=input; return; } } if((input==';')||(input==',')||(input=='(')||(input==')')||(input=='{')||(input=='}')||(input=='[')||(input==']'))/* please check ASCII table for the values I use here */ { if(flag_letter==ON) { flag_letter=OFF; is_reserved_word=search_reserved_words(reserved_words,buffer,&idx); if(is_reserved_word==ON) update_reserved_words(reserved_words,idx); else update_identifiers(); for(i=0;i<100;i++) buffer[i]='\0'; position=0; } else if(flag_number==ON) { flag_number=OFF; update_numbers(); for(i=0;i<100;i++) buffer[i]='\0'; position=0; } search_punctuations(punctuations,input,&idx); update_punctuations(punctuations,idx); for(i=0;i<100;i++) buffer[i]='\0'; position=0; return; } if(is_operator_char(input)) /* whether the current input is an operator symbol?*/ { if(flag_operator==OFF) { flag_operator=ON; buffer[position++]=input; return; } else { buffer[position++]=input; return; } } return; } /* this function calculates and output the final results */ void end( ) { calculate_and_print_results(); end_list_reserved_words(reserved_words); end_list_operators(operators); end_list_punctuations(punctuations); end_white_space(white_space); } int main(int argc, char *argv[]) { int input; FILE *fp; if(argc<2) { printf("The Usage of the program is:\n"); printf("prac3a [c program file]\n"); exit(0); } if((fp=fopen(argv[1],"r"))==NULL) { printf(" The program can't open the c source file, please check the directory and the file name\n"); exit(0); } init(); while((input=fgetc(fp))!=EOF) { process(input); } printf(" The source file is successfully parsed!\n"); end(); printf(" Game Over! Thanks for your Using!\n"); exit(0); }