aboutsummaryrefslogtreecommitdiff
path: root/demos/sample-files/gram-skeleton.c
diff options
context:
space:
mode:
Diffstat (limited to 'demos/sample-files/gram-skeleton.c')
-rw-r--r--demos/sample-files/gram-skeleton.c175
1 files changed, 163 insertions, 12 deletions
diff --git a/demos/sample-files/gram-skeleton.c b/demos/sample-files/gram-skeleton.c
index 89ef6b4..a5899ac 100644
--- a/demos/sample-files/gram-skeleton.c
+++ b/demos/sample-files/gram-skeleton.c
@@ -1,13 +1,14 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <stdint.h>
#include <ctype.h>
#define ARENA_IMPLEMENTATION
#include "util/arena.h"
-static char buf[1024];
-static struct arena_ctx global_arena = ARENA_CTX_INIT(buf, sizeof(buf));
+static char buf[2048];
+static struct arena_ctx global_arena;
static void *xalloc(size_t sz) {
void *addr = arena_allocate(&global_arena, sz);
if(!addr) {
@@ -17,6 +18,146 @@ static void *xalloc(size_t sz) {
return addr;
}
+#include "parts/precedence.h"
+#include "util/list.h"
+
+static inline struct list_head *list_new_head(struct list_head *head, struct list_head *new)
+{
+ if(head) list_add(new, head);
+ return new;
+}
+
+struct ptr_entry {
+ intptr_t data;
+ struct list_head list;
+};
+
+struct prec_entry {
+ enum precedence_flag flag;
+ struct list_head *ptrlist;
+ struct list_head list;
+};
+
+struct strnptr_entry {
+ char *str;
+ struct list_head *ptrlist;
+ struct list_head list;
+};
+
+#define new_entry(type, entry, __do__) \
+ { \
+ type *entry = xalloc(sizeof(type)); \
+ LIST_EMPTY(&entry->list); \
+ __do__; \
+ return &entry->list; \
+ }
+
+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)
+{
+ printf("#include \"parts/symbol.h\"\n");
+ printf("enum symbol { ");
+ list_for_each_entry(struct ptr_entry, entry, list, terminals)
+ printf("%s, ", (char *)entry->data);
+ printf("END_INPUT, ");
+ list_for_each_entry(struct ptr_entry, entry, list, nonterminals)
+ printf("%s, ", (char *)entry->data);
+ printf("SYMBOLS_END };\n");
+
+ printf("char **symbol_to_str = (char *([])){ ");
+ list_for_each_entry(struct ptr_entry, entry, list, terminals)
+ printf("\"%s\", ", (char *)entry->data);
+ printf("\"END_INPUT\",");
+ list_for_each_entry(struct ptr_entry, entry, list, nonterminals)
+ printf("\"%s\", ", (char *)entry->data);
+ printf("\"SYMBOLS_END\" };\n");
+
+ printf("IMPLEMENT_FUNCPTR(int, symbol_is_terminal, (symbol s)) { return s < %s; }\n",
+ (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)
+{
+ printf("#include \"parts/precedence.h\"\n");
+ printf("struct precedence_def {\n");
+ printf(" int flag;\n");
+ printf(" int *list;\n");
+ printf(" size_t nlist;\n");
+ printf("};\n");
+ printf("struct precedence_def *precedence_defs = (struct precedence_def[]){\n");
+ list_for_each_entry(struct prec_entry, entry, list, preclist) {
+ printf("{ %d, (int[]){", entry->flag);
+ list_for_each_entry(struct ptr_entry, e, list, entry->ptrlist)
+ if((e->data & 0x1) == 0) printf("%s, ", (char *)e->data);
+ else printf("~%ju, ", e->data >> 1);
+ printf("}, %zu}, ", list_len(entry->ptrlist));
+ }
+ printf("};\n");
+ printf("size_t nprecedence_defs = %zu;\n", list_len(preclist));
+}
+
+void handle_prod(struct list_head *prodlist)
+{
+ size_t productions = 0;
+
+ printf("#include \"parts/grammar.h\"\n");
+ printf("struct production *grammar = (struct production[]){\n");
+ list_for_each_entry(struct strnptr_entry, e1, list, prodlist) {
+ 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)
+ printf("%s, ", (char *)e3->data);
+ printf("}, %zu}, \n", list_len(e2->ptrlist));
+ }
+ }
+ printf("};\n");
+ printf("size_t total_productions = %zu;\n", productions);
+
+ printf("char **semantic_action_str = (char *([])){");
+ list_for_each_entry(struct strnptr_entry, e1, list, prodlist)
+ list_for_each_entry(struct strnptr_entry, e2, list, e1->ptrlist)
+ printf("\"%s\", ", e2->str); // todo: escape the quotes
+ printf("};\n");
+}
+
+#define list_new_head(head, new) (intptr_t)list_new_head((struct list_head *)head, (struct list_head *)new)
+
+#define ptr_new(iden) (intptr_t)ptr_new((void *)iden)
+#define num_new(num) (intptr_t)num_new(num)
+#define prec_new(idenlist, flag) (intptr_t)prec_new((struct list_head *)idenlist, flag)
+#define action_new(idenlist, action) (intptr_t)action_new((struct list_head *)idenlist, (char *)action)
+#define prod_new(iden, actionlist) (intptr_t)prod_new((char *)iden, (struct list_head *)actionlist)
+
+#define handle_type(terminals, nonterminals) handle_type((struct list_head *)terminals, (struct list_head *)nonterminals);
+#define handle_prec(preclist) handle_prec((struct list_head *)preclist);
+#define handle_prod(prodlist) handle_prod((struct list_head *)prodlist);
+
// generated
#include "bin/gram.h"
#include "bin/gram.c"
@@ -33,12 +174,19 @@ symbol token_sym(struct token *t) { return t->s; }
intptr_t token_val(struct token *t) { return t->v; }
static char *input = (char []){
- "-left B;"
- "-right C;"
- "-left D;"
+ "-terminal ID EQUAL STAR;"
+ "-nonterminal EP E L R."
""
- "A: B {a}"
- " | C N {d}."
+ "-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}."
};
struct token *toklist_eat()
@@ -55,14 +203,17 @@ struct token *toklist_peek() { return &tok; }
int main(void)
{
+ global_arena = ARENA_CTX_INIT(buf, sizeof(buf));
+
input = next_token(input);
intptr_t value;
if(lr_parser(&value)) {
+ printf(input);
return 1;
}
- printf("OUTPUT: %jd\n", value);
+ fprintf(stderr, "OUTPUT: %jd\n", value);
return 0;
}
@@ -117,10 +268,10 @@ static char *next_token(char *str)
case '-':
off = tillsep(++str);
char *s = substring(str, off);
- if(strcmp(s, "left") == 0) tok.s = D_LEFT;
- else if(strcmp(s, "right") == 0) tok.s = D_RIGHT;
- else if(strcmp(s, "terminal") == 0) tok.s = D_TERMINAL;
- else if(strcmp(s, "nonterminal") == 0) tok.s = D_NONTERM;
+ if(strcmp(s, "terminal") == 0) tok.s = TERMINAL;
+ else if(strcmp(s, "nonterminal") == 0) tok.s = NONTERM;
+ else if(strcmp(s, "left") == 0) tok.s = LEFT;
+ else if(strcmp(s, "right") == 0) tok.s = RIGHT;
else { fprintf(stderr, "ERROR: Unknown directive '-%s'\n", s); goto fail; }
break;
case '{':