diff options
| author | kartofen <kartofen.mail.0@protonmail.com> | 2025-10-08 20:21:40 +0300 |
|---|---|---|
| committer | kartofen <kartofen.mail.0@protonmail.com> | 2025-10-08 20:21:40 +0300 |
| commit | bcd3391e98511f5913149b87eb8617aaaa4beb6b (patch) | |
| tree | e21a9be998a2ae9a4007b7c631c64dcb5f9b7277 /demos | |
| parent | adcadb01becb36c79d881080d399328de051e301 (diff) | |
edit README and build extend ninja.m4
Diffstat (limited to 'demos')
| -rw-r--r-- | demos/instant-parser.c | 79 | ||||
| -rw-r--r-- | demos/sample-files/calc-skeleton.c | 5 |
2 files changed, 3 insertions, 81 deletions
diff --git a/demos/instant-parser.c b/demos/instant-parser.c deleted file mode 100644 index 8af2318..0000000 --- a/demos/instant-parser.c +++ /dev/null @@ -1,79 +0,0 @@ -#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; - -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, -->, 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), -}; - -struct production *grammar = _grammar; -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 - -int __prod0_action(int *stack_head) { return *(stack_head-4); } -int __prod1_action(int *stack_head) { return *(stack_head-7) + *(stack_head-1); } -int __prod2_action(int *stack_head) { return *(stack_head-7) + *(stack_head-1); } -int __prod3_action(int *stack_head) { return *(stack_head - 1); } -int __prod4_action(int *stack_head) { return *(stack_head - 4); } -int __prod5_action(int *stack_head) { return *(stack_head - 1); } -int __prod6_action(int *stack_head) { return 0; } -int __prod7_action(int *stack_head) { return 1; } - -typedef int (*semantic_action_fn)(int *stack_head); -semantic_action_fn *semantic_actions = (semantic_action_fn[]){ - __prod0_action, __prod1_action, __prod2_action, __prod3_action, - __prod4_action, __prod5_action, __prod6_action, __prod7_action, -}; - -#include "slr-table.c" -#include "util-tables.c" -#include "lr-parser.c" - -int main(void) -{ - util_tables_fill(); - int r = 0; - if((r = table_fill())) goto cleanup; - - table_print(); - printf("RESULT: %d\n", lr_parser()); - -cleanup: - table_free(); - util_tables_free(); - - return r; -} diff --git a/demos/sample-files/calc-skeleton.c b/demos/sample-files/calc-skeleton.c index 414293e..a5cd3cc 100644 --- a/demos/sample-files/calc-skeleton.c +++ b/demos/sample-files/calc-skeleton.c @@ -40,8 +40,9 @@ int main(int argc, char **argv) input = next_token(argv[1]); - int value; - if(lr_parser(&value)) return 1; + struct lr_parseinfo parseinfo; + int value = *(intptr_t *)lr_parser(&parseinfo); + if(parseinfo.type) return 1; printf("INPUT: '%s'\n", argv[1]); printf("OUTPUT: %d\n", value); |
