From 1c83c514c8108fccfec9764da5e4563b98eb871b Mon Sep 17 00:00:00 2001 From: kartofen Date: Sun, 3 Aug 2025 23:53:24 +0300 Subject: calc implemented in my grammar --- demos/sample-files/gram-skeleton.c | 48 ++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 23 deletions(-) (limited to 'demos/sample-files/gram-skeleton.c') diff --git a/demos/sample-files/gram-skeleton.c b/demos/sample-files/gram-skeleton.c index a5899ac..7a54548 100644 --- a/demos/sample-files/gram-skeleton.c +++ b/demos/sample-files/gram-skeleton.c @@ -4,10 +4,13 @@ #include #include +#define INPUT_CAP 4096 +#define ARENA_CAP 4096 + #define ARENA_IMPLEMENTATION #include "util/arena.h" -static char buf[2048]; +static char buf[ARENA_CAP]; static struct arena_ctx global_arena; static void *xalloc(size_t sz) { void *addr = arena_allocate(&global_arena, sz); @@ -55,26 +58,26 @@ struct strnptr_entry { struct list_head *ptr_new(void *ptr) new_entry(struct ptr_entry, entry, { entry->data = (intptr_t)ptr; - }); + }) struct list_head *num_new(intmax_t num) new_entry(struct ptr_entry, entry, { entry->data = (num << 1) | 0x1; - }); + }) struct list_head *prec_new(struct list_head *idenlist, enum precedence_flag flag) new_entry(struct prec_entry, entry, { entry->ptrlist = idenlist; entry->flag = flag; - }); + }) struct list_head *action_new(struct list_head *idenlist, char *action) new_entry(struct strnptr_entry, entry, { entry->str = action; entry->ptrlist = idenlist; - }); + }) struct list_head *prod_new(char *iden, struct list_head *actionlist) new_entry(struct strnptr_entry, entry, { entry->str = iden; entry->ptrlist = actionlist; - }); + }) void handle_type(struct list_head *terminals, struct list_head *nonterminals) { @@ -87,6 +90,8 @@ void handle_type(struct list_head *terminals, struct list_head *nonterminals) printf("%s, ", (char *)entry->data); printf("SYMBOLS_END };\n"); + printf("size_t total_symbols = %zu;\n", list_len(terminals) + list_len(nonterminals) + 2); + printf("char **symbol_to_str = (char *([])){ "); list_for_each_entry(struct ptr_entry, entry, list, terminals) printf("\"%s\", ", (char *)entry->data); @@ -99,6 +104,7 @@ void handle_type(struct list_head *terminals, struct list_head *nonterminals) (char *)container_of(nonterminals, struct ptr_entry, list)->data); printf("IMPLEMENT_FUNCPTR(int, symbol_is_input_end, (symbol s)) { return s == END_INPUT; }\n"); printf("IMPLEMENT_FUNCPTR(int, symbol_is_valid, (symbol s)) { return s < SYMBOLS_END; }\n"); + } void handle_prec(struct list_head *preclist) @@ -128,7 +134,11 @@ void handle_prod(struct list_head *prodlist) printf("#include \"parts/grammar.h\"\n"); printf("struct production *grammar = (struct production[]){\n"); list_for_each_entry(struct strnptr_entry, e1, list, prodlist) { + if(productions == 0) + list_add(list_get_tail(list_entry(e1->ptrlist, struct strnptr_entry, list)->ptrlist), + ptr_new("END_INPUT")); productions += list_len(e1->ptrlist); + list_for_each_entry(struct strnptr_entry, e2, list, e1->ptrlist) { printf("{%s, (symbol[]){ ", e1->str); list_for_each_entry(struct ptr_entry, e3, list, e2->ptrlist) @@ -173,21 +183,7 @@ static char *next_token(char *str); symbol token_sym(struct token *t) { return t->s; } intptr_t token_val(struct token *t) { return t->v; } -static char *input = (char []){ - "-terminal ID EQUAL STAR;" - "-nonterminal EP E L R." - "" - "-left ID;" - "-right STAR;" - "-left EQUAL." - "" - "EP: E END_INPUT {1};" - "E : L EQUAL R {2}" - " | R {3};" - "L : STAR R {4}" - " | ID {5};" - "R : L {6}." -}; +static char *input; struct token *toklist_eat() { @@ -203,9 +199,15 @@ struct token *toklist_peek() { return &tok; } int main(void) { - global_arena = ARENA_CTX_INIT(buf, sizeof(buf)); + static char input_buf[INPUT_CAP]; + if(fread(input_buf, INPUT_CAP, 1, stdin) == INPUT_CAP) { + fprintf(stderr, "INPUT_CAP reached\n"); + return 1; + } - input = next_token(input); + global_arena = ARENA_CTX_INIT(buf, ARENA_CAP); + + input = next_token(input_buf); intptr_t value; if(lr_parser(&value)) { -- cgit v1.2.3