diff options
author | kartofen <mladenovnasko0@gmail.com> | 2025-07-08 18:25:29 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2025-07-08 18:25:29 +0300 |
commit | d028cc9c04cf46256166434bdea68d5f5c6d310f (patch) | |
tree | 28420104516e446a06172687eae16e3bf4dfd5bd /demos/sample-files/arithmetic-skeleton.c | |
parent | 919611902c39fd70afe1162883ee6bfd34f2642e (diff) |
simple calculator example
Diffstat (limited to 'demos/sample-files/arithmetic-skeleton.c')
-rw-r--r-- | demos/sample-files/arithmetic-skeleton.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/demos/sample-files/arithmetic-skeleton.c b/demos/sample-files/arithmetic-skeleton.c new file mode 100644 index 0000000..ef5ec2f --- /dev/null +++ b/demos/sample-files/arithmetic-skeleton.c @@ -0,0 +1,62 @@ +#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; +} |