From 5064a7ebce75a26d0405c92040f1a40187fcc7e3 Mon Sep 17 00:00:00 2001 From: kartofen Date: Wed, 2 Jul 2025 22:55:08 +0300 Subject: turn clr into lalr and first steps for generating a parser --- lr-parser.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'lr-parser.c') diff --git a/lr-parser.c b/lr-parser.c index 41cc45b..2358164 100644 --- a/lr-parser.c +++ b/lr-parser.c @@ -19,10 +19,10 @@ int lr_parser() #define pop() (--stack_head) #define eat() toklist_eat() #define peek() ({ symbol s = toklist_peek(); if(!symbol_is_valid(s)) return 1; s; }) - + while(1) { struct action a = table[(size_t)*stack_head][peek()]; - + switch(a.type) { case ACTION_SHIFT: push(eat()); @@ -30,7 +30,7 @@ int lr_parser() break; case ACTION_REDUCE: for(size_t i = 0; i < 2*grammar[a.arg].nRHS; i++) pop(); - + symbol lhs = grammar[a.arg].LHS; struct action a_goto = table[(size_t)*stack_head][lhs]; if(a_goto.type != ACTION_GOTO) { @@ -39,7 +39,7 @@ int lr_parser() lhs, (size_t)*stack_head); return 1; } - + push(lhs); push(a_goto.arg); break; @@ -72,7 +72,7 @@ enum symbol { SYMBOLS_END, }; -const size_t total_symbols = SYMBOLS_END; +size_t total_symbols = SYMBOLS_END; int symbol_is_valid(symbol s) { return s < SYMBOLS_END; } @@ -92,7 +92,7 @@ struct production grammar[] = { const size_t total_productions = sizeof(grammar)/sizeof(*grammar); // implement table.h -struct action *table[] = { +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},}, -- cgit v1.2.3