aboutsummaryrefslogtreecommitdiff
path: root/demos/sample-files/gram-skeleton.c
diff options
context:
space:
mode:
authorkartofen <kartofen.mail.0@protonmail.com>2025-08-03 23:53:24 +0300
committerkartofen <kartofen.mail.0@protonmail.com>2025-08-03 23:53:24 +0300
commit1c83c514c8108fccfec9764da5e4563b98eb871b (patch)
treeccc6657a0b24900a17cf90cfd0676c8123492566 /demos/sample-files/gram-skeleton.c
parent059ee9afcc575572f87f224c93288e2835cd1a52 (diff)
calc implemented in my grammar
Diffstat (limited to 'demos/sample-files/gram-skeleton.c')
-rw-r--r--demos/sample-files/gram-skeleton.c48
1 files changed, 25 insertions, 23 deletions
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 <stdint.h>
#include <ctype.h>
+#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)) {