aboutsummaryrefslogtreecommitdiff
path: root/demos/sample-files
diff options
context:
space:
mode:
Diffstat (limited to 'demos/sample-files')
-rw-r--r--demos/sample-files/arithmetic-defs.c49
-rw-r--r--demos/sample-files/arithmetic-skeleton.c62
-rw-r--r--demos/sample-files/calc-defs.c40
-rw-r--r--demos/sample-files/calc-skeleton.c12
4 files changed, 32 insertions, 131 deletions
diff --git a/demos/sample-files/arithmetic-defs.c b/demos/sample-files/arithmetic-defs.c
deleted file mode 100644
index 9a0db22..0000000
--- a/demos/sample-files/arithmetic-defs.c
+++ /dev/null
@@ -1,49 +0,0 @@
-#include <stdio.h>
-#include <stddef.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 "???.h"
-char **semantic_action_str = (char *([])){
- "v = A(0);",
- "v = A(0) + A(2);",
- "v = A(0) - A(2);",
- "v = A(0);",
- "v = A(1);",
- "v = A(0);",
- "v = 0;",
- "v = 1;",
-};
diff --git a/demos/sample-files/arithmetic-skeleton.c b/demos/sample-files/arithmetic-skeleton.c
deleted file mode 100644
index ef5ec2f..0000000
--- a/demos/sample-files/arithmetic-skeleton.c
+++ /dev/null
@@ -1,62 +0,0 @@
-#include <stdio.h>ae
-#include <stdlib.h>
-
-#include "lr-parser.c"
-#include "bin/generated.c"
-
-#include "parts/toklist.h"
-
-enum symbol {
- PLUS = 0,
- MINUS,
- LPAREN,
- RPAREN,
- N0, N1,
- END_INPUT,
-
- EP, E, T, N,
- SYMBOLS_END,
-};
-
-struct token {
- symbol s;
-};
-
-static inline struct token *char_to_token(char c)
-{
- static struct token t;
-
- switch(c) {
- case '+': t = (struct token){PLUS}; break;
- case '-': t = (struct token){MINUS}; break;
- case '(': t = (struct token){LPAREN}; break;
- case ')': t = (struct token){RPAREN}; break;
- case '0': t = (struct token){N0}; break;
- case '1': t = (struct token){N1}; break;
- case 0 : t = (struct token){END_INPUT}; break;
- default: fprintf(stderr, "ERROR: Unknown character '%c'\n", c); exit(1);
- }
-
- return &t;
-}
-
-static char *input;
-
-symbol token_sym(struct token *t) { return t->s; }
-int token_val(struct token *t) { return 0; }
-struct token *toklist_eat() { return char_to_token(*(input++)); } // unsafe
-struct token *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];
-
- printf("INPUT: '%s'\n", input);
- printf("OUTPUT: %d\n", lr_parser());
- return 0;
-}
diff --git a/demos/sample-files/calc-defs.c b/demos/sample-files/calc-defs.c
index 656ab1c..7321a88 100644
--- a/demos/sample-files/calc-defs.c
+++ b/demos/sample-files/calc-defs.c
@@ -2,12 +2,9 @@
#include "parts/symbol.h"
enum symbol {
- PLUS = 0,
- MINUS,
- LPAREN,
- RPAREN,
- NUM,
- END_INPUT,
+ PLUS, MINUS, TIMES,
+ LPAREN, RPAREN,
+ NUM, END_INPUT,
EP, E, T,
SYMBOLS_END,
@@ -20,14 +17,14 @@ 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), 0}
+#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, -->, NUM),
+ PROD(E, -->, E, PLUS, E),
+ PROD(E, -->, E, MINUS, E),
+ PROD(E, -->, E, TIMES, E),
+ PROD(E, -->, LPAREN, E, RPAREN),
+ PROD(E, -->, NUM),
};
struct production *grammar = _grammar;
@@ -38,7 +35,24 @@ char **semantic_action_str = (char *([])){
"v = A(0);",
"v = A(0) + A(2);",
"v = A(0) - A(2);",
- "v = A(0);",
+ "v = A(0) * A(2);",
"v = A(1);",
"v = A(0);",
};
+
+#include "parts/precedence.h"
+
+struct precedence_def {
+ int flag;
+ int *list;
+ size_t nlist;
+};
+#define PREC(f, ...) {f, (int[]){__VA_ARGS__}, sizeof((int[]){__VA_ARGS__})/sizeof(int)}
+struct precedence_def _precedence_defs[] = {
+ PREC(PRECEDENCE_LEFT_ASSOC, MINUS, PLUS),
+ PREC(PRECEDENCE_LEFT_ASSOC, TIMES),
+ PREC(PRECEDENCE_LEFT_ASSOC, LPAREN, RPAREN),
+};
+
+struct precedence_def *precedence_defs = _precedence_defs;
+size_t nprecedence_defs = sizeof(_precedence_defs)/sizeof(*_precedence_defs);
diff --git a/demos/sample-files/calc-skeleton.c b/demos/sample-files/calc-skeleton.c
index 2108999..b0cbf00 100644
--- a/demos/sample-files/calc-skeleton.c
+++ b/demos/sample-files/calc-skeleton.c
@@ -9,12 +9,9 @@
// header file by the parser generator
#include "parts/symbol.h"
enum symbol {
- PLUS = 0,
- MINUS,
- LPAREN,
- RPAREN,
- NUM,
- END_INPUT,
+ PLUS, MINUS, TIMES,
+ LPAREN, RPAREN,
+ NUM, END_INPUT,
EP, E, T,
SYMBOLS_END,
@@ -27,7 +24,7 @@ static struct token {
static inline int issep(char c)
{
- return isspace(c) || c == '\0' || c == '(' || c == ')' || c == '+' || c == '-';
+ return isspace(c) || c == '\0' || c == '(' || c == ')' || c == '+' || c == '-' || c == '*';;
}
static inline int tillsep(char *str)
@@ -61,6 +58,7 @@ static char *next_token(char *str)
case ')': tok.s = RPAREN; break;
case '-': tok.s = MINUS; break;
case '+': tok.s = PLUS; break;
+ case '*': tok.s = TIMES; break;
}
} else if(c0 >= '0' && c0 <= '9') { // num
tok.s = NUM;