aboutsummaryrefslogtreecommitdiff
path: root/demos/sample-files/arithmetic-skeleton.c
blob: ef5ec2f107cdbe762f2f3bf555068cc3a03aac52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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;
}