Fixed formatting in lex; Updated Makefile t#30
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,5 +6,4 @@ grammar.tab.c
|
||||
grammar.tab.h
|
||||
.vscode
|
||||
out
|
||||
src
|
||||
tmp
|
25
Makefile
25
Makefile
@ -5,8 +5,7 @@ EXE := alpha
|
||||
CFLAGS :=
|
||||
YACC := bison
|
||||
|
||||
compiler: tmp/grammar.tab.c tmp/lex.yy.c tmp/runner.o tmp/symbol_table.o
|
||||
$(CC) $(CFLAGS) -o $(EXE) tmp/runner.o tmp/lex.yy.c tmp/symbol_table.o
|
||||
compiler: runner
|
||||
|
||||
tmp/grammar.tab.c: src/grammar.y
|
||||
mkdir -p tmp
|
||||
@ -14,7 +13,7 @@ tmp/grammar.tab.c: src/grammar.y
|
||||
mv grammar.tab.c tmp/
|
||||
mv grammar.tab.h tmp/
|
||||
|
||||
tmp/lex.yy.c: src/lexicalStructure.lex
|
||||
tmp/lex.yy.c: src/lexicalStructure.lex tmp/grammar.tab.c
|
||||
$(FLEX) -o tmp/lex.yy.c $(LEX)
|
||||
mv flex.h tmp/
|
||||
|
||||
@ -25,21 +24,17 @@ tmp/symbol_table.o: src/symbol_table.c src/symbol_table.h
|
||||
$(CC) $(CFLAGS) -o tmp/symbol_table.o -c src/symbol_table.c
|
||||
|
||||
parser : tmp/lex.yy.c tmp/grammar.tab.c
|
||||
$(CC) -o parser tmp/lex.yy.c tmp/grammar.tab.c src/symbol_table.c
|
||||
$(CC) -o parser tmp/lex.yy.c tmp/grammar.tab.c
|
||||
|
||||
runner: tmp/lex.yy.c tmp/runner.o tmp/symbol_table.o
|
||||
$(CC) $(CFLAGS) -o $(EXE) tmp/runner.o tmp/lex.yy.c tmp/symbol_table.o
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
debug: CFLAGS += -DDEBUG=1
|
||||
debug: clean compiler
|
||||
|
||||
test:
|
||||
test: test-s1 test-s2
|
||||
|
||||
test-s1:
|
||||
./$(EXE) -tok ./tests/sprint1/test_comment_fix1.alpha
|
||||
./$(EXE) -tok ./tests/sprint1/test_comment_fix2.alpha
|
||||
./$(EXE) -tok ./tests/sprint1/test_comment_issues.alpha
|
||||
@ -55,6 +50,11 @@ test:
|
||||
./$(EXE) -tok ./tests/sprint1/test_simple_literals.alpha
|
||||
./$(EXE) -tok ./tests/sprint1/test_variables.alpha
|
||||
|
||||
test-s2:
|
||||
./$(EXE) -tok ./tests/sprint2/alpha/test_library.alpha
|
||||
./$(EXE) -tok ./tests/sprint2/alpha/test_one_line.alpha
|
||||
./$(EXE) -tok ./tests/sprint2/alpha/test_simple.alpha
|
||||
|
||||
clean:
|
||||
rm -f *.o
|
||||
rm -f lex.yy.c
|
||||
@ -65,4 +65,5 @@ clean:
|
||||
rm -f grammar.tab.h
|
||||
rm -f *.st
|
||||
rm -rf out
|
||||
rm -rf tmp
|
||||
rm -rf tmp
|
||||
rm -f parser
|
@ -4,7 +4,7 @@
|
||||
// Prologue
|
||||
%{
|
||||
#include <stdio.h>
|
||||
#include "symbol_table.c"
|
||||
#include "../src/symbol_table.c"
|
||||
extern int yylex(void);
|
||||
void yyerror(const char *err);
|
||||
extern char* yytext;
|
||||
|
@ -1,91 +1,84 @@
|
||||
/* Lexical Analysis with Flex (1.6.0) We used some of the code from this manual */
|
||||
/* so we placed the citation here. */
|
||||
/* definitions */
|
||||
/* Lexical Analyzer with Flex (1.6.0) */
|
||||
|
||||
%option noyywrap
|
||||
%option header-file="flex.h"
|
||||
%{
|
||||
#include <stdbool.h>
|
||||
//#include "typedefs.h"
|
||||
#include "grammar.tab.h"
|
||||
int line_number = 1, column_number = 1;
|
||||
#ifndef DEBUG
|
||||
#define DEBUG 0
|
||||
#endif
|
||||
|
||||
|
||||
#include <stdbool.h>
|
||||
//#include "typedefs.h"
|
||||
#include "grammar.tab.h"
|
||||
int line_number = 1, column_number = 1;
|
||||
#ifndef DEBUG
|
||||
#define DEBUG 0
|
||||
#endif
|
||||
%}
|
||||
|
||||
STARCOM [^\*]|\*+[^\)\*]+
|
||||
PARENCOM [^\)]|[^\*\)]+\)+
|
||||
COMMENT \(\*{STARCOM}*\*\)|\(\*{PARENCOM}*\*\)
|
||||
ID [A-Za-z_][0-9A-Za-z_]*
|
||||
DIGIT [0-9]
|
||||
CHAR \\n|\\t|\\'|[^'\n\t\\]|\\\\
|
||||
/* char can be a newline, tab, an escaped quote, or anything but a single quote, an actual line break, an actual tab, or a backslash by itself (to prevent confusion from escaped quote */
|
||||
SCHAR \\n|\\t|\\\"|[^\"\n\\]
|
||||
/* similar to above, a string Char (SCHAR) is the same as a CHAR except we cannot have double quotes instead of single quotes. Double quotes need to be escaped in Flex unlike single quotes based on documentation */
|
||||
|
||||
%%
|
||||
|
||||
"integer" {if(DEBUG) {printf( "T_INTEGER: %s (%d)\n", yytext, T_INTEGER);} else {return T_INTEGER;}}
|
||||
"address" {if(DEBUG) {printf( "T_ADDRESS: %s (%d)\n", yytext, T_ADDRESS);} else {return T_ADDRESS;}}
|
||||
"Boolean" {if(DEBUG) {printf( "T_BOOLEAN: %s (%d)\n", yytext, T_BOOLEAN);} else {return T_BOOLEAN;}}
|
||||
"character" {if(DEBUG) {printf( "T_CHARACTER: %s (%d)\n", yytext, T_CHARACTER);} else {return T_CHARACTER;}}
|
||||
"integer" {if(DEBUG) {printf( "T_INTEGER: %s (%d)\n", yytext, T_INTEGER);} else {return T_INTEGER;}}
|
||||
"address" {if(DEBUG) {printf( "T_ADDRESS: %s (%d)\n", yytext, T_ADDRESS);} else {return T_ADDRESS;}}
|
||||
"Boolean" {if(DEBUG) {printf( "T_BOOLEAN: %s (%d)\n", yytext, T_BOOLEAN);} else {return T_BOOLEAN;}}
|
||||
"character" {if(DEBUG) {printf( "T_CHARACTER: %s (%d)\n", yytext, T_CHARACTER);} else {return T_CHARACTER;}}
|
||||
|
||||
"while" {if(DEBUG) {printf( "WHILE: %s (%d)\n", yytext, WHILE);} else {return WHILE;}}
|
||||
"if" {if(DEBUG) {printf( "IF: %s (%d)\n", yytext, IF);} else {return IF;}}
|
||||
"then" {if(DEBUG) {printf( "THEN: %s (%d)\n", yytext, THEN);} else {return THEN;}}
|
||||
"else" {if(DEBUG) {printf( "ELSE: %s (%d)\n", yytext, ELSE);} else {return ELSE;}}
|
||||
"type" {if(DEBUG) {printf( "TYPE: %s (%d)\n", yytext, TYPE);} else {return TYPE;}}
|
||||
"function" {if(DEBUG) {printf( "FUNCTION: %s (%d)\n", yytext, FUNCTION);} else {return FUNCTION;}}
|
||||
"return" {if(DEBUG) {printf( "RETURN: %s (%d)\n", yytext, RETURN);} else {return RETURN;}}
|
||||
"external" {if(DEBUG) {printf( "EXTERNAL: %s (%d)\n", yytext, EXTERNAL);} else {return EXTERNAL;}}
|
||||
"as" {if(DEBUG) {printf( "AS: %s (%d)\n", yytext, AS);} else {return AS;}}
|
||||
"while" {if(DEBUG) {printf( "WHILE: %s (%d)\n", yytext, WHILE);} else {return WHILE;}}
|
||||
"if" {if(DEBUG) {printf( "IF: %s (%d)\n", yytext, IF);} else {return IF;}}
|
||||
"then" {if(DEBUG) {printf( "THEN: %s (%d)\n", yytext, THEN);} else {return THEN;}}
|
||||
"else" {if(DEBUG) {printf( "ELSE: %s (%d)\n", yytext, ELSE);} else {return ELSE;}}
|
||||
"type" {if(DEBUG) {printf( "TYPE: %s (%d)\n", yytext, TYPE);} else {return TYPE;}}
|
||||
"function" {if(DEBUG) {printf( "FUNCTION: %s (%d)\n", yytext, FUNCTION);} else {return FUNCTION;}}
|
||||
"return" {if(DEBUG) {printf( "RETURN: %s (%d)\n", yytext, RETURN);} else {return RETURN;}}
|
||||
"external" {if(DEBUG) {printf( "EXTERNAL: %s (%d)\n", yytext, EXTERNAL);} else {return EXTERNAL;}}
|
||||
"as" {if(DEBUG) {printf( "AS: %s (%d)\n", yytext, AS);} else {return AS;}}
|
||||
|
||||
"release" {if(DEBUG) {printf( "RELEASE: %s (%d)\n", yytext, RELEASE);} else {return RELEASE;}}
|
||||
"reserve" {if(DEBUG) {printf( "RESERVE: %s (%d)\n", yytext, RESERVE);} else {return RESERVE;}}
|
||||
"release" {if(DEBUG) {printf( "RELEASE: %s (%d)\n", yytext, RELEASE);} else {return RELEASE;}}
|
||||
"reserve" {if(DEBUG) {printf( "RESERVE: %s (%d)\n", yytext, RESERVE);} else {return RESERVE;}}
|
||||
|
||||
"+" {if(DEBUG) {printf( "ADD: %s (%d)\n", yytext, ADD);} else {return ADD;}}
|
||||
"-" {if(DEBUG) {printf( "SUB_OR_NEG: %s (%d)\n", yytext, SUB_OR_NEG);} else {return SUB_OR_NEG;}}
|
||||
"*" {if(DEBUG) {printf( "MUL: %s (%d)\n", yytext, MUL);} else {return MUL;}}
|
||||
"/" {if(DEBUG) {printf( "DIV: %s (%d)\n", yytext, DIV);} else {return DIV;}}
|
||||
"%" {if(DEBUG) {printf( "REM: %s (%d)\n", yytext, REM);} else {return REM;}}
|
||||
"<" {if(DEBUG) {printf( "LESS_THAN: %s (%d)\n", yytext, LESS_THAN);} else {return LESS_THAN;}}
|
||||
"=" {if(DEBUG) {printf( "EQUAL_TO: %s (%d)\n", yytext, EQUAL_TO);} else {return EQUAL_TO;}}
|
||||
":=" {if(DEBUG) {printf( "ASSIGN: %s (%d)\n", yytext, ASSIGN);} else {return ASSIGN;}}
|
||||
"!" {if(DEBUG) {printf( "NOT: %s (%d)\n", yytext, NOT);} else {return NOT;}}
|
||||
"&" {if(DEBUG) {printf( "AND: %s (%d)\n", yytext, AND);} else {return AND;}}
|
||||
"|" {if(DEBUG) {printf( "OR: %s (%d)\n", yytext, OR);} else {return OR;}}
|
||||
"." {if(DEBUG) {printf( "DOT: %s (%d)\n", yytext, DOT);} else {return DOT;}}
|
||||
"+" {if(DEBUG) {printf( "ADD: %s (%d)\n", yytext, ADD);} else {return ADD;}}
|
||||
"-" {if(DEBUG) {printf( "SUB_OR_NEG: %s (%d)\n", yytext, SUB_OR_NEG);} else {return SUB_OR_NEG;}}
|
||||
"*" {if(DEBUG) {printf( "MUL: %s (%d)\n", yytext, MUL);} else {return MUL;}}
|
||||
"/" {if(DEBUG) {printf( "DIV: %s (%d)\n", yytext, DIV);} else {return DIV;}}
|
||||
"%" {if(DEBUG) {printf( "REM: %s (%d)\n", yytext, REM);} else {return REM;}}
|
||||
"<" {if(DEBUG) {printf( "LESS_THAN: %s (%d)\n", yytext, LESS_THAN);} else {return LESS_THAN;}}
|
||||
"=" {if(DEBUG) {printf( "EQUAL_TO: %s (%d)\n", yytext, EQUAL_TO);} else {return EQUAL_TO;}}
|
||||
":=" {if(DEBUG) {printf( "ASSIGN: %s (%d)\n", yytext, ASSIGN);} else {return ASSIGN;}}
|
||||
"!" {if(DEBUG) {printf( "NOT: %s (%d)\n", yytext, NOT);} else {return NOT;}}
|
||||
"&" {if(DEBUG) {printf( "AND: %s (%d)\n", yytext, AND);} else {return AND;}}
|
||||
"|" {if(DEBUG) {printf( "OR: %s (%d)\n", yytext, OR);} else {return OR;}}
|
||||
"." {if(DEBUG) {printf( "DOT: %s (%d)\n", yytext, DOT);} else {return DOT;}}
|
||||
|
||||
";" {if(DEBUG) {printf( "SEMI_COLON: %s (%d)\n", yytext, SEMI_COLON);} else {return SEMI_COLON;}}
|
||||
":" {if(DEBUG) {printf( "COLON: %s (%d)\n", yytext, COLON);} else {return COLON;}}
|
||||
"," {if(DEBUG) {printf( "COMMA: %s (%d)\n", yytext, COMMA);} else {return COMMA;}}
|
||||
"->" {if(DEBUG) {printf( "ARROW: %s (%d)\n", yytext, ARROW);} else {return ARROW;}}
|
||||
";" {if(DEBUG) {printf( "SEMI_COLON: %s (%d)\n", yytext, SEMI_COLON);} else {return SEMI_COLON;}}
|
||||
":" {if(DEBUG) {printf( "COLON: %s (%d)\n", yytext, COLON);} else {return COLON;}}
|
||||
"," {if(DEBUG) {printf( "COMMA: %s (%d)\n", yytext, COMMA);} else {return COMMA;}}
|
||||
"->" {if(DEBUG) {printf( "ARROW: %s (%d)\n", yytext, ARROW);} else {return ARROW;}}
|
||||
|
||||
{DIGIT}+ {if(DEBUG) {printf( "C_INTEGER: %s (%d)\n", yytext, C_INTEGER);} else {return C_INTEGER;}}
|
||||
'{CHAR}' {if(DEBUG) {printf( "C_CHARACTER: %s (%d)\n", yytext, C_CHARACTER);} else {return C_CHARACTER;}}
|
||||
\"{SCHAR}*\" {if(DEBUG) {printf( "C_STRING: %s (%d)\n", yytext, C_STRING);} else {return C_STRING;}}
|
||||
\(\*{STARCOM}*\*\)|\(\*{PARENCOM}*\*\) {if(DEBUG) {printf( "COMMENT: %s (%d)\n", yytext, COMMENT);} else {return COMMENT;}}
|
||||
{DIGIT}+ {if(DEBUG) {printf( "C_INTEGER: %s (%d)\n", yytext, C_INTEGER);} else {return C_INTEGER;}}
|
||||
'{CHAR}' {if(DEBUG) {printf( "C_CHARACTER: %s (%d)\n", yytext, C_CHARACTER);} else {return C_CHARACTER;}}
|
||||
\"{SCHAR}*\" {if(DEBUG) {printf( "C_STRING: %s (%d)\n", yytext, C_STRING);} else {return C_STRING;}}
|
||||
{COMMENT} {if(DEBUG) {printf( "COMMENT: %s (%d)\n", yytext, COMMENT);} else {return COMMENT;}}
|
||||
|
||||
"(" {if(DEBUG) {printf( "L_PAREN: %s (%d)\n", yytext, L_PAREN);} else {return L_PAREN;}}
|
||||
")" {if(DEBUG) {printf( "R_PAREN: %s (%d)\n", yytext, R_PAREN);} else {return R_PAREN;}}
|
||||
"(" {if(DEBUG) {printf( "L_PAREN: %s (%d)\n", yytext, L_PAREN);} else {return L_PAREN;}}
|
||||
")" {if(DEBUG) {printf( "R_PAREN: %s (%d)\n", yytext, R_PAREN);} else {return R_PAREN;}}
|
||||
"[" {if(DEBUG) {printf( "L_BRACKET: %s (%d)\n", yytext, L_BRACKET);} else {return L_BRACKET;}}
|
||||
"]" {if(DEBUG) {printf( "R_BRACKET: %s (%d)\n", yytext, R_BRACKET);} else {return R_BRACKET;}}
|
||||
"{" {if(DEBUG) {printf( "L_BRACE: %s (%d)\n", yytext, L_BRACE);} else {return L_BRACE;}}
|
||||
"}" {if(DEBUG) {printf( "R_BRACE: %s (%d)\n", yytext, R_BRACE);} else {return R_BRACE;}}
|
||||
|
||||
"[" {if(DEBUG) {printf( "L_BRACKET: %s (%d)\n", yytext, L_BRACKET);} else {return L_BRACKET;}}
|
||||
"]" {if(DEBUG) {printf( "R_BRACKET: %s (%d)\n", yytext, R_BRACKET);} else {return R_BRACKET;}}
|
||||
"true" {if(DEBUG) {printf( "C_TRUE: %s (%d)\n", yytext, C_TRUE);} else {return C_TRUE;}}
|
||||
"false" {if(DEBUG) {printf( "C_FALSE: %s (%d)\n", yytext, C_FALSE);} else {return C_FALSE;}}
|
||||
"null" {if(DEBUG) {printf( "C_NULL: %s (%d)\n", yytext, C_NULL);} else {return C_NULL;}}
|
||||
|
||||
"{" {if(DEBUG) {printf( "L_BRACE: %s (%d)\n", yytext, L_BRACE);} else {return L_BRACE;}}
|
||||
"}" {if(DEBUG) {printf( "R_BRACE: %s (%d)\n", yytext, R_BRACE);} else {return R_BRACE;}}
|
||||
{ID} {if(DEBUG) {printf( "ID: %s (%d)\n", yytext, ID);} else {/*yylval.words = strdup(yytext);*/ return ID;}}
|
||||
|
||||
"true" {if(DEBUG) {printf( "C_TRUE: %s (%d)\n", yytext, C_TRUE);} else {return C_TRUE;}}
|
||||
"false" {if(DEBUG) {printf( "C_FALSE: %s (%d)\n", yytext, C_FALSE);} else {return C_FALSE;}}
|
||||
"null" {if(DEBUG) {printf( "C_NULL: %s (%d)\n", yytext, C_NULL);} else {return C_NULL;}}
|
||||
|
||||
{ID} {if(DEBUG) {printf( "ID: %s (%d)\n", yytext, ID);} else {yylval.words = strdup(yytext); return ID;}}
|
||||
|
||||
\n {line_number++; column_number = 1;}
|
||||
\t {column_number++;}
|
||||
" " {column_number++;}
|
||||
. {column_number++; return 1999;}
|
||||
\n {line_number++; column_number = 1;}
|
||||
\t {column_number++;}
|
||||
" " {column_number++;}
|
||||
. {column_number++; return 1999;}
|
||||
|
||||
%%
|
||||
|
290
src/runner.c
290
src/runner.c
@ -1,177 +1,183 @@
|
||||
//#include "symbol_table.h"
|
||||
// #include "symbol_table.h"
|
||||
#include "runner.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc == 1) {
|
||||
fprintf(stderr, "INVALID INPUT: Include a .alpha file or use -help for more inputs \n");
|
||||
return -1;
|
||||
} else if (argc == 2) {
|
||||
//can be either -help or .alpha file
|
||||
if (is_help(argv[1])) {
|
||||
return 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");
|
||||
if (argc == 1) {
|
||||
fprintf(
|
||||
stderr,
|
||||
"INVALID INPUT: Include a .alpha file or use -help for more inputs \n");
|
||||
return -1;
|
||||
} else if (argc == 2) {
|
||||
// can be either -help or .alpha file
|
||||
if (is_help(argv[1])) {
|
||||
return 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;
|
||||
}
|
||||
} else {
|
||||
//last input must be .alpha
|
||||
if (is_alpha_file(argv[argc - 1], strlen(argv[argc - 1])) != 0) {
|
||||
fprintf(stderr, "INVALID INPUT: Include a .alpha file at end of input or use -help for more inputs \n");
|
||||
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;
|
||||
}
|
||||
}
|
||||
alpha_file = fopen(argv[argc - 1], "r");
|
||||
}
|
||||
fprintf(stderr,
|
||||
"INVALID INPUT: Include a .alpha file or use -help for more "
|
||||
"inputs\n");
|
||||
return -1;
|
||||
}
|
||||
return run(alpha_file);
|
||||
} else {
|
||||
// last input must be .alpha
|
||||
if (is_alpha_file(argv[argc - 1], strlen(argv[argc - 1])) != 0) {
|
||||
fprintf(stderr,
|
||||
"INVALID INPUT: Include a .alpha file at end of input or use "
|
||||
"-help for more inputs \n");
|
||||
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;
|
||||
}
|
||||
}
|
||||
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 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())) {
|
||||
if (tok_flag != NULL) {
|
||||
fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number, token, yytext);
|
||||
}
|
||||
if(token == COMMENT){
|
||||
for (int i = 0; i < yyleng; i++) {
|
||||
if(yytext[i] == '\n'){
|
||||
line_number++;
|
||||
column_number = 0;
|
||||
}
|
||||
column_number++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if(token == 1999){
|
||||
printf("On line number %d and column number %d we have an invalid character:%s\n",line_number,column_number,yytext);
|
||||
//return -1;
|
||||
}
|
||||
column_number += yyleng;
|
||||
}
|
||||
|
||||
if (st_flag != NULL) {
|
||||
//output symbol table, file pointer is
|
||||
//print_symbol_table(top,st_flag);
|
||||
}
|
||||
|
||||
if (yyin != NULL) {
|
||||
fclose(yyin);
|
||||
}
|
||||
|
||||
int token;
|
||||
// check that file exists
|
||||
if (alpha == NULL) {
|
||||
fprintf(stderr, "INPUT FILE NOT FOUND\n");
|
||||
return -1;
|
||||
}
|
||||
yyin = alpha;
|
||||
while (0 != (token = yylex())) {
|
||||
if (tok_flag != NULL) {
|
||||
fclose(tok_flag);
|
||||
fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number, token,
|
||||
yytext);
|
||||
}
|
||||
if (token == COMMENT) {
|
||||
for (int i = 0; i < yyleng; i++) {
|
||||
if (yytext[i] == '\n') {
|
||||
line_number++;
|
||||
column_number = 0;
|
||||
}
|
||||
column_number++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (token == 1999) {
|
||||
printf(
|
||||
"On line number %d and column number %d we have an invalid "
|
||||
"character:%s\n",
|
||||
line_number, column_number, yytext);
|
||||
// return -1;
|
||||
}
|
||||
column_number += yyleng;
|
||||
}
|
||||
|
||||
return 0;
|
||||
if (st_flag != NULL) {
|
||||
// output symbol table, file pointer is
|
||||
// print_symbol_table(top,st_flag);
|
||||
}
|
||||
|
||||
if (yyin != NULL) {
|
||||
fclose(yyin);
|
||||
}
|
||||
|
||||
if (tok_flag != NULL) {
|
||||
fclose(tok_flag);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool is_help(char * input) {
|
||||
if (strcmp(input, "-help") == 0) {
|
||||
printf("%s", HELP);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
bool is_help(char *input) {
|
||||
if (strcmp(input, "-help") == 0) {
|
||||
printf("%s", HELP);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int new_file(char *arg, char *alpha) {
|
||||
int type_len;
|
||||
const char *basename = alpha;
|
||||
const char *slash = strchr(alpha, '/');
|
||||
int type_len;
|
||||
const char *basename = alpha;
|
||||
const char *slash = strchr(alpha, '/');
|
||||
|
||||
while (slash != NULL) {
|
||||
basename = slash + 1;
|
||||
slash = strchr(basename, '/');
|
||||
}
|
||||
|
||||
mkdir("./out", 0777);
|
||||
while (slash != NULL) {
|
||||
basename = slash + 1;
|
||||
slash = strchr(basename, '/');
|
||||
}
|
||||
|
||||
char *new_basename = calloc(strlen(basename) + 5, sizeof(char));
|
||||
strcpy(new_basename, "./out/");
|
||||
strcat(new_basename, basename);
|
||||
basename = new_basename;
|
||||
mkdir("./out", 0777);
|
||||
|
||||
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;
|
||||
}
|
||||
char *new_basename = calloc(strlen(basename) + 5, sizeof(char));
|
||||
strcpy(new_basename, "./out/");
|
||||
strcat(new_basename, basename);
|
||||
basename = new_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
|
||||
int basename_len = strlen(basename);
|
||||
char *file_name = calloc(basename_len - ALPHA_OFFSET + type_len + 2, sizeof(char));
|
||||
// calculate lengths
|
||||
int basename_len = strlen(basename);
|
||||
char *file_name =
|
||||
calloc(basename_len - ALPHA_OFFSET + type_len + 2, sizeof(char));
|
||||
|
||||
//coy filename and add extension
|
||||
strncpy(file_name, basename, basename_len - ALPHA_OFFSET);
|
||||
strcat(file_name, ".");
|
||||
strcat(file_name, arg + 1);
|
||||
// coy filename and add extension
|
||||
strncpy(file_name, basename, basename_len - ALPHA_OFFSET);
|
||||
strcat(file_name, ".");
|
||||
strcat(file_name, arg + 1);
|
||||
|
||||
|
||||
if (strcmp(arg, "-tok") == 0) {
|
||||
tok_flag = fopen(file_name, "w");
|
||||
} else if (strcmp(arg, "-st") == 0) {
|
||||
st_flag = fopen(file_name, "w");
|
||||
}
|
||||
return 0;
|
||||
if (strcmp(arg, "-tok") == 0) {
|
||||
tok_flag = fopen(file_name, "w");
|
||||
} else if (strcmp(arg, "-st") == 0) {
|
||||
st_flag = fopen(file_name, "w");
|
||||
}
|
||||
return 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 0; //is alpha file
|
||||
if (strcmp(".alpha", alpha + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) {
|
||||
return -1; // not alpha file
|
||||
}
|
||||
return 0; // is alpha file
|
||||
}
|
||||
|
||||
void enter_scope(int line, int column){
|
||||
curr = CreateScope(curr, line, column);
|
||||
void enter_scope(int line, int column) {
|
||||
curr = CreateScope(curr, line, column);
|
||||
}
|
||||
void exit_scope() {
|
||||
if(curr->Parent_Scope == NULL){
|
||||
printf("Can't close top");
|
||||
return;
|
||||
}
|
||||
curr = curr->Parent_Scope;
|
||||
if (curr->Parent_Scope == NULL) {
|
||||
printf("Can't close top");
|
||||
return;
|
||||
}
|
||||
curr = curr->Parent_Scope;
|
||||
}
|
||||
|
34
src/runner.h
34
src/runner.h
@ -1,29 +1,35 @@
|
||||
#define ALPHA_OFFSET 6
|
||||
#define TOK_LEN 3
|
||||
#define ST_LEN 2
|
||||
#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-help print this message and exit the alpha compiler\n"
|
||||
//use to set flags for arg types
|
||||
#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-help print this message and exit " \
|
||||
"the alpha compiler\n"
|
||||
// use to set flags for arg types
|
||||
#define SET_FLAG 1
|
||||
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../tmp/flex.h"
|
||||
#include "typedefs.h"
|
||||
#include <stdbool.h>
|
||||
#include "symbol_table.h"
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "../tmp/flex.h"
|
||||
#include "symbol_table.h"
|
||||
#include "typedefs.h"
|
||||
|
||||
extern int line_number, column_number;
|
||||
extern char *yytext;
|
||||
extern FILE *yyin;
|
||||
int arg;
|
||||
|
||||
SymbolTable * top;
|
||||
SymbolTable * curr;
|
||||
SymbolTable *top;
|
||||
SymbolTable *curr;
|
||||
|
||||
// int main(int argc, char* argv[]);
|
||||
char *is_tok(int argc, char* argv[]);
|
||||
char *is_tok(int argc, char *argv[]);
|
||||
// int is_alpha_file(char *file, int file_len);
|
||||
void enter_scope(int, int);
|
||||
void exit_scope(void);
|
||||
@ -33,9 +39,9 @@ FILE *tok_flag = NULL;
|
||||
FILE *st_flag = NULL;
|
||||
int no_flag = 0;
|
||||
|
||||
int main(int argc, char* argv[]);
|
||||
int main(int argc, char *argv[]);
|
||||
int new_file(char *arg, char *alpha);
|
||||
int is_alpha_file(char *alpha, int file_len);
|
||||
bool is_help(char * input);
|
||||
int run(FILE*alpha);
|
||||
int check_flag(char * arg, char* alpha);
|
||||
bool is_help(char *input);
|
||||
int run(FILE *alpha);
|
||||
int check_flag(char *arg, char *alpha);
|
||||
|
Reference in New Issue
Block a user