diff options
| author | kartofen <kartofen.mail.0@protonmail.com> | 2025-07-20 01:32:24 +0300 |
|---|---|---|
| committer | kartofen <kartofen.mail.0@protonmail.com> | 2025-07-20 01:32:24 +0300 |
| commit | 34357640c0676f33ad13aac1fe28effc6f6e47c7 (patch) | |
| tree | d656ee61da7d7a0b133aa57311266653ef100569 /demos/sample-files/gram-defs.c | |
| parent | 174e9b35ce3b6e99e500907f1bb24c6f31f481bf (diff) | |
start of grammar parsing
Diffstat (limited to 'demos/sample-files/gram-defs.c')
| -rw-r--r-- | demos/sample-files/gram-defs.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/demos/sample-files/gram-defs.c b/demos/sample-files/gram-defs.c new file mode 100644 index 0000000..733a866 --- /dev/null +++ b/demos/sample-files/gram-defs.c @@ -0,0 +1,65 @@ +#include "util/util.h" +#define SYMBOLS(X) \ + X(COLON) X(PIPE) X(SEMICOL) X(DOT) \ + X(D_LEFT) X(D_RIGHT) X(D_TERMINAL) X(D_NONTERM) \ + X(IDEN) X(NUM) X(ACTION) X(END_INPUT) \ + \ + X(Sp) X(S) X(Slist) X(Prod) X(Prec) \ + X(Prodlist) X(Idenlist) X(IorN) X(IorNlist) \ + X(SYMBOLS_END) \ + +#include "parts/symbol.h" +enum symbol { SYMBOLS(X_TO_ENUM) }; +size_t total_symbols = SYMBOLS_END; + +char **symbol_to_str = (char *([])){ SYMBOLS(X_TO_STR) }; + +IMPLEMENT_FUNCPTR(int, symbol_is_terminal, (symbol s)) { return s < Sp; } +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)} +#define GRAMMAR_ACTION_DEF(X) \ + X(PROD(Sp, ->, Slist, END_INPUT), "v = 0;") \ + X(PROD(Slist, -->, S, SEMICOL, Slist), "v = 0;") \ + X(PROD(Slist, -->, S, DOT), "v = 0;") \ + X(PROD(S, -->, Prod), "v = 0;") \ + X(PROD(S, -->, Prec), "v = 0;") \ + X(PROD(Idenlist, -->, IDEN, Idenlist), "v = 0;") \ + X(PROD(Idenlist, -->, IDEN), "v = 0;") \ + X(PROD(Prod, -->, IDEN, COLON, Prodlist), "v = 0;") \ + X(PROD(Prodlist, -->, Idenlist, ACTION, PIPE, Prodlist), "printf(\"ACTION: '%s'\\n\", A(1));") \ + X(PROD(Prodlist, -->, Idenlist, ACTION), "printf(\"ACTION: '%s'\\n\", A(1));") \ + X(PROD(Prec, -->, D_TERMINAL, Idenlist), "v = 0;") \ + X(PROD(Prec, -->, D_NONTERM, Idenlist), "v = 0;") \ + X(PROD(Prec, -->, D_LEFT, IorNlist), "v = 0;") \ + X(PROD(Prec, -->, D_RIGHT, IorNlist), "v = 0;") \ + X(PROD(IorNlist, -->, IorN, IorNlist), "v = 0;") \ + X(PROD(IorNlist, -->, IorN), "v = 0;") \ + X(PROD(IorN, -->, IDEN), "v = 0;") \ + X(PROD(IorN, -->, NUM), "v = 0;") + +#define X_GRAMMAR(G, A) G, +#define X_ACTION(G, A) A, + +static struct production _grammar[] = { + GRAMMAR_ACTION_DEF(X_GRAMMAR) +}; + +struct production *grammar = _grammar; +size_t total_productions = sizeof(_grammar) / sizeof(*_grammar); + +// #include "???.h" +char **semantic_action_str = (char *([])){ + GRAMMAR_ACTION_DEF(X_ACTION) +}; + +#include "parts/precedence.h" +struct precedence_def { + int flag; + int *list; + size_t nlist; +}; +struct precedence_def *precedence_defs = NULL; +size_t nprecedence_defs = 0; |
