diff options
Diffstat (limited to 'demos/generate-parser.c')
-rw-r--r-- | demos/generate-parser.c | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/demos/generate-parser.c b/demos/generate-parser.c index 8d50ad6..d32a697 100644 --- a/demos/generate-parser.c +++ b/demos/generate-parser.c @@ -14,6 +14,8 @@ int (*symbol_is_valid)(symbol s); struct production *grammar; size_t total_productions; +char **semantic_action_str; + #include "util-tables.c" #define _LAZY_LALR @@ -35,19 +37,21 @@ void *xdlsym(void *handle, char *sym) int main(int argc, char **argv) { if(argc != 2) return 1; - + void *handle = dlopen(argv[1], RTLD_LAZY); if(!handle) { puts(dlerror()); return 1; } - GET_VARIABLE(total_symbols, handle); + 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); + GET_VARIABLE(semantic_action_str, handle); + util_tables_fill(); table_fill() && (fprintf(stderr, "ERROR: Table couldn't be generated\n"), exit(1), 1); - + printf("size_t total_symbols = %zu;\n", total_symbols); printf("IMPLEMENT_FUNCPTR(int, symbol_is_valid, (symbol s), {return s < total_symbols;})\n"); @@ -57,15 +61,32 @@ int main(int argc, char **argv) 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); + + printf("struct action **table = (struct action *([])){\n"); table_print_cstyle(); printf("};\n"); printf("size_t table_states = %zu;\n", table_states); + for(size_t i = 0; i < total_productions; i++) { + printf("#define A(n) (*(stack_head-3*%d+3*n-1))\n", grammar[i].nRHS-1); + printf("int __prod%d_action(int *stack_head)\n", i); + printf("{ int v;\n"); + printf(semantic_action_str[i]); + printf("return v; }\n"); + printf("#undef A\n"); + } + + + printf("typedef int (*semantic_action_fn)(int *stack_head);\n"); + + printf("semantic_action_fn *semantic_actions = (semantic_action_fn[]){\n"); + for(size_t i = 0; i < total_productions; i++) + printf("__prod%d_action, ", i); + printf("};"); + table_free(); util_tables_free(); - + dlclose(handle); return 0; } |