diff options
author | kartofen <mladenovnasko0@gmail.com> | 2025-07-03 19:11:36 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2025-07-03 19:11:36 +0300 |
commit | f2bef76fb369d4c9c3e53dca60eb78b75bb02d97 (patch) | |
tree | 94de181d7dae1d35310e1e9bfbf761b0fc536adf /demos/generate-parser.c | |
parent | 5064a7ebce75a26d0405c92040f1a40187fcc7e3 (diff) |
working more or less parser generator (no semantic action, so pretty much useless
Diffstat (limited to 'demos/generate-parser.c')
-rw-r--r-- | demos/generate-parser.c | 64 |
1 files changed, 56 insertions, 8 deletions
diff --git a/demos/generate-parser.c b/demos/generate-parser.c index b0a769d..fc022be 100644 --- a/demos/generate-parser.c +++ b/demos/generate-parser.c @@ -1,11 +1,37 @@ #include <stdio.h> +#include <stdlib.h> #include <dlfcn.h> -#include <parts/table.h> - -struct action **table; -size_t table_states; +#include "parts/symbol.h" +#include "parts/table.h" size_t total_symbols; +int (*symbol_is_terminal)(symbol s); +int (*symbol_is_input_end)(symbol s); +int (*symbol_is_valid)(symbol s); + +#include "parts/grammar.h" + +struct production *grammar; +size_t total_productions; + +#include "util-tables.c" + +// _LAZY_LALR on clr does not work?? +#include "slr-table.c" + +void *xdlsym(void *handle, char *sym) +{ + void *r = dlsym(handle, sym); + if(!r) { + fprintf(stderr, "ERROR: Symbol \"%s\" was not found\n", sym); + exit(1); + } + return r; +} + + +#define GET_VARIABLE(var, handle) \ + var = *(typeof(&var))xdlsym(handle, #var) int main(int argc, char **argv) { @@ -13,13 +39,35 @@ int main(int argc, char **argv) void *handle = dlopen(argv[1], RTLD_LAZY); if(!handle) { puts(dlerror()); return 1; } + + GET_VARIABLE(total_symbols, handle); + GET_VARIABLE(symbol_is_terminal, handle); + GET_VARIABLE(symbol_is_input_end, handle); + GET_VARIABLE(symbol_is_valid, handle); + GET_VARIABLE(grammar, handle); + GET_VARIABLE(total_productions, handle); + + util_tables_fill(); + table_fill() && (exit(1), 1); - table = *(typeof(&table))dlsym(handle, "table"); - table_states = *(typeof(&table_states))dlsym(handle, "table_states"); - total_symbols = *(typeof(&total_symbols))dlsym(handle, "total_symbols"); + printf("size_t total_symbols = %zu;\n", total_symbols); + printf("IMPLEMENT_FUNCPTR(int, symbol_is_valid, (symbol s), {return s >= 0 && s < total_symbols;})\n"); - table_print(); + printf("struct production _grammar[] = {\n"); + grammar_print_cstyle(); + printf("};\n"); + printf("struct production *grammar = _grammar;"); + printf("size_t total_productions = %zu;\n", total_productions); + + printf("struct action **table = (struct action *([])){\n", table_states, total_symbols); + table_print_cstyle(); + printf("};\n"); + printf("size_t table_states = %zu;\n", table_states); + + table_free(); + util_tables_free(); + dlclose(handle); return 0; } |