diff options
Diffstat (limited to 'demos')
-rw-r--r-- | demos/generate-parser.c | 25 | ||||
-rw-r--r-- | demos/instant-parser.c | 61 | ||||
-rw-r--r-- | demos/sample-files/defs.c | 23 |
3 files changed, 109 insertions, 0 deletions
diff --git a/demos/generate-parser.c b/demos/generate-parser.c new file mode 100644 index 0000000..b0a769d --- /dev/null +++ b/demos/generate-parser.c @@ -0,0 +1,25 @@ +#include <stdio.h> +#include <dlfcn.h> + +#include <parts/table.h> + +struct action **table; +size_t table_states; +size_t total_symbols; + +int main(int argc, char **argv) +{ + if(argc != 2) return 1; + + void *handle = dlopen(argv[1], RTLD_LAZY); + if(!handle) { puts(dlerror()); return 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"); + + table_print(); + + dlclose(handle); + return 0; +} diff --git a/demos/instant-parser.c b/demos/instant-parser.c new file mode 100644 index 0000000..3a82b07 --- /dev/null +++ b/demos/instant-parser.c @@ -0,0 +1,61 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "parts/symbol.h" +enum symbol { + PLUS = 0, + MINUS, + LPAREN, + RPAREN, + N0, N1, + END_INPUT, + + EP, E, T, N, + SYMBOLS_END, +}; + +size_t total_symbols = SYMBOLS_END; + +int symbol_is_terminal(symbol s) { return s < EP; } +int symbol_is_input_end(symbol s) { return s == END_INPUT; } +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)} +struct production grammar[] = { + PROD(EP, ->, E, END_INPUT), + PROD(E, -->, E, PLUS, T), + PROD(E, -->, E, MINUS, T), + PROD(E, -->, T), + PROD(T, -->, LPAREN, E, RPAREN), + PROD(T, -->, N), + PROD(N, -->, N0), + PROD(N, -->, N1), +}; + +const size_t total_productions = sizeof(grammar)/sizeof(*grammar); + +#include "parts/toklist.h" +static symbol toklist[] = {N0, PLUS, N1, MINUS, N0, END_INPUT}; +static symbol *tok = toklist; + +symbol toklist_eat() { return *(tok++); } // unsafe +symbol toklist_peek() { return *tok; } // unsafe + +#include "slr-table.c" +#include "util-tables.c" +#include "lr-parser.c" + +int main(void) +{ + util_tables_fill(); + table_fill(); + + int r = 0; + r = lr_parser(); + + table_free(); + util_tables_free(); + + return r; +} diff --git a/demos/sample-files/defs.c b/demos/sample-files/defs.c new file mode 100644 index 0000000..797c86d --- /dev/null +++ b/demos/sample-files/defs.c @@ -0,0 +1,23 @@ +#include <stdio.h> +#include <stddef.h> + +#include <parts/table.h> + +struct action **table = (struct action *([])){ + (struct action[]){{0, 0},{0, 0},{1, 1},{0, 0},{1, 2},{1, 3},{0, 0},{0, 0},{2, 12},{2, 11},{2, 7},}, + (struct action[]){{0, 0},{0, 0},{1, 1},{0, 0},{1, 2},{1, 3},{0, 0},{0, 0},{2, 4},{2, 11},{2, 7},}, + (struct action[]){{3, 6},{3, 6},{0, 0},{3, 6},{0, 0},{0, 0},{3, 6},{0, 0},{0, 0},{0, 0},{0, 0},}, + (struct action[]){{3, 7},{3, 7},{0, 0},{3, 7},{0, 0},{0, 0},{3, 7},{0, 0},{0, 0},{0, 0},{0, 0},}, + (struct action[]){{1, 5},{1, 8},{0, 0},{1, 10},{0, 0},{0, 0},{0, 0},{0, 0},{0, 0},{0, 0},{0, 0},}, + (struct action[]){{0, 0},{0, 0},{1, 1},{0, 0},{1, 2},{1, 3},{0, 0},{0, 0},{0, 0},{2, 6},{2, 7},}, + (struct action[]){{3, 1},{3, 1},{0, 0},{3, 1},{0, 0},{0, 0},{3, 1},{0, 0},{0, 0},{0, 0},{0, 0},}, + (struct action[]){{3, 5},{3, 5},{0, 0},{3, 5},{0, 0},{0, 0},{3, 5},{0, 0},{0, 0},{0, 0},{0, 0},}, + (struct action[]){{0, 0},{0, 0},{1, 1},{0, 0},{1, 2},{1, 3},{0, 0},{0, 0},{0, 0},{2, 9},{2, 7},}, + (struct action[]){{3, 2},{3, 2},{0, 0},{3, 2},{0, 0},{0, 0},{3, 2},{0, 0},{0, 0},{0, 0},{0, 0},}, + (struct action[]){{3, 4},{3, 4},{0, 0},{3, 4},{0, 0},{0, 0},{3, 4},{0, 0},{0, 0},{0, 0},{0, 0},}, + (struct action[]){{3, 3},{3, 3},{0, 0},{3, 3},{0, 0},{0, 0},{3, 3},{0, 0},{0, 0},{0, 0},{0, 0},}, + (struct action[]){{1, 5},{1, 8},{0, 0},{0, 0},{0, 0},{0, 0},{4, 0},{0, 0},{0, 0},{0, 0},{0, 0},}, +}; + +size_t total_symbols = 10; +size_t table_states = 13; |