From 0e0c0e0f26fcd669e45604fd5d9bcc2891a932a2 Mon Sep 17 00:00:00 2001 From: kartofen Date: Sat, 5 Jul 2025 12:14:27 +0300 Subject: lalr now acutally works --- lr-parser.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'lr-parser.c') diff --git a/lr-parser.c b/lr-parser.c index 9f7ea24..3ab9af4 100644 --- a/lr-parser.c +++ b/lr-parser.c @@ -18,10 +18,13 @@ static stack_item *stack_head = stack_bottom; int lr_parser() { -#define push(item) ({ if(++stack_head - stack_bottom < STACK_CAP ) (*stack_head = item); else { fprintf(stderr, "ERROR: STACK_CAP exceeded"); return 1; }}) +#define push(item) ({ if(++stack_head - stack_bottom < STACK_CAP ) (*stack_head = item); else { fprintf(stderr, "ERROR: STACK_CAP exceeded\n"); return 1; }}) #define pop() (--stack_head) #define eat() toklist_eat() -#define peek() ({ symbol s = toklist_peek(); if(!symbol_is_valid(s)) return 1; s; }) +#define peek() ({ \ + symbol s = toklist_peek(); \ + if(!symbol_is_valid(s)) { fprintf(stderr, "ERROR: Unknown token '%d'\n", s); return 1;} \ + s; }) while(1) { struct action a = table[(size_t)*stack_head][peek()]; @@ -38,7 +41,7 @@ int lr_parser() struct action a_goto = table[(size_t)*stack_head][lhs]; if(a_goto.type != ACTION_GOTO) { fprintf(stderr, - "ERROR: Expected goto action for symbol %d at state %zu", + "ERROR: Expected goto action for token '%d' at state %zu\n", lhs, (size_t)*stack_head); return 1; } @@ -51,7 +54,7 @@ int lr_parser() case ACTION_NOT_SET: default: fprintf(stderr, - "ERROR: Unexpected error symbol %d at state %zu", + "ERROR: Unexpected token '%d' at state %zu\n", peek(), (size_t)*stack_head); return 1; } -- cgit v1.2.3