int main(int argc, char **argv){ Buffer *ptr_Buffer; /* pointer to Buffer structure */ FILE *fi; /* input file handle */ int loadsize = 0; /*the size of the file loaded in the buffer */ int ansi_c = !ANSI_C; /* ANSI C compliancy flag */ /* Check if the compiler option is set to compile ANSI C */ /* __DATE__, __TIME__, __LINE__, __FILE__, __STDC__ are predefined preprocessor macros*/ if (ansi_c){ err_printf("Date: %s Time: %s", __DATE__, __TIME__); err_printf("ERROR: Compiler is not ANSI C compliant!\n"); exit(1); } /* missing file name or/and mode parameter */ if (argc <= 2){ err_printf("\nDate: %s Time: %s", __DATE__, __TIME__); err_printf("\nRuntime error at line %d in file %s\n", __LINE__, __FILE__); err_printf("%s\b\b\b\b%s%s", argv[0], ": ", "Missing parameters."); err_printf("Usage: platybt source_file_name mode"); exit(1); } /* create a source code input buffer */ switch (*argv[2]){ /*gets the first caracter of the mode argument*/ case 'f': case 'a': case 'm': break; default: err_printf("%s%s%s", argv[0], ": ", "Wrong mode parameter."); exit(1); } /*create the input buffer */ ptr_Buffer = b_create(INIT_CAPACITY, INC_FACTOR, *argv[2]); if (ptr_Buffer == NULL){ err_printf("%s%s%s", argv[0], ": ", "Could not create buffer."); exit(1); } /* open the source file */ if ((fi = fopen(argv[1], "r")) == NULL){ err_printf("%s%s%s%s", argv[0], ": ", "Cannot open file: ", argv[1]); exit(1); } /* load a source file into the input buffer */ printf("Reading file %s ....Please wait\n", argv[1]); loadsize = b_load(fi, ptr_Buffer); if (loadsize == R_FAIL_1) err_printf("%s%s%s", argv[0], ": ", "Error in loading buffer."); /* close the source file */ fclose(fi); /*find the size of the file */ if (loadsize == LOAD_FAIL){ printf("The input file %s %s\n", argv[1], "is not completely loaded."); printf("Input file size: %ld\n", get_filesize(argv[1])); } /* set a mark at the last char in the buffer*/ b_setmark(ptr_Buffer, b_size(ptr_Buffer)); /* display the contents of the input buffer */ display(ptr_Buffer); /* pack the buffer * if possible, add end-of-file character (EOF) to the buffer * display again */ if (b_pack(ptr_Buffer)){ if (!b_addc(ptr_Buffer, EOF)) err_printf("%s%s%s", argv[0], ": ", "Error in writing to buffer."); display(ptr_Buffer); } /* destroy the buffer */ b_destroy(ptr_Buffer); /* make the buffer invalid It is not necessary here because the function terminates anyway, but will prevent run-time errors and crashes in future expansions */ ptr_Buffer = NULL; /*return success */ return (0); }
/* main function takes a PLATYPUS source file as * an argument at the command line. * usage: parser source_file_name [-stz size][-sts:A | -sts:D] */ int main(int argc, char ** argv){ FILE *fi; /* input file handle */ int loadsize = 0; /*the size of the file loaded in the buffer */ int st_def_size = ST_DEF_SIZE; /* Sumbol Table default size */ char sort_st = 0; /*Symbol Table sort switch */ int ansi_c = !ANSI_C; /* ANSI C flag */ /* Check if the compiler option is set to compile ANSI C */ /* __DATE__, __TIME__, __LINE__, __FILE__, __STDC__ are predefined preprocessor macros*/ if(ansi_c){ err_printf("Date: %s Time: %s",__DATE__, __TIME__); err_printf("ERROR: Compiler is not ANSI C compliant!\n"); exit(1); } /*check for correct arrguments - source file name */ if (argc <= 1){ /* __DATE__, __TIME__, __LINE__, __FILE__ are predefined preprocessor macros*/ err_printf("Date: %s Time: %s",__DATE__, __TIME__); err_printf("Runtime error at line %d in file %s", __LINE__, __FILE__); err_printf("%s%s%s",argv[0],": ","Missing source file name."); err_printf("%s%s%s","Usage: ", "parser", " source_file_name [-stz size][-sts:A | -sts:D]"); exit(EXIT_FAILURE); } /* check for optional switches - symbol table size and/or sort */ if (argc == 3){ if (strcmp(argv[2],"-sts:A") && strcmp(argv[2],"-sts:D") ){ err_printf("%s%s%s",argv[0],": ","Invalid switch."); err_printf("%s%s\b\b\b\b%s","Usage: ", argv[0], " source file name [-stz size][-sts:A | -sts:D]"); exit(EXIT_FAILURE); } if(strcmp(argv[2],"-sts:A")) sort_st = 'D'; else sort_st = 'A'; } /* symbol table size specified */ if (argc == 4){ if (strcmp(argv[2],"-stz")){ err_printf("%s%s%s",argv[0],": ","Invalid switch."); err_printf("%s%s\b\b\b\b%s","Usage: ", argv[0], " source file name [-stz size][-sts:A | -sts:D]"); exit(EXIT_FAILURE); } /* convert the symbol table size */ st_def_size = atoi(argv[3]); if (st_def_size <= 0){ err_printf("%s%s%s",argv[0],": ","Invalid switch."); err_printf("%s%s\b\b\b\b%s","Usage: ", argv[0], " source file name [-stz size][-sts:A | -sts:D]"); exit(EXIT_FAILURE); } } if (argc == 5){ if (strcmp(argv[2],"-stz")){ err_printf("%s%s%s",argv[0],": ","Invalid switch."); err_printf("%s%s\b\b\b\b%s","Usage: ", argv[0], " source file name [-stz size][-sts:A | -sts:D]"); exit(EXIT_FAILURE); } /* convert the symbol table size */ st_def_size = atoi(argv[3]); if (st_def_size <= 0){ err_printf("%s%s%s",argv[0],": ","Invalid switch."); err_printf("%s%s\b\b\b\b%s","Usage: ", argv[0], " source file name [-stz size][-sts:A | -sts:D]"); exit(EXIT_FAILURE); } if (strcmp(argv[4],"-sts:A")&& strcmp(argv[4],"-sts:D") ){ err_printf("%s%s%s",argv[0],": ","Invalid switch."); err_printf("%s%s\b\b\b\b%s","Usage: ", argv[0], " source file name [-stz size][-sts:A | -sts:D]"); exit(EXIT_FAILURE); } if(strcmp(argv[4],"-sts:A")) sort_st = 'D'; else sort_st = 'A'; } /* create a source code input buffer - multiplicative mode */ sc_buf = b_create(INIT_CAPACITY,INC_FACTOR,'m'); if (sc_buf == NULL){ err_printf("%s%s%s",argv[0],": ","Could not create source buffer"); exit(EXIT_FAILURE); } /* create symbol table */ sym_table = st_create(st_def_size); if (!sym_table.st_size){ err_printf("%s%s%s",argv[0],": ","Could not create symbol table"); exit (EXIT_FAILURE); } /*open source file */ if ((fi = fopen(argv[1],"r")) == NULL){ err_printf("%s%s%s%s",argv[0],": ", "Cannot open file: ",argv[1]); exit (1); } /* load source file into input buffer */ printf("Reading file %s ....Please wait\n",argv[1]); loadsize = b_load (fi,sc_buf); if(loadsize == R_FAIL_1) err_printf("%s%s%s",argv[0],": ","Error in loading buffer."); /* close source file */ fclose(fi); /*find the size of the file */ if (loadsize == LOAD_FAIL){ printf("The input file %s %s\n", argv[1],"is not completely loaded."); printf("Input file size: %ld\n", get_filesize(argv[1])); } /* pack and display the source buffer */ if(b_pack(sc_buf)){ display(sc_buf); } /* create string Literal Table */ str_LTBL = b_create(INIT_CAPACITY,INC_FACTOR,'a'); if (str_LTBL == NULL){ err_printf("%s%s%s",argv[0],": ","Could not create string buffer"); exit(EXIT_FAILURE); } /*registrer exit function */ atexit(garbage_collect); /*Testbed for buffer, scanner,symbol table and parser*/ /* Initialize scanner */ scanner_init(sc_buf); /* Add SEOF to input buffer */ b_addc(sc_buf, EOF); /* Start parsing */ printf("\nParsing the source file...\n\n"); parser(sc_buf); /* print Symbol Table */ /* if(sym_table.st_size && sort_st){ st_print(sym_table); if(sort_st){ printf("\nSorting symbol table...\n"); st_sort(sym_table,sort_st); st_print(sym_table); } } */ return (EXIT_SUCCESS); /* same effect as exit(0) */ }/*end of main */
/* main function takes a PLATYPUS source file as * an argument at the command line. * usage: scanner source_file_name" */ int main(int argc, char ** argv){ Buffer *sc_buf; /* pointer to input (source) buffer */ FILE *fi; /* input file handle */ Token t; /* token produced by the scanner */ int loadsize = 0; /*the size of the file loaded in the buffer */ int ansi_c = !ANSI_C; /* ANSI C compliancy flag */ /* Check if the compiler option is set to compile ANSI C */ /* __DATE__, __TIME__, __LINE__, __FILE__, __STDC__ are predefined preprocessor macros*/ if(ansi_c){ err_printf("Date: %s Time: %s",__DATE__, __TIME__); err_printf("ERROR: Compiler is not ANSI C compliant!\n"); exit(1); } /*check for correct arrguments - source file name */ if (argc <= 1){ /* __DATE__, __TIME__, __LINE__, __FILE__ are predefined preprocessor macros*/ err_printf("Date: %s Time: %s",__DATE__, __TIME__); err_printf("Runtime error at line %d in file %s", __LINE__, __FILE__); err_printf("%s%s%s",argv[0],": ","Missing source file name."); err_printf("%s%s%s","Usage: ", "scanner", " source_file_name"); exit(1); } /* create a source code input buffer - multiplicative mode */ sc_buf = b_create(INIT_CAPACITY,INC_FACTOR,'m'); if (sc_buf == NULL){ err_printf("%s%s%s",argv[0],": ","Could not create source buffer"); exit(1); } /*open source file */ if ((fi = fopen(argv[1],"r")) == NULL){ err_printf("%s%s%s%s",argv[0],": ", "Cannot open file: ",argv[1]); exit (1); } /* load source file into input buffer */ printf("Reading file %s ....Please wait\n",argv[1]); loadsize = b_load (fi,sc_buf); if(loadsize == R_FAIL_1) err_printf("%s%s%s",argv[0],": ","Error in loading buffer."); /* close source file */ fclose(fi); /*find the size of the file */ if (loadsize == LOAD_FAIL){ printf("The input file %s %s\n", argv[1],"is not completely loaded."); printf("Input file size: %ld\n", get_filesize(argv[1])); } /* pack and display the source buffer */ if(b_pack(sc_buf)){ display(sc_buf); } /* create string Literal Table */ str_LTBL = b_create(INIT_CAPACITY,INC_FACTOR,'a'); if (str_LTBL == NULL){ err_printf("%s%s%s",argv[0],": ","Could not create string literals buffer"); exit(1); } /*Testbed for the scanner */ /* add SEOF to input program buffer*/ b_addc(sc_buf,'\0'); /* Initialize scanner input buffer */ if(scanner_init(sc_buf)){; err_printf("%s%s%s",argv[0],": ","Empty program buffer - scanning canceled"); exit(1); } printf("\nScanning source file...\n\n"); printf("Token\t\tAttribute\n"); printf("----------------------------------\n"); do{ t= mlwpar_next_token(sc_buf); print_token(t); }while(t.code != SEOF_T); if(b_size(str_LTBL)) b_print(str_LTBL); b_destroy(sc_buf); b_destroy(str_LTBL); sc_buf = str_LTBL = NULL; return (0); }