aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkartofen <kartofen.mail.0@protonmail.com>2025-10-08 20:21:40 +0300
committerkartofen <kartofen.mail.0@protonmail.com>2025-10-08 20:21:40 +0300
commitbcd3391e98511f5913149b87eb8617aaaa4beb6b (patch)
treee21a9be998a2ae9a4007b7c631c64dcb5f9b7277
parentadcadb01becb36c79d881080d399328de051e301 (diff)
edit README and build extend ninja.m4
-rw-r--r--README.md12
-rw-r--r--demos/instant-parser.c79
-rw-r--r--demos/sample-files/calc-skeleton.c5
-rw-r--r--ninja.m429
4 files changed, 35 insertions, 90 deletions
diff --git a/README.md b/README.md
index 9e853ba..417c644 100644
--- a/README.md
+++ b/README.md
@@ -13,23 +13,21 @@ techniques and ways to add semanitic meaning.
### TODO
-#### Goals
-
-- The LR parser implementation is very dirty and bad
- The building process is too compilated, the grammar parser
should also do the table generation
-- Proper LALR generation
+- Proper LALR generation (without generating all CLR states)
- LL table generation and parsing
- Possibly recursive ascent and recursive descent generation (a bit pointless)
- Left corner / resursive ascent-descent generation (Horspool)
+
- Proper attribute grammar implementation, evaluation, and dealing with cycles
- (S)GLR - Scannerless Generalized LR (Masaru Tomita)
- It would be good to implemented a compiler of C language (C, B, BCPL),
and a high level language, maybe Prolog or something mine and with all
- beingfairly optimized
+ being fairly optimized
### Buildling
-The build script couldn't be more straight forward, use just ```./build.sh```
-and uncomment the lines for the give use
+It is done using Ninja and M4, use `ninja` to build (and run).
+See `ninja.m4` for more information
diff --git a/demos/instant-parser.c b/demos/instant-parser.c
deleted file mode 100644
index 8af2318..0000000
--- a/demos/instant-parser.c
+++ /dev/null
@@ -1,79 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "parts/symbol.h"
-enum symbol {
- PLUS = 0,
- MINUS,
- LPAREN,
- RPAREN,
- N0, N1,
- END_INPUT,
-
- EP, E, T, N,
- SYMBOLS_END,
-};
-
-size_t total_symbols = SYMBOLS_END;
-
-IMPLEMENT_FUNCPTR(int, symbol_is_terminal, (symbol s)) { return s < EP; }
-IMPLEMENT_FUNCPTR(int, symbol_is_input_end, (symbol s)) { return s == END_INPUT; }
-IMPLEMENT_FUNCPTR(int, symbol_is_valid,(symbol s)) { return s < SYMBOLS_END; }
-
-#include "parts/grammar.h"
-#define PROD(LHS, _, ...) {LHS, (symbol[]){__VA_ARGS__}, sizeof((symbol[]){__VA_ARGS__})/sizeof(symbol)}
-static struct production _grammar[] = {
- PROD(EP, ->, E, END_INPUT),
- PROD(E, -->, E, PLUS, T),
- PROD(E, -->, E, MINUS, T),
- PROD(E, -->, T),
- PROD(T, -->, LPAREN, E, RPAREN),
- PROD(T, -->, N),
- PROD(N, -->, N0),
- PROD(N, -->, N1),
-};
-
-struct production *grammar = _grammar;
-size_t total_productions = sizeof(_grammar)/sizeof(*_grammar);
-
-#include "parts/toklist.h"
-static symbol toklist[] = {N0, PLUS, N1, MINUS, N0, END_INPUT};
-static symbol *tok = toklist;
-
-symbol toklist_eat() { return *(tok++); } // unsafe
-symbol toklist_peek() { return *tok; } // unsafe
-
-int __prod0_action(int *stack_head) { return *(stack_head-4); }
-int __prod1_action(int *stack_head) { return *(stack_head-7) + *(stack_head-1); }
-int __prod2_action(int *stack_head) { return *(stack_head-7) + *(stack_head-1); }
-int __prod3_action(int *stack_head) { return *(stack_head - 1); }
-int __prod4_action(int *stack_head) { return *(stack_head - 4); }
-int __prod5_action(int *stack_head) { return *(stack_head - 1); }
-int __prod6_action(int *stack_head) { return 0; }
-int __prod7_action(int *stack_head) { return 1; }
-
-typedef int (*semantic_action_fn)(int *stack_head);
-semantic_action_fn *semantic_actions = (semantic_action_fn[]){
- __prod0_action, __prod1_action, __prod2_action, __prod3_action,
- __prod4_action, __prod5_action, __prod6_action, __prod7_action,
-};
-
-#include "slr-table.c"
-#include "util-tables.c"
-#include "lr-parser.c"
-
-int main(void)
-{
- util_tables_fill();
- int r = 0;
- if((r = table_fill())) goto cleanup;
-
- table_print();
- printf("RESULT: %d\n", lr_parser());
-
-cleanup:
- table_free();
- util_tables_free();
-
- return r;
-}
diff --git a/demos/sample-files/calc-skeleton.c b/demos/sample-files/calc-skeleton.c
index 414293e..a5cd3cc 100644
--- a/demos/sample-files/calc-skeleton.c
+++ b/demos/sample-files/calc-skeleton.c
@@ -40,8 +40,9 @@ int main(int argc, char **argv)
input = next_token(argv[1]);
- int value;
- if(lr_parser(&value)) return 1;
+ struct lr_parseinfo parseinfo;
+ int value = *(intptr_t *)lr_parser(&parseinfo);
+ if(parseinfo.type) return 1;
printf("INPUT: '%s'\n", argv[1]);
printf("OUTPUT: %d\n", value);
diff --git a/ninja.m4 b/ninja.m4
index 62f3aa2..7a22ba0 100644
--- a/ninja.m4
+++ b/ninja.m4
@@ -1,5 +1,12 @@
divert(-1)
changequote([,])
+
+define([map], [ifelse([$2],,, [dnl
+$1($2)[]map([$1], shift(shift($@)))dnl
+])])
+
+define([first], [$1 ])
+
divert(0)
# rules
@@ -16,6 +23,14 @@ rule shared
command = gcc -MD -MF $out.d $cflags -shared -fPIC $in -o $out
rule m4
command = m4 $m4flags $in > $out
+rule leak
+ command = $leak $in
+
+define([sbuild], [
+build $bin/$1: cc $2
+ cflags = $cflags $3
+build $1: leak $bin/$1
+])
build $bin/generate-parser: cc demos/generate-parser.c
cflags = $cflags -rdynamic
@@ -55,18 +70,28 @@ rule lbp_parse
command = $leak $bin/lbp-parser < $in
build lbp_test: lbp_parse demos/sample-files/lbp-code.lbp | lbp
-gbuild(from-gram, calc, demos/sample-files, lalr-table) # skeleton doesn't work
+gbuild(from-gram, calc, demos/sample-files, lalr-table)
rule calc_parse
command = $leak $bin/calc-parser 5+3
build calc_test: calc_parse | calc
+define([standalone_table], [
+[util-tables, util-tables.c, -D_UTIL_TABLES_STANDALONE],
+[slr-table, slr-table.c, -D_SLR_TABLE_STANDALONE],
+[clr-table, clr-table.c, -D_CLR_TABLE_STANDALONE],
+[lalr-table, clr-table.c, -D_CLR_TABLE_STANDALONE -D_LAZY_LALR],
+[lr-parser, lr-parser.c, -D_LR_PARSER_STANDALONE]])
+
+map([sbuild], standalone_table)
+
# targets
rule ninja_clean
command = ninja -t clean
build clean: ninja_clean
-default lbp_test
+# default map([first], standalone_table)
+default calc_test
# regeneration