Combined binaries (parser + alpha)

This commit is contained in:
Scarlett
2025-03-05 14:01:34 -05:00
parent 45940c0500
commit c2f28b6836
5 changed files with 47 additions and 37 deletions

View File

@ -26,11 +26,8 @@ tmp/runner.o: src/runner.c src/runner.h tmp/flex.h
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 : clean tmp/lex.yy.c tmp/grammar.tab.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
$(CC) $(CFLAGS) -o $(EXE) tmp/runner.o tmp/grammar.tab.c tmp/lex.yy.c
debug: CFLAGS += -DDEBUG=1
debug: clean compiler
@ -46,6 +43,7 @@ test-s2:
chmod +x ./check.sh
$(foreach test, $(TESTS-S2), ./$(EXE) -tok $(test);)
./check.sh
$(foreach test, $(TESTS-S2), ./$(EXE) $(test);)
clean:
rm -f *.o

View File

@ -253,10 +253,10 @@ types:
%%
void yyerror(const char *err) {
// fprintf(stderr, "ERROR: %s at token %s at line number %d,column number %d\n", err,yytext,line_number,column_number);
fprintf(stderr, "ERROR: %s at token %s at line number %d,column number %d\n", err,yytext,line_number,column_number);
}
int main() {
/* int main() {
//char *str = strdup("taco");
//cur_value = NULL;
//cur_type = NULL;
@ -276,4 +276,4 @@ int main() {
//}
//}
return 0;
}
} */

View File

@ -6,7 +6,7 @@
%{
#include <stdbool.h>
#include "grammar.tab.h"
#include "../tmp/grammar.tab.h"
#ifndef DEBUG
#define DEBUG 0
#endif
@ -76,7 +76,7 @@ SCHAR \\n|\\t|\\\"|[^\"\n\\]
"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;}}
{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++;}

View File

@ -5,8 +5,7 @@
int main(int argc, char *argv[]) {
if (argc == 1) {
fprintf(
stderr, INVALID);
fprintf(stderr, INVALID);
return -1;
}
@ -60,35 +59,46 @@ int check_flag(char *arg, char *alpha) {
int run(FILE *alpha) {
int token;
// check that file exists
curr = CreateScope(NULL, 1, 1);
// If file is not found
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++;
// TOK FLAG
if (tok_flag != NULL) {
while (0 != (token = yylex())) {
if (tok_flag != NULL) {
fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number,
token, yytext);
}
continue;
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);
}
column_number += yyleng;
}
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;
fclose(tok_flag);
if (yyin != NULL) {
fclose(yyin);
}
column_number += yyleng;
return 0;
}
if (st_flag != NULL) {
@ -96,14 +106,15 @@ int run(FILE *alpha) {
// print_symbol_table(top,st_flag);
}
yyparse();
FILE *f = fdopen(1, "w");
print_symbol_table(getAncestor(curr), f);
fclose(f);
if (yyin != NULL) {
fclose(yyin);
}
if (tok_flag != NULL) {
fclose(tok_flag);
}
return 0;
}

View File

@ -22,7 +22,8 @@
#include "../tmp/flex.h"
#include "symbol_table.h"
#include "typedefs.h"
//#include "typedefs.h"
#include "../tmp/grammar.tab.h"
extern int line_number, column_number;
extern char *yytext;