diff options
Diffstat (limited to 'demos')
-rw-r--r-- | demos/generate-parser.c | 89 | ||||
-rw-r--r-- | demos/instant-parser.c | 6 | ||||
-rw-r--r-- | demos/sample-files/arithmetic-defs.c (renamed from demos/sample-files/defs.c) | 6 | ||||
-rw-r--r-- | demos/sample-files/lalr-defs.c | 35 |
4 files changed, 105 insertions, 31 deletions
diff --git a/demos/generate-parser.c b/demos/generate-parser.c index d32a697..5766223 100644 --- a/demos/generate-parser.c +++ b/demos/generate-parser.c @@ -2,30 +2,35 @@ #include <stdlib.h> #include <dlfcn.h> +#define DEFUALT_PATH "./bin" +#define DEFUALT_TYPE "lalr-table" + #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; char **semantic_action_str; -#include "util-tables.c" +#include "parts/table.h" +struct action **table; +size_t table_states; +int (*table_fill)(); +void (*table_free)(); -#define _LAZY_LALR -#include "clr-table.c" +#include "util-tables.c" void *xdlsym(void *handle, char *sym) { void *r = dlsym(handle, sym); if(!r) { fprintf(stderr, "ERROR: Symbol '%s' was not found\n", sym); + dlclose(handle); exit(1); } return r; @@ -34,26 +39,60 @@ void *xdlsym(void *handle, char *sym) #define GET_VARIABLE(var, handle) \ var = *(typeof(&var))xdlsym(handle, #var) -int main(int argc, char **argv) + +char *modpath(char *name) { - if(argc != 2) return 1; + static char fullpath[128]; + char *path = getenv("GENERATE_PARSER_PATH"); + if(!path) path = DEFUALT_PATH; - void *handle = dlopen(argv[1], RTLD_LAZY); - if(!handle) { puts(dlerror()); return 1; } + snprintf(fullpath, 128, "%s/%s.so", path, name); + return fullpath; +} - 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); +int main(int argc, char **argv) +{ + if(argc < 2) return -1; + + void *table_handle; + void *def_handle; + + if(argc == 2) { + table_handle = dlopen(modpath(DEFUALT_TYPE), RTLD_LAZY); + if(!table_handle) { fputs(dlerror(), stderr); return 1; } + def_handle = dlopen(argv[1], RTLD_LAZY); + if(!def_handle) { fputs(dlerror(), stderr); return 1; } + } else if(argc == 4 && + argv[1][0] == '-' && argv[1][1] == 't') { + table_handle = dlopen(modpath(argv[2]), RTLD_LAZY); + if(!table_handle) { fputs(dlerror(), stderr); return 1; } + def_handle = dlopen(argv[3], RTLD_LAZY); + if(!def_handle) { fputs(dlerror(), stderr); return 1; } + } else return -1; + + GET_VARIABLE(table, table_handle); + GET_VARIABLE(table_states, table_handle); + GET_VARIABLE(table_fill, table_handle); + GET_VARIABLE(table_free, table_handle); + + GET_VARIABLE(total_symbols, def_handle); + GET_VARIABLE(symbol_is_terminal, def_handle); + GET_VARIABLE(symbol_is_input_end, def_handle); + GET_VARIABLE(symbol_is_valid, def_handle); + GET_VARIABLE(grammar, def_handle); + GET_VARIABLE(total_productions, def_handle); + GET_VARIABLE(semantic_action_str, def_handle); util_tables_fill(); - table_fill() && (fprintf(stderr, "ERROR: Table couldn't be generated\n"), exit(1), 1); + + int r = 0; + if((r = table_fill())) { + fprintf(stderr, "ERROR: Table couldn't be generated\n"); + goto cleanup; + } printf("size_t total_symbols = %zu;\n", total_symbols); - printf("IMPLEMENT_FUNCPTR(int, symbol_is_valid, (symbol s), {return s < total_symbols;})\n"); + printf("IMPLEMENT_FUNCPTR(int, symbol_is_valid, (symbol s)) {return s < total_symbols;}\n"); printf("struct production _grammar[] = {\n"); grammar_print_cstyle(); @@ -68,25 +107,25 @@ int main(int argc, char **argv) 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("#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("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("__prod%zu_action, ", i); printf("};"); +cleanup: table_free(); util_tables_free(); - - dlclose(handle); - return 0; + dlclose(table_handle); + dlclose(def_handle); + return r; } diff --git a/demos/instant-parser.c b/demos/instant-parser.c index 57d1f2f..06990f6 100644 --- a/demos/instant-parser.c +++ b/demos/instant-parser.c @@ -16,9 +16,9 @@ enum symbol { size_t total_symbols = SYMBOLS_END; -IMPLEMENT_FUNCPTR(int, symbol_is_terminal, (symbol s), { return s < EP; }) -IMPLEMENT_FUNCPTR(int, symbol_is_input_end, (symbol s), { return s == END_INPUT; }) -IMPLEMENT_FUNCPTR(int, symbol_is_valid,(symbol s), { return s < SYMBOLS_END; }) +IMPLEMENT_FUNCPTR(int, symbol_is_terminal, (symbol s)) { return s < EP; } +IMPLEMENT_FUNCPTR(int, symbol_is_input_end, (symbol s)) { return s == END_INPUT; } +IMPLEMENT_FUNCPTR(int, symbol_is_valid,(symbol s)) { return s < SYMBOLS_END; } #include "parts/grammar.h" #define PROD(LHS, _, ...) {LHS, (symbol[]){__VA_ARGS__}, sizeof((symbol[]){__VA_ARGS__})/sizeof(symbol)} diff --git a/demos/sample-files/defs.c b/demos/sample-files/arithmetic-defs.c index 76a0534..9a0db22 100644 --- a/demos/sample-files/defs.c +++ b/demos/sample-files/arithmetic-defs.c @@ -16,9 +16,9 @@ enum symbol { size_t total_symbols = SYMBOLS_END; -IMPLEMENT_FUNCPTR(int, symbol_is_terminal, (symbol s), { return s < EP; }) -IMPLEMENT_FUNCPTR(int, symbol_is_input_end, (symbol s), { return s == END_INPUT; }) -IMPLEMENT_FUNCPTR(int, symbol_is_valid,(symbol s), { return s < SYMBOLS_END; }) +IMPLEMENT_FUNCPTR(int, symbol_is_terminal, (symbol s)) { return s < EP; } +IMPLEMENT_FUNCPTR(int, symbol_is_input_end, (symbol s)) { return s == END_INPUT; } +IMPLEMENT_FUNCPTR(int, symbol_is_valid, (symbol s)) { return s < SYMBOLS_END; } #include "parts/grammar.h" #define PROD(LHS, _, ...) {LHS, (symbol[]){__VA_ARGS__}, sizeof((symbol[]){__VA_ARGS__})/sizeof(symbol)} diff --git a/demos/sample-files/lalr-defs.c b/demos/sample-files/lalr-defs.c new file mode 100644 index 0000000..6147661 --- /dev/null +++ b/demos/sample-files/lalr-defs.c @@ -0,0 +1,35 @@ +#include <stdio.h> +#include <stddef.h> + +#include "parts/symbol.h" +enum symbol { + ID, EQUAL, STAR, + END_INPUT, + EP, E, L, R, + SYMBOLS_END, +}; + +size_t total_symbols = SYMBOLS_END; + +IMPLEMENT_FUNCPTR(int, symbol_is_terminal, (symbol s)) { return s < EP; } +IMPLEMENT_FUNCPTR(int, symbol_is_input_end, (symbol s)) { return s == END_INPUT; } +IMPLEMENT_FUNCPTR(int, symbol_is_valid, (symbol s)) { return s < SYMBOLS_END; } + +#include "parts/grammar.h" +#define PROD(LHS, _, ...) {LHS, (symbol[]){__VA_ARGS__}, sizeof((symbol[]){__VA_ARGS__})/sizeof(symbol)} +static struct production _grammar[] = { + PROD(EP, ->, E, END_INPUT), + PROD(E, -->, L, EQUAL, R), + PROD(E, -->, R), + PROD(L, -->, STAR, R), + PROD(L, -->, ID), + PROD(R, -->, L), +}; + +struct production *grammar = _grammar; +size_t total_productions = sizeof(_grammar)/sizeof(*_grammar); + +// #include "???.h" +char **semantic_action_str = (char *([])){ + "", "", "", "", "", "", +}; |