static int compile(const char* source) { extern FILE* yyin; extern int yyparse(); char *fout = NULL; size_t fsz; yyin = fopen(source, "r"); if (!yyin) fatal("Could not open %s\n", source); /* Call the parser; currently a bison-generated parser */ yyparse(); if (stdoutFlag) prnt_code(); fsz = strlen(source) + 2; if (!(fout = calloc(1, fsz))) fatal("%s: could not allocate memory\n", getprogname()); strlcpy(fout, source, fsz); strlcat(fout, "b", fsz); save_code(fout); if (yyin) fclose(yyin); if (fout) free(fout); return 0; }
int main (int argc, char *argv[]) { char buffer[10000]; token tokens[500]; int t; //total number of tokens int i; //general use counter int res; FILE *fp; if (argc < 2) { //check if the number of arguments is correct printf("Usage: %s <input_file> -o <output_file>\n", argv[0]); return 1; } else if ((fp = fopen(argv[1], "r")) == NULL) { //try to open the input file printf("%s\n", strerror(errno)); return 2; } else { /*Phase 0: Put instuctions from the input file to the buffer serialized*/ res = serialize_input(buffer, fp); /*If is on DEBUG_MODE print debuggin info*/ if (DEBUG_MODE) { printf("Phase 0: Put instuctions from the input file to the buffer serialized:\n%s\n\n", buffer); } close(fp); if (res == 0) { puts("Empty input file."); return 3; } } /*Phase 1: Parse the code*/ validate_tokens(buffer); /*If is on DEBUG_MODE print debuggin info*/ if (DEBUG_MODE) { printf("Phase 1: Remove lines with invalid tokens:\n%s\n\n", buffer); } /*Phase 2: Extract the tokens*/ t = extract_tokens(buffer, tokens); /*If is on DEBUG_MODE print debuggin info*/ if (DEBUG_MODE) { puts("Phase 2: Extract the tokens:"); print_tokens(tokens, t); putchar('\n'); } /*Phase 3: Do syntax analysis on the tokens*/ t = analize_tokens(tokens, t); /*If is on DEBUG_MODE print debuggin info*/ if (DEBUG_MODE) { puts("Phase 3: Do syntax analysis on the tokens:"); print_tokens(tokens, t); putchar('\n'); } /*Phase 4: Do optimization on the tokens*/ t = optimize_tokens(tokens, t); /*If is on DEBUG_MODE print debuggin info*/ if (DEBUG_MODE) { puts("Phase 4: Do optimization on the tokens:"); print_tokens(tokens, t); putchar('\n'); } /*Phase 5: Do code generation based on the tokens*/ generate_code(buffer, tokens, t); /*If is on DEBUG_MODE print debuggin info*/ if (DEBUG_MODE) { printf("Phase 5: Do code generation based on the tokens:\n%s\n\n", buffer); } /*Print errors buffer*/ if (error_cnt != 0) { puts(error_buffer); } else { puts("No Errors"); } /*Final Phase: Save the code in a file*/ if (argc == 4 && strcmp(argv[2], "-o") == 0) { //check if user provided output file name save_code(buffer, argv[3]); } else { save_code(buffer, "out.c"); } return 0; }