Added option to generate header file for yy.lex.c, fixed errors in runner.c, and updated Makefile
This commit is contained in:
55
runner.c
55
runner.c
@ -1,38 +1,59 @@
|
||||
#include "runner.h"
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char *check_input;
|
||||
int token;
|
||||
//check_input can be compared to INVALID_ARG and DIFF_ARG to determine if -tok and holds the generated file name if it is
|
||||
check_input = is_tok(argc, argv);
|
||||
FILE * output = fopen(check_input, "w");
|
||||
FILE *output;
|
||||
|
||||
if (check_input == INVALID_ARG) {
|
||||
return -1;
|
||||
if (argc == 1 || argc > 3 || (argc == 2 && is_alpha_file(argv[1], strlen(argv[1])) != 0)) {
|
||||
fprintf(stderr, "invalid input with 1, >3, or non .alpha arg \n");
|
||||
return -1; //no alpha file or too many args
|
||||
} else if (argc == 2) {
|
||||
arg = NO_ARG; //no argument but valid input
|
||||
yyin = fopen(argv[1], "r");
|
||||
} else {
|
||||
check_input = is_tok(argc, argv);
|
||||
if (strcmp(CHECK_OTHER, check_input) == 0 || strcmp(INVALID_ARG, check_input) == 0) {
|
||||
fprintf(stderr, "check_other or invalid_arg \n");
|
||||
return -1; //invalid argument (need to update as we add more valid arguments)
|
||||
}
|
||||
output = fopen(check_input, "w");
|
||||
arg = TOK_ARG; //it is a -tok arg with a valid alpha file at argv[2]
|
||||
yyin = fopen(argv[2], "r");
|
||||
}
|
||||
|
||||
while (0 != (token = yylex())) {
|
||||
if (check_input != DIFF_ARG) {
|
||||
token = yylex();
|
||||
fprintf(stderr, "made it here");
|
||||
while (token != 0) {
|
||||
if (arg == TOK_ARG) {
|
||||
fprintf(output, "%d %d %3d \"%s\"\n", line_number, column_number, token, yytext);
|
||||
}
|
||||
}
|
||||
token = yylex();
|
||||
fprintf(stderr, "made it past yylex");
|
||||
}
|
||||
|
||||
fprintf(stderr, "returned 0\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *is_tok(int argc, char *argv[]) {
|
||||
if (argc == 3 && strcmp("-tok", argv[1])) {
|
||||
if (argc == 3 && strcmp("-tok", argv[1]) == 0) {
|
||||
char *input_prog = argv[2];
|
||||
int file_len = strlen(input);
|
||||
int file_len = strlen(input_prog);
|
||||
//check that input program is a .alpha file
|
||||
if (strcmp(".alpha", input_prog[file_len - ALPHA_OFFSET]) != 0) {
|
||||
if (is_alpha_file(input_prog, file_len) != 0) {
|
||||
return INVALID_ARG;
|
||||
}
|
||||
char *FILE_tok[file_len - ALPHA_OFFSET + TOK_LEN];
|
||||
strncpy(input, FILE_tok, file_len - ALPHA_OFFSET); //copy name of prog before .alpha
|
||||
strcpy(".tok", FILE_tok[file_len - ALPHA_OFFSET]); //add .tok to end of file name
|
||||
char* FILE_tok = calloc(sizeof(char), file_len - ALPHA_OFFSET + TOK_LEN);
|
||||
strncpy(FILE_tok, input_prog, file_len - ALPHA_OFFSET); //copy name of prog before .alpha
|
||||
strcpy(FILE_tok + sizeof(char) * (file_len - ALPHA_OFFSET), ".tok"); //add .tok to end of file name
|
||||
return FILE_tok;
|
||||
}
|
||||
return DIFF_ARG;
|
||||
return CHECK_OTHER;
|
||||
}
|
||||
|
||||
int is_alpha_file(char *file, int file_len) {
|
||||
if (strcmp(".alpha", file + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) {
|
||||
return -1; //not alpha file
|
||||
}
|
||||
return 0; //is alpha file
|
||||
}
|
||||
|
Reference in New Issue
Block a user