aboutsummaryrefslogtreecommitdiff
path: root/demos
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2025-07-05 12:14:27 +0300
committerkartofen <mladenovnasko0@gmail.com>2025-07-05 12:14:27 +0300
commit0e0c0e0f26fcd669e45604fd5d9bcc2891a932a2 (patch)
treef57eb9f80883bdab57d00a97ad97508ecdbb0c2d /demos
parentf2bef76fb369d4c9c3e53dca60eb78b75bb02d97 (diff)
lalr now acutally works
Diffstat (limited to 'demos')
-rw-r--r--demos/generate-parser.c12
-rw-r--r--demos/sample-files/parser-skeleton.c30
2 files changed, 30 insertions, 12 deletions
diff --git a/demos/generate-parser.c b/demos/generate-parser.c
index fc022be..8d50ad6 100644
--- a/demos/generate-parser.c
+++ b/demos/generate-parser.c
@@ -16,20 +16,19 @@ size_t total_productions;
#include "util-tables.c"
-// _LAZY_LALR on clr does not work??
-#include "slr-table.c"
+#define _LAZY_LALR
+#include "clr-table.c"
void *xdlsym(void *handle, char *sym)
{
void *r = dlsym(handle, sym);
if(!r) {
- fprintf(stderr, "ERROR: Symbol \"%s\" was not found\n", sym);
+ fprintf(stderr, "ERROR: Symbol '%s' was not found\n", sym);
exit(1);
}
return r;
}
-
#define GET_VARIABLE(var, handle) \
var = *(typeof(&var))xdlsym(handle, #var)
@@ -46,12 +45,11 @@ int main(int argc, char **argv)
GET_VARIABLE(symbol_is_valid, handle);
GET_VARIABLE(grammar, handle);
GET_VARIABLE(total_productions, handle);
-
util_tables_fill();
- table_fill() && (exit(1), 1);
+ table_fill() && (fprintf(stderr, "ERROR: Table couldn't be generated\n"), exit(1), 1);
printf("size_t total_symbols = %zu;\n", total_symbols);
- printf("IMPLEMENT_FUNCPTR(int, symbol_is_valid, (symbol s), {return s >= 0 && s < total_symbols;})\n");
+ printf("IMPLEMENT_FUNCPTR(int, symbol_is_valid, (symbol s), {return s < total_symbols;})\n");
printf("struct production _grammar[] = {\n");
grammar_print_cstyle();
diff --git a/demos/sample-files/parser-skeleton.c b/demos/sample-files/parser-skeleton.c
index facbc1b..031d829 100644
--- a/demos/sample-files/parser-skeleton.c
+++ b/demos/sample-files/parser-skeleton.c
@@ -18,13 +18,33 @@ enum symbol {
SYMBOLS_END,
};
-static symbol toklist[] = {N0, PLUS, N1, END_INPUT};
-static symbol *tok = toklist;
+static inline symbol char_to_token(char c)
+{
+ switch(c) {
+ case '+': return PLUS;
+ case '-': return MINUS;
+ case '(': return LPAREN;
+ case ')': return RPAREN;
+ case '0': return N0;
+ case '1': return N1;
+ case 0 : return END_INPUT;
+ default: fprintf(stderr, "ERROR: Unknown character '%c'\n", c); exit(1);
+ }
+}
-symbol toklist_eat() { return *(tok++); } // unsafe
-symbol toklist_peek() { return *tok; } // unsafe
+static char *input;
-int main(void)
+symbol toklist_eat() { return char_to_token(*(input++)); } // unsafe
+symbol toklist_peek() { return char_to_token(*input); } // unsafe
+
+int main(int argc, char **argv)
{
+ if(argc != 2) {
+ fprintf(stderr, "ERROR: Not enough arguments\n");
+ return 1;
+ }
+
+ input = argv[1];
+
return lr_parser();
}