aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md29
-rw-r--r--clr-table.c2
-rw-r--r--demos/instant-parser.c23
-rw-r--r--parts/symbol.h2
4 files changed, 50 insertions, 6 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..f1efbff
--- /dev/null
+++ b/README.md
@@ -0,0 +1,29 @@
+### Parser things
+
+This repo has simple implementations of concepts seen in parsing
+and compiler design, written in a way to be easily integrated and
+combined while each file/concept has a standalone example.
+
+The main example if this is a simple compiler generator where the
+table to be generated (lalr, clr, slr) and the definitions are loaded
+as shared libraries.
+
+The idea is to extend it to many types of table generation, parsing
+techniques and ways to add semanitic meaning.
+
+### TODO
+
+- Proper LALR generation
+- 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
+
+### Buildling
+
+The build script couldn't be more straight forward, use just ```./build.sh```
+and uncomment the lines for the give use
diff --git a/clr-table.c b/clr-table.c
index ca23799..001a45c 100644
--- a/clr-table.c
+++ b/clr-table.c
@@ -90,7 +90,7 @@ static size_t itemset_handle(struct item *set, size_t nset)
if(!item_core_eq(&seen_sets[i].items[k], &set[j])) _same_core = 0;
if(!_same_core) break;
}
- if(_same_core) use_state = seen_sets[i].state;
+ if(_same_core) { use_state = seen_sets[i].state; break; }
#endif
}
diff --git a/demos/instant-parser.c b/demos/instant-parser.c
index 06990f6..8af2318 100644
--- a/demos/instant-parser.c
+++ b/demos/instant-parser.c
@@ -43,6 +43,21 @@ 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"
@@ -50,13 +65,13 @@ symbol toklist_peek() { return *tok; } // unsafe
int main(void)
{
util_tables_fill();
- table_fill();
+ int r = 0;
+ if((r = table_fill())) goto cleanup;
table_print();
+ printf("RESULT: %d\n", lr_parser());
- int r = 0;
- r = lr_parser();
-
+cleanup:
table_free();
util_tables_free();
diff --git a/parts/symbol.h b/parts/symbol.h
index dea457e..e5e45d3 100644
--- a/parts/symbol.h
+++ b/parts/symbol.h
@@ -3,7 +3,7 @@
typedef unsigned int symbol;
extern size_t total_symbols;
-// extern char *symbol_to_str[] ...
+extern char **symbol_to_str;
extern int (*symbol_is_terminal)(symbol s);
extern int (*symbol_is_input_end)(symbol s);