diff options
author | kartofen <mladenovnasko0@gmail.com> | 2025-07-01 00:11:29 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2025-07-01 00:11:29 +0300 |
commit | a67266ff72280b85fed7ec498967a855a5735639 (patch) | |
tree | f53d551e49e2263b5208b9adf1d4c50943f4d59d /lr-parser.c | |
parent | 7743cb4f8a06ab79a521c4346aac74b47c8ce224 (diff) |
major refactor, more modular (wow because obviously modularity is always a good thing yes), and etc
Diffstat (limited to 'lr-parser.c')
-rw-r--r-- | lr-parser.c | 52 |
1 files changed, 16 insertions, 36 deletions
diff --git a/lr-parser.c b/lr-parser.c index 3b0ecb7..41cc45b 100644 --- a/lr-parser.c +++ b/lr-parser.c @@ -1,42 +1,17 @@ #include <stdio.h> #include <stdlib.h> -typedef unsigned int symbol; -// extern const size_t total_symbols; - -// extern int symbol_is_terminal(symbol s); -// extern int symbol_is_input_end(symbol s); -extern int symbol_is_valid(symbol s); - -extern struct production { - symbol LHS; - symbol *RHS; - size_t nRHS; -} grammar[]; -extern const size_t total_productions; -// extern void grammar_print(); - -struct action { - enum action_type { - ACTION_NOT_SET = 0, ACTION_SHIFT, - ACTION_GOTO, ACTION_REDUCE, - ACTION_ACCEPT - } type; - size_t arg; -}; - -extern struct action *table[]; -extern size_t table_states; -// extern void table_print(); - -extern symbol toklist_eat(); -extern symbol toklist_peek(); +// Requirements +#include "parts/symbol.h" +#include "parts/grammar.h" +#include "parts/table.h" +#include "parts/toklist.h" typedef int stack_item; #define STACK_CAP 128 -stack_item stack_bottom[STACK_CAP]; -stack_item *stack_head = stack_bottom; +static stack_item stack_bottom[STACK_CAP]; +static stack_item *stack_head = stack_bottom; int lr_parser() { @@ -84,6 +59,7 @@ int lr_parser() #ifdef _LR_PARSER_STANDALONE +// implement symbol.h enum symbol { PLUS = 0, MINUS, @@ -100,6 +76,7 @@ const size_t total_symbols = SYMBOLS_END; int symbol_is_valid(symbol s) { return s < SYMBOLS_END; } +// implement grammar.h #define PROD(LHS, _, ...) {LHS, (symbol[]){__VA_ARGS__}, sizeof((symbol[]){__VA_ARGS__})/sizeof(symbol)} struct production grammar[] = { PROD(EP, ->, E, END_INPUT), @@ -114,6 +91,7 @@ struct production grammar[] = { const size_t total_productions = sizeof(grammar)/sizeof(*grammar); +// implement table.h struct action *table[] = { (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},}, @@ -129,13 +107,15 @@ struct action *table[] = { (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 table_states = 13; -symbol toklist[] = {N0, PLUS, N1, END_INPUT}; -symbol *tok = toklist; +// implement toklist +static symbol toklist[] = {N0, PLUS, N1, END_INPUT}; +static symbol *tok = toklist; -symbol toklist_eat() { return *(tok++); } -symbol toklist_peek() { return *tok; } +symbol toklist_eat() { return *(tok++); } // unsafe +symbol toklist_peek() { return *tok; } // unsafe int main(void) { |