-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
253 lines (211 loc) · 4.49 KB
/
test.c
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <stdint.h>
#include <assert.h>
#include "lex.h"
#include "grammar.h"
#if 1
#define dprintf(...) printf(__VA_ARGS__)
#else
#define dprintf(x...) do { } while(0);
#endif
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#endif
#define STACK_INCREMENT 32
#define PARSE_ERR(_tok, fmt, x...) \
fprintf(stderr, "Parse Error: %s:%u: " fmt, \
(_tok)->t_file, (_tok)->t_line, ##x)
#define PARSE_WARN(_tok, fmt, x...) \
fprintf(stderr, "Warning: %s:%u: " fmt, \
(_tok)->t_file, (_tok)->t_line, ##x)
struct _parser {
unsigned int *stack;
unsigned int stack_top;
unsigned int stack_max;
};
static const struct action *action_lookup(unsigned int s,
int a)
{
unsigned int i;
for(i = 0; i < ARRAY_SIZE(ACTION); i++) {
if ( ACTION[i].i == s && ACTION[i].a == a) {
return &ACTION[i];
}
}
return NULL;
}
static const int goto_lookup(unsigned int s, int A, unsigned int *res)
{
unsigned int i;
for(i = 0; i < ARRAY_SIZE(GOTO); i++) {
if ( GOTO[i].i == s && GOTO[i].A == A) {
dprintf(" - GOTO[%u,%s] = %u\n",
s, sym_name(A), GOTO[i].j);
*res = GOTO[i].j;
return 1;
}
}
return 0;
}
static int stack_push(struct _parser *p, unsigned int state)
{
if ( p->stack_top >= p->stack_max ) {
void *new;
size_t sz;
sz = (p->stack_max + STACK_INCREMENT) * sizeof(int);
new = realloc(p->stack, sz);
if ( NULL == new ) {
fprintf(stderr, "%s\n", strerror(errno));
return 0;
}
p->stack = new;
p->stack_max += STACK_INCREMENT;
}
p->stack[p->stack_top++] = state;
return 1;
}
static unsigned int stack_pop(struct _parser *p)
{
assert(p->stack_top);
return p->stack[--p->stack_top];
}
static unsigned int stack_top(struct _parser *p)
{
assert(p->stack_top);
return p->stack[p->stack_top - 1];
}
static int parser_token(struct _parser *p, tok_t tok)
{
const struct production *prod;
const struct action *a;
unsigned int i, j;
printf("token: %s\n", sym_name(tok->t_type));
again:
dprintf("Lookup ACTION[%u,%s]\n", stack_top(p), sym_name(tok->t_type));
a = action_lookup(stack_top(p), tok->t_type);
if ( NULL == a ) {
printf("Parse error\n");
return 0;
}
switch(a->action) {
case ACTION_ACCEPT:
dprintf("accept\n");
break;
case ACTION_SHIFT:
dprintf("shift\n");
stack_push(p, a->u.shift.t);
dprintf(" - state now %u\n", stack_top(p));
if ( tok->t_type == SYM_EOF ) {
goto again;
}
break;
case ACTION_REDUCE:
prod = &productions[a->u.reduce.index];
printf(" - reduce: %u\n", prod->action);
dprintf("reduce (len %u)\n", prod->len);
for(i = 0; i < prod->len; i++) {
dprintf(" - pop %u\n", stack_top(p));
stack_pop(p);
}
dprintf(" - state now %u\n", stack_top(p));
if ( !goto_lookup(stack_top(p), prod->head, &j) ) {
return 0;
}
stack_push(p, j);
dprintf(" - state now %u\n", stack_top(p));
goto again;
}
printf("\n");
return 1;
}
static int parser_eof(struct _parser *p)
{
struct _tok tok;
tok.t_type = SYM_EOF;
if ( !parser_token(p, &tok) ) {
return 0;
}
return 1;
}
static struct _parser *parser_new(void)
{
struct _parser *p;
p = calloc(1, sizeof(*p));
if ( NULL == p ) {
fprintf(stderr, "%s\n", strerror(errno));
goto out;
}
if ( !stack_push(p, INITIAL_STATE) )
goto out_free;
/* success */
goto out;
out_free:
free(p);
p = NULL;
out:
return p;
}
static int tok_cb(tok_t tok, void *priv)
{
struct _parser *p = priv;
return parser_token(p, tok);
}
static int lex_file(const char *fn)
{
struct _parser *p;
lexer_t lex;
char buf[8192];
FILE *f;
size_t sz;
int ret = 0;
printf("opening %s\n", fn);
p = parser_new();
if ( NULL == p )
goto out;
lex = lexer_new(fn, tok_cb, p);
if ( NULL == lex )
goto out_free_parser;
f = fopen(fn, "r");
if ( NULL == f ) {
fprintf(stderr, "open: %s: %s\n", fn, strerror(errno));
goto out_free;
}
while((sz = fread(buf, 1, sizeof(buf), f))) {
if (!lexer_feed(lex, buf, sz)) {
goto out_close;
}
}
if ( ferror(f) ) {
fprintf(stderr, "parse: %s: %s\n", fn, strerror(errno));
goto out_close;
}
if ( !lexer_eof(lex) || !parser_eof(p) ) {
fprintf(stderr, "parse: %s: parse/lex error\n", fn);
goto out_close;
}
/* success */
printf("OK\n");
ret = 1;
out_close:
fclose(f);
out_free:
lexer_free(lex);
out_free_parser:
out:
return ret;
}
int main(int argc, char **argv)
{
int i;
printf("Start:\n\n");
for(i = 1; i < argc; i++) {
if ( !lex_file(argv[i]) )
printf("%s: parse: fucked\n", argv[i]);
}
return 0;
}