implemented -st and -tok functionality, updated -help
This commit is contained in:
1
Makefile
1
Makefile
@ -44,3 +44,4 @@ clean:
|
|||||||
rm -f $(EXE)
|
rm -f $(EXE)
|
||||||
rm -f flex.h
|
rm -f flex.h
|
||||||
rm -f *.tok
|
rm -f *.tok
|
||||||
|
rm -f *.st
|
||||||
|
143
runner.c
143
runner.c
@ -2,34 +2,72 @@
|
|||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
char *check_input;
|
if (argc == 1) {
|
||||||
int token;
|
fprintf(stderr, "INVALID INPUT: Include a .alpha file or use -help for more inputs \n");
|
||||||
FILE *output;
|
return -1;
|
||||||
|
|
||||||
if (argc == 1 || argc > 3) {
|
|
||||||
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) {
|
} else if (argc == 2) {
|
||||||
|
//can be either -help or .alpha file
|
||||||
if (is_help(argv[1])) {
|
if (is_help(argv[1])) {
|
||||||
return 0;
|
return 0;
|
||||||
} else if (is_alpha_file(argv[1], strlen(argv[1])) != 0) {
|
} else if (is_alpha_file(argv[1], strlen(argv[1])) == 0) {
|
||||||
|
//run flex for now
|
||||||
|
no_flag = SET_FLAG; //no argument but valid input
|
||||||
|
alpha_file = fopen(argv[1], "r");
|
||||||
|
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "INVALID INPUT: Include a .alpha file or use -help for more inputs\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
arg = NO_ARG; //no argument but valid input
|
|
||||||
yyin = fopen(argv[1], "r");
|
|
||||||
} else {
|
} else {
|
||||||
check_input = is_tok(argc, argv);
|
//last input must be .alpha
|
||||||
if (strcmp(CHECK_OTHER, check_input) == 0 || strcmp(INVALID_ARG, check_input) == 0) {
|
if (is_alpha_file(argv[argc - 1], strlen(argv[argc - 1])) != 0) {
|
||||||
fprintf(stderr, "check_other or invalid_arg \n");
|
fprintf(stderr, "INVALID INPUT: Include a .alpha file at end of input or use -help for more inputs \n");
|
||||||
return -1; //invalid argument (need to update as we add more valid arguments)
|
return -1;
|
||||||
|
} else {
|
||||||
|
//now check that other args are valid (flags will not be null if flag is present)
|
||||||
|
for (int i = 1; i < argc - 1; i++) {
|
||||||
|
if (check_flag(argv[i], argv[argc - 1]) != 0) {
|
||||||
|
fprintf(stderr, "INVALID FLAG(S): Use -help to view valid inputs \n");
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
|
alpha_file = fopen(argv[argc - 1], "r");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return run(alpha_file);
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_flag(char *arg, char* alpha) {
|
||||||
|
if (strcmp("-tok", arg) == 0) {
|
||||||
|
if (tok_flag == NULL) {
|
||||||
|
return new_file(arg, alpha);
|
||||||
|
}
|
||||||
|
fprintf(stderr, "FLAGS REPEAT\n");
|
||||||
|
return -1;
|
||||||
|
} else if (strcmp("-st", arg) == 0) {
|
||||||
|
if (st_flag == NULL) {
|
||||||
|
return new_file(arg, alpha);
|
||||||
|
}
|
||||||
|
fprintf(stderr, "FLAGS REPEAT\n");
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "INVALID FLAG: Use -help for valid inputs\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int run(FILE *alpha) {
|
||||||
|
int token;
|
||||||
|
//check that file exists
|
||||||
|
if (alpha == NULL) {
|
||||||
|
fprintf(stderr, "INPUT FILE NOT FOUND\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
yyin = alpha;
|
||||||
while (0 != (token = yylex())) {
|
while (0 != (token = yylex())) {
|
||||||
if (arg == TOK_ARG) {
|
if (tok_flag != NULL) {
|
||||||
fprintf(output, "%d %d %3d \"%s\"\n", line_number, column_number, token, yytext);
|
fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number, token, yytext);
|
||||||
}
|
}
|
||||||
if(token == COMMENT){
|
if(token == COMMENT){
|
||||||
for (int i = 0; i < yyleng; i++) {
|
for (int i = 0; i < yyleng; i++) {
|
||||||
@ -44,54 +82,71 @@ int main(int argc, char *argv[]) {
|
|||||||
column_number += yyleng;
|
column_number += yyleng;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (st_flag != NULL) {
|
||||||
|
//output symbol table, file pointer is
|
||||||
|
fprintf(st_flag, "just checking that this works \n");
|
||||||
|
}
|
||||||
|
|
||||||
if (yyin != NULL) {
|
if (yyin != NULL) {
|
||||||
fclose(yyin);
|
fclose(yyin);
|
||||||
}
|
}
|
||||||
if (output != NULL && arg == TOK_ARG) {
|
|
||||||
fclose(output);
|
if (tok_flag != NULL) {
|
||||||
|
fclose(tok_flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool is_help(char * input) {
|
bool is_help(char * input) {
|
||||||
if (strcmp(input, "-help") == 0) {
|
if (strcmp(input, "-help") == 0) {
|
||||||
printf("HELP:\nHow to run the alpha compiler:\n./alpha [options] program\nValid options:\n-tok output the token number, token, line number, and column number for each of the tokens to the .tok file\n");
|
printf("%s", HELP);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *is_tok(int argc, char *argv[]) {
|
int new_file(char *arg, char *alpha) {
|
||||||
if (argc == 3 && strcmp("-tok", argv[1]) == 0) {
|
int type_len;
|
||||||
char *input_prog = argv[2];
|
const char *basename = alpha;
|
||||||
int file_len = strlen(input_prog);
|
const char *slash = strchr(alpha, '/');
|
||||||
//check that input program is a .alpha file
|
while (slash != NULL) {
|
||||||
if (is_alpha_file(input_prog, file_len) != 0) {
|
|
||||||
return INVALID_ARG;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *basename = input_prog;
|
|
||||||
const char *slash = strrchr(input_prog, '/');
|
|
||||||
if (slash != NULL) {
|
|
||||||
basename = slash + 1;
|
basename = slash + 1;
|
||||||
|
slash = strchr(basename, '/');
|
||||||
|
}
|
||||||
|
if (strcmp(arg, "-tok") == 0) {
|
||||||
|
type_len = TOK_LEN;
|
||||||
|
} else if (strcmp(arg, "-st") == 0) {
|
||||||
|
type_len = ST_LEN;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "INVALID FLAG: Use -help to view valid inputs\n");
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate lengths
|
|
||||||
|
// calculate lengths
|
||||||
int basename_len = strlen(basename);
|
int basename_len = strlen(basename);
|
||||||
char* FILE_tok = calloc(basename_len - ALPHA_OFFSET + TOK_LEN + 2, sizeof(char));
|
char *file_name = calloc(basename_len - ALPHA_OFFSET + type_len + 2, sizeof(char));
|
||||||
|
|
||||||
// Copy filename and add .tok extension
|
//coy filename and add extension
|
||||||
strncpy(FILE_tok, basename, basename_len - ALPHA_OFFSET);
|
strncpy(file_name, basename, basename_len - ALPHA_OFFSET);
|
||||||
//fprintf(stderr, "hello");
|
strcat(file_name, ".");
|
||||||
strcat(FILE_tok, ".tok");
|
strcat(file_name, arg + 1);
|
||||||
|
|
||||||
return FILE_tok;
|
|
||||||
|
if (strcmp(arg, "-tok") == 0) {
|
||||||
|
tok_flag = fopen(file_name, "w");
|
||||||
|
} else if (strcmp(arg, "-st") == 0) {
|
||||||
|
st_flag = fopen(file_name, "w");
|
||||||
}
|
}
|
||||||
return CHECK_OTHER;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int is_alpha_file(char *file, int file_len) {
|
|
||||||
if (strcmp(".alpha", file + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) {
|
int is_alpha_file(char *alpha, int file_len) {
|
||||||
|
if (strcmp(".alpha", alpha + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) {
|
||||||
return -1; //not alpha file
|
return -1; //not alpha file
|
||||||
}
|
}
|
||||||
return 0; //is alpha file
|
return 0; //is alpha file
|
||||||
|
23
runner.h
23
runner.h
@ -1,14 +1,9 @@
|
|||||||
#define ALPHA_OFFSET 6
|
#define ALPHA_OFFSET 6
|
||||||
#define TOK_LEN 3
|
#define TOK_LEN 3
|
||||||
|
#define ST_LEN 2
|
||||||
//returns for is_tok
|
#define HELP "HELP:\nHow to run the alpha compiler:\n./alpha [options] program\nValid options:\n-tok output the token number, token, line number, and column number for each of the tokens to the .tok file\n-st output the symbol table for the program to the .st file\n"
|
||||||
#define INVALID_ARG "invalid"
|
//use to set flags for arg types
|
||||||
#define CHECK_OTHER "diff"
|
#define SET_FLAG 1
|
||||||
|
|
||||||
//argument type in main
|
|
||||||
#define NO_ARG 0
|
|
||||||
#define DIFF_ARG 1
|
|
||||||
#define TOK_ARG 2
|
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -21,8 +16,14 @@ extern int line_number, column_number;
|
|||||||
extern char *yytext;
|
extern char *yytext;
|
||||||
extern FILE *yyin;
|
extern FILE *yyin;
|
||||||
int arg;
|
int arg;
|
||||||
|
FILE *alpha_file;
|
||||||
|
FILE *tok_flag = NULL;
|
||||||
|
FILE *st_flag = NULL;
|
||||||
|
int no_flag = 0;
|
||||||
|
|
||||||
int main(int argc, char* argv[]);
|
int main(int argc, char* argv[]);
|
||||||
char *is_tok(int argc, char* argv[]);
|
int new_file(char *arg, char *alpha);
|
||||||
int is_alpha_file(char *file, int file_len);
|
int is_alpha_file(char *alpha, int file_len);
|
||||||
bool is_help(char * input);
|
bool is_help(char * input);
|
||||||
|
int run(FILE*alpha);
|
||||||
|
int check_flag(char * arg, char* alpha);
|
||||||
|
Reference in New Issue
Block a user