diff options
| author | kartofen <kartofen.mail.0@protonmail.com> | 2025-07-20 01:32:24 +0300 |
|---|---|---|
| committer | kartofen <kartofen.mail.0@protonmail.com> | 2025-07-20 01:32:24 +0300 |
| commit | 34357640c0676f33ad13aac1fe28effc6f6e47c7 (patch) | |
| tree | d656ee61da7d7a0b133aa57311266653ef100569 /demos/generate-parser.c | |
| parent | 174e9b35ce3b6e99e500907f1bb24c6f31f481bf (diff) | |
start of grammar parsing
Diffstat (limited to 'demos/generate-parser.c')
| -rw-r--r-- | demos/generate-parser.c | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/demos/generate-parser.c b/demos/generate-parser.c index 23201fa..48fa48c 100644 --- a/demos/generate-parser.c +++ b/demos/generate-parser.c @@ -5,12 +5,13 @@ #include <unistd.h> // getopt #include <assert.h> -#define DEFUALT_PATH "./bin" -#define DEFUALT_TYPE "lalr-table" #define DEFAULT_OUTPUT "bin/a" +#define DEFUALT_MODPATH "./bin" +#define DEFUALT_TYPE "lalr-table" #include "parts/symbol.h" size_t total_symbols; +char **symbol_to_str; int (*symbol_is_terminal)(symbol s); int (*symbol_is_input_end)(symbol s); int (*symbol_is_valid)(symbol s); @@ -42,8 +43,7 @@ void (*table_free)(); #include "util-tables.c" - -void *xdlsym(void *handle, char *sym) +static void *xdlsym(void *handle, char *sym) { void *r = dlsym(handle, sym); if(!r) { @@ -58,18 +58,18 @@ void *xdlsym(void *handle, char *sym) var = *(typeof(&var))xdlsym(handle, #var) -char *modpath(char *name) +static char *modpath(char *name) { static char fullpath[128]; // TODO: search the GENERATE_PARSER_PATH env var - char *path = DEFUALT_PATH; + char *path = DEFUALT_MODPATH; assert(snprintf(fullpath, 128, "%s/%s.so", path, name) < 128); return fullpath; } -char *add_extension(char *str, char *ext) +static char *add_extension(char *str, char *ext) { static char full[128]; assert((strlen(str) + strlen(ext) + 1) <= 128); @@ -77,7 +77,7 @@ char *add_extension(char *str, char *ext) return strcat(full, ext); } -void set_stdout(char *filename) +static void set_stdout(char *filename) { if(!filename) filename = "/dev/tty"; assert(freopen(filename, "w", stdout)); @@ -114,6 +114,7 @@ int main(int argc, char **argv) GET_VARIABLE(table_free, table_handle); GET_VARIABLE(total_symbols, def_handle); + GET_VARIABLE(symbol_to_str, def_handle); GET_VARIABLE(symbol_is_terminal, def_handle); GET_VARIABLE(symbol_is_input_end, def_handle); GET_VARIABLE(symbol_is_valid, def_handle); @@ -132,8 +133,13 @@ int main(int argc, char **argv) goto cleanup; } + table_print(); + set_stdout(add_extension(output_path, ".c")); printf("size_t total_symbols = %zu;\n", total_symbols); + printf("char **symbol_to_string = (char *([])){\n"); + for(size_t i = 0; i < total_symbols; i++) printf("\"%s\", ", symbol_to_str[i]); + printf("};\n"); printf("IMPLEMENT_FUNCPTR(int, symbol_is_valid, (symbol s)) {return s < total_symbols;}\n"); printf("struct production _grammar[] = {\n"); @@ -157,19 +163,33 @@ int main(int argc, char **argv) for(size_t i = 0; i < total_productions; i++) { printf("#define A(n) (*(stack_head-3*%zu+3*n-1))\n", grammar[i].nRHS-1); - printf("int __prod%zu_action(int *stack_head)\n", i); - printf("{ int v;\n"); - printf(semantic_action_str[i]); + printf("intptr_t __prod%zu_action(intmax_t *stack_head)\n", i); + printf("{ intptr_t v;\n"); + puts(semantic_action_str[i]); printf("return v; }\n"); printf("#undef A\n"); } - printf("typedef int (*semantic_action_fn)(int *stack_head);\n"); + printf("typedef intptr_t (*semantic_action_fn)(intmax_t *stack_head);\n"); printf("semantic_action_fn *semantic_actions = (semantic_action_fn[]){\n"); for(size_t i = 0; i < total_productions; i++) printf("__prod%zu_action, ", i); printf("};"); + + set_stdout(add_extension(output_path, ".h")); + printf("#ifndef GENERATED_H\n"); + printf("#define GENERATED_H\n"); + printf("#include \"parts/symbol.h\"\n"); + printf("enum symbol {\n"); + for(size_t i = 0; i < total_symbols; i++) printf("%s, ", symbol_to_str[i]); + printf("};\n"); + printf("#include \"parts/grammar.h\"\n"); + printf("#include \"parts/table.h\"\n"); + printf("#include <stdint.h>\n"); + printf("typedef intptr_t (*semantic_action_fn)(intmax_t *stack_head);\n"); + printf("extern semantic_action_fn *semantic_actions;\n"); + printf("#endif\n"); set_stdout(NULL); cleanup: @@ -189,10 +209,10 @@ void precedence_tables_fill() for(size_t i = 0; i < nprecedence_defs; i++) for(size_t j = 0; j < precedence_defs[i].nlist; j++) - if(precedence_defs[i].flag >= 0) + if(precedence_defs[i].list[j] >= 0) precedence_symbol[precedence_defs[i].list[j]] = PRECEDENCE_SET(precedence_defs[i].flag, i+1); else - precedence_production[precedence_defs[i].list[j]] = PRECEDENCE_SET(~precedence_defs[i].flag, i+1); + precedence_production[~precedence_defs[i].list[j]] = PRECEDENCE_SET(precedence_defs[i].flag, i+1); for(size_t i = 0; i < total_productions; i++) { if(precedence_production[i]) continue; |
