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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
// TODO: - check erros and fail safely and
// see connection with the lexer
// - standardize this
// Requirements
#include "parts/symbol.h"
#include "parts/grammar.h"
#include "parts/table.h"
#include "parts/toklist.h"
// and
typedef intptr_t (*semantic_action_fn)(intmax_t *stack_head);
extern semantic_action_fn *semantic_actions;
typedef intmax_t stack_item;
#define STACK_CAP 128
static stack_item stack_bottom[STACK_CAP];
static stack_item *stack_head = stack_bottom;
static void print_stack()
{
fprintf(stderr, "STACK: { ");
for(stack_item *s = stack_bottom+1; s <= stack_head; s += 3)
fprintf(stderr, "%s ", symbol_to_str[*(symbol *)s]);
fprintf(stderr, "}\n\n");
}
int lr_parser(intptr_t *value)
{
#define push(item) do { \
if(++stack_head - stack_bottom < STACK_CAP ) *stack_head = item; \
else { fprintf(stderr, "ERROR: STACK_CAP exceeded\n"); print_stack(); return 1; } \
} while(0)
#define pop() (--stack_head)
#define eat() toklist_eat()
#define peek() toklist_peek()
while(1) {
struct action a = table[(size_t)*stack_head][token_sym(peek())];
switch(a.type) {
case ACTION_SHIFT:;
struct token *t = eat();
push(token_sym(t));
push(token_val(t));
push(a.arg);
#ifdef _LR_PARSER_DEBUG
fprintf(stderr, "SHIFT %s\n", symbol_to_str[token_sym(t)]);
print_stack();
#endif
break;
case ACTION_REDUCE:
intptr_t semantic_value = semantic_actions[a.arg](stack_head);
for(size_t i = 0; i < 3*grammar[a.arg].nRHS; i++) pop();
symbol lhs = grammar[a.arg].LHS;
struct action a_goto = table[(size_t)*stack_head][lhs];
if(a_goto.type != ACTION_GOTO) {
fprintf(stderr,
"ERROR: Expected goto action for token '%d' at state %zu\n",
lhs, (size_t)*stack_head);
return 1;
}
push(lhs);
push(semantic_value);
push(a_goto.arg);
#ifdef _LR_PARSER_DEBUG
fprintf(stderr, "READUCE %s\n", symbol_to_str[lhs]);
#endif
break;
case ACTION_ACCEPT:
for(size_t i = 0; i < 3; i++) push(0); // todo: better fix for reducing the final production expecting an END_INPUT on the stack
*value = semantic_actions[0](stack_head);
return 0;
case ACTION_NOT_SET:
default:
fprintf(stderr,
"ERROR: Unexpected symbol '%s' at state %zu\n",
symbol_to_str[token_sym(peek())], (size_t)*stack_head);
// Expected ...
print_stack();
return 1;
}
}
return 1;
}
#ifdef _LR_PARSER_STANDALONE
// implement 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_valid, (symbol s)) { return s < SYMBOLS_END; }
// implement 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),
};
int *precedence_symbol = (int[]){0};
int *precedence_production = (int[]){0};
struct production *grammar = _grammar;
size_t total_productions = sizeof(_grammar)/sizeof(*_grammar);
// implement table.h
struct action **table = (struct action *([])){
(struct action[]){{0, 0},{0, 0},{1, 1},{0, 0},{1, 2},{1, 3},{0, 0},{0, 0},{2, 12},{2, 11},{2, 7},},
(struct action[]){{0, 0},{0, 0},{1, 1},{0, 0},{1, 2},{1, 3},{0, 0},{0, 0},{2, 4},{2, 11},{2, 7},},
(struct action[]){{3, 6},{3, 6},{0, 0},{3, 6},{0, 0},{0, 0},{3, 6},{0, 0},{0, 0},{0, 0},{0, 0},},
(struct action[]){{3, 7},{3, 7},{0, 0},{3, 7},{0, 0},{0, 0},{3, 7},{0, 0},{0, 0},{0, 0},{0, 0},},
(struct action[]){{1, 5},{1, 8},{0, 0},{1, 10},{0, 0},{0, 0},{0, 0},{0, 0},{0, 0},{0, 0},{0, 0},},
(struct action[]){{0, 0},{0, 0},{1, 1},{0, 0},{1, 2},{1, 3},{0, 0},{0, 0},{0, 0},{2, 6},{2, 7},},
(struct action[]){{3, 1},{3, 1},{0, 0},{3, 1},{0, 0},{0, 0},{3, 1},{0, 0},{0, 0},{0, 0},{0, 0},},
(struct action[]){{3, 5},{3, 5},{0, 0},{3, 5},{0, 0},{0, 0},{3, 5},{0, 0},{0, 0},{0, 0},{0, 0},},
(struct action[]){{0, 0},{0, 0},{1, 1},{0, 0},{1, 2},{1, 3},{0, 0},{0, 0},{0, 0},{2, 9},{2, 7},},
(struct action[]){{3, 2},{3, 2},{0, 0},{3, 2},{0, 0},{0, 0},{3, 2},{0, 0},{0, 0},{0, 0},{0, 0},},
(struct action[]){{3, 4},{3, 4},{0, 0},{3, 4},{0, 0},{0, 0},{3, 4},{0, 0},{0, 0},{0, 0},{0, 0},},
(struct action[]){{3, 3},{3, 3},{0, 0},{3, 3},{0, 0},{0, 0},{3, 3},{0, 0},{0, 0},{0, 0},{0, 0},},
(struct action[]){{1, 5},{1, 8},{0, 0},{0, 0},{0, 0},{0, 0},{4, 0},{0, 0},{0, 0},{0, 0},{0, 0},},
};
size_t table_states = 13;
// implement toklist.h
struct token {
symbol s;
int v;
};
static struct token toklist[] = {{N0, 0}, {PLUS, 0}, {N1, 0}, {END_INPUT, 0}};
static const size_t ntoklist = sizeof(toklist)/sizeof(*toklist);
static size_t tok;
struct token *toklist_eat()
{
if(tok == ntoklist) return toklist + ntoklist-1;
return toklist + tok++;
}
struct token *toklist_peek() { return toklist + tok; }
symbol token_sym(struct token *t) { return t->s; }
int token_val(struct token *t) { return t->v; }
intptr_t none(intmax_t *stack_head) {(void)stack_head; return 0;}
semantic_action_fn *semantic_actions = (semantic_action_fn[]){none, none, none, none, none, none, none, none};
int main(void)
{
intptr_t value;
if(lr_parser(&value)) return 1;
printf("%jd\n", value);
return 0;
}
#endif
|