From 34357640c0676f33ad13aac1fe28effc6f6e47c7 Mon Sep 17 00:00:00 2001 From: kartofen Date: Sun, 20 Jul 2025 01:32:24 +0300 Subject: start of grammar parsing --- lr-parser.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'lr-parser.c') diff --git a/lr-parser.c b/lr-parser.c index 799276d..3b6be84 100644 --- a/lr-parser.c +++ b/lr-parser.c @@ -1,5 +1,6 @@ #include #include +#include // TODO: - check erros and fail safely and // see connection with the lexer @@ -11,16 +12,16 @@ #include "parts/table.h" #include "parts/toklist.h" // and -typedef int (*semantic_action_fn)(int *stack_head); +typedef intptr_t (*semantic_action_fn)(intmax_t *stack_head); extern semantic_action_fn *semantic_actions; -typedef int stack_item; +typedef intmax_t stack_item; #define STACK_CAP 128 static stack_item stack_bottom[STACK_CAP]; static stack_item *stack_head = stack_bottom; -int lr_parser(int *value) +int lr_parser(intptr_t *value) { #define push(item) do { \ if(++stack_head - stack_bottom < STACK_CAP ) *stack_head = item; \ @@ -42,7 +43,7 @@ int lr_parser(int *value) push(a.arg); break; case ACTION_REDUCE: - int semantic_value = semantic_actions[a.arg](stack_head); + intptr_t semantic_value = semantic_actions[a.arg](stack_head); for(size_t i = 0; i < 3*grammar[a.arg].nRHS; i++) pop(); symbol lhs = grammar[a.arg].LHS; @@ -136,7 +137,7 @@ struct token { int v; }; -static struct token toklist[] = {{N0}, {PLUS}, {N1}, {END_INPUT}}; +static struct token toklist[] = {{N0, 0}, {PLUS, 0}, {N1, 0}, {END_INPUT, 0}}; static const size_t ntoklist = sizeof(toklist)/sizeof(*toklist); static size_t tok; @@ -151,15 +152,15 @@ struct token *toklist_peek() { return toklist + tok; } symbol token_sym(struct token *t) { return t->s; } int token_val(struct token *t) { return t->v; } -int none(int *stack_head) {(void)stack_head; return 0;} +intptr_t none(intmax_t *stack_head) {(void)stack_head; return 0;} semantic_action_fn *semantic_actions = (semantic_action_fn[]){none, none, none, none, none, none, none, none}; int main(void) { - int value; + intptr_t value; if(lr_parser(&value)) return 1; - printf("%d\n", value); + printf("%jd\n", value); return 0; } -- cgit v1.2.3