From fbf41d75c4a6c77a2218f20e2843d99d35930bca Mon Sep 17 00:00:00 2001 From: raffitz Date: Wed, 13 Mar 2019 15:50:34 +0000 Subject: [PATCH] Attempt to debug various faults --- Makefile | 6 +++--- js-api.c | 18 +++++++++++------- lexer.l | 1 - native-test.c | 13 +++++++++---- parser.y | 2 +- 5 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index a6baef6..5d41656 100644 --- a/Makefile +++ b/Makefile @@ -11,16 +11,16 @@ webdice.js: js-api.c expression.o lex.yy.o parser.tab.o $(CC) $^ $(CFLAGS) -o $@ $(LFLAGS) lex.yy.c lex.yy.h: lexer.l parser.tab.h expression.h - flex --header-file=lex.yy.h $< + flex --debug --header-file=lex.yy.h $< parser.tab.c parser.tab.h: parser.y expression.h - bison -d $< + bison --debug -d $< %.o: %.c $(CC) $(CFLAGS) -c $< native-test: native-test.c lex.yy.c parser.tab.c expression.c - gcc -std=gnu99 -Wall -pedantic $^ -o $@ $(LFLAGS) + gcc -m32 -std=gnu99 -Wall -pedantic $^ -o $@ $(LFLAGS) -g .PHONY: clean clean: diff --git a/js-api.c b/js-api.c index 5d68901..a1978c4 100644 --- a/js-api.c +++ b/js-api.c @@ -20,21 +20,24 @@ int yyerror(void* scanner, void* lvalp, char* str){ } void EMSCRIPTEN_KEEPALIVE webdice(char* str){ - union expression* root; + union expression* root = NULL; struct expression_result result; int length; + char* in_block; char* out_block; - console_print("A"); + yydebug = 1; + + in_block = (char*) malloc((2 + strlen(str)) * sizeof(char)); + sprintf(in_block,"%s\n",str); + yyscan_t scanner; - console_print("B"); yylex_init(&scanner); - console_print("C"); - yy_scan_string (str, scanner); - console_print("D"); + yy_scan_string (in_block, scanner); - if(yyparse((void*) scanner, &root) == 0){ + yyparse((void*) scanner, &root); + if(root != NULL){ result = resolve(root); length = strlen(result.text) + 4 * ceill(log10l(result.max)) + 100; out_block = (char*) malloc(length * sizeof(char)); @@ -46,5 +49,6 @@ void EMSCRIPTEN_KEEPALIVE webdice(char* str){ }else{ output_print("

Error!

"); } + free(in_block); yylex_destroy(scanner); } diff --git a/lexer.l b/lexer.l index db80f53..0a91ed1 100644 --- a/lexer.l +++ b/lexer.l @@ -3,7 +3,6 @@ #include #include "expression.h" #include "parser.tab.h" -#define _POSIX_C_SOURCE %} diff --git a/native-test.c b/native-test.c index 82d2bd5..d4dabc1 100644 --- a/native-test.c +++ b/native-test.c @@ -32,7 +32,7 @@ void webdice(char* str){ yy_scan_string (str, scanner); console_print("D"); - if(yyparse((void*) scanner, &root) == 0){ + if(yyparse((void*) scanner, &root) == 0 && root != NULL){ result = resolve(root); length = strlen(result.text) + 4 * ceill(log10l(result.max)) + 100; out_block = (char*) malloc(length * sizeof(char)); @@ -48,8 +48,13 @@ void webdice(char* str){ } int main(){ - webdice("cenas"); - webdice("1d4"); - webdice("5"); + +#ifdef YYDEBUG + yydebug = 1; +#endif + + webdice("1d4\n"); + webdice("cenas\n"); + webdice("5\n"); return 0; } diff --git a/parser.y b/parser.y index e8491f0..a49e920 100644 --- a/parser.y +++ b/parser.y @@ -41,7 +41,7 @@ expression: expression WEBDICE_TOKEN_ADD expression { $$ = webdice_add($1,$3); } ; lines: - | lines WEBDICE_TOKEN_EOL { (*root) = NULL; } + | lines WEBDICE_TOKEN_EOL {} | lines expression WEBDICE_TOKEN_EOL { (*root) = $2; } ;