changeset 29:3e38ca5d5feb

gen: Return parse result as output argument.
author Lewin Bormann <lbo@spheniscida.de>
date Mon, 19 Aug 2019 22:30:24 +0200
parents bcc2670db1a6
children 656efbd10859
files gen/debug_parser.c gen/y.lex gen/y.yy
diffstat 3 files changed, 47 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/gen/debug_parser.c	Mon Aug 19 22:16:19 2019 +0200
+++ b/gen/debug_parser.c	Mon Aug 19 22:30:24 2019 +0200
@@ -1,9 +1,17 @@
 #include <stdio.h>
 
+#include <src/expr.h>
+
 #include <gen/y.tab.h>
 
-int parse(FILE* in);
+int parse(FILE* in, yvec_t* out);
 
 int main(int argc, char** argv) {
-    parse(stdin);
+    yvec_t expr;
+    parse(stdin, &expr);
+
+    fprintf(stderr, ">>> full parse result: \n");
+    for (size_t i = 0; i < expr.len; i++) {
+        yexpr_debug(YVEC_AT(&expr, i, yexpr_t));
+    }
 }
--- a/gen/y.lex	Mon Aug 19 22:16:19 2019 +0200
+++ b/gen/y.lex	Mon Aug 19 22:30:24 2019 +0200
@@ -55,6 +55,6 @@
     return 0;
 }
 
-void yyerror(void* _scanner, char const* s) {
+void yyerror(void* _scanner, void* _output, char const* s) {
     fprintf(stderr, "%s\n", s);
 }
--- a/gen/y.yy	Mon Aug 19 22:16:19 2019 +0200
+++ b/gen/y.yy	Mon Aug 19 22:30:24 2019 +0200
@@ -14,7 +14,7 @@
 #include <gen/y.tab.h>
 
 // Our error handler
-void yyerror(void* ignored, char const* s);
+void yyerror(void* ignored, void* ignored2, char const* s);
 
 // Declare flex-generated functions
 int yylex(YYSTYPE* v, void* scanner);
@@ -26,6 +26,7 @@
 
 %define parse.error verbose
 %param {void* scan}
+%parse-param {yvec_t* output_yexprs}
 %define api.pure full
 %define lr.type ielr
 
@@ -48,16 +49,36 @@
 %token TOK_LEFTP
 %token TOK_RIGHTP
 
-%type <exprs> prog
+%type <exprs> many_sexpr
 %type <expr> sexpr
 %type <expr> inner_sexpr
 
 %%
 
-prog: prog sexpr { YVEC_PUSH(&$1, &$2); yexpr_debug(&$2); }
-    | %empty { yvec_t v; YVEC_INIT(&v, 0, yexpr_t); $$ = v; };
+prog: many_sexpr {
+            // Assign overall result (list of yexpr_t) to parser's output
+            // argument.
+            *output_yexprs = $1;
+            printf("finished\n");
+    }
 
-sexpr: TOK_LEFTP inner_sexpr TOK_RIGHTP { printf("found sexpr\n"); $$ = $2; };
+many_sexpr: many_sexpr sexpr {
+            // Push found sexpr to previous sexprs.
+            YVEC_PUSH(&$1, &$2);
+            yexpr_debug(&$2);
+            // Push result on stack.
+            $$ = $1;
+    }
+    | %empty {
+            yvec_t v;
+            YVEC_INIT(&v, 0, yexpr_t);
+            $$ = v;
+    };
+
+sexpr: TOK_LEFTP inner_sexpr TOK_RIGHTP {
+                printf("found sexpr\n");
+                $$ = $2;
+};
 
 inner_sexpr: inner_sexpr TOK_ATOM {
                 printf("found atom %s\n", $2);
@@ -87,10 +108,15 @@
                 yexpr_init_or_extend(&$1, $2);
                 $$ = $1;
            }
-           | %empty { printf("finished sexpr\n"); yexpr_t e; yexpr_init(&e); $$ = e; };
+           | %empty {
+                printf("finished sexpr\n");
+                yexpr_t e;
+                yexpr_init(&e);
+                $$ = e;
+           };
 %%
 
-int parse(FILE* in) {
+int parse(FILE* in, yvec_t* out_exprs) {
     void* scanner;
     assert(0 == yylex_init(&scanner));
     #ifndef DEBUG
@@ -102,7 +128,9 @@
     yyset_in(in, scanner);
     yyset_out(out, scanner);
 
-    yyparse(scanner);
+    YVEC_INIT(out_exprs, 16, yexpr_t);
+
+    yyparse(scanner, out_exprs);
 
     fclose(in);
     fclose(out);