-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.c
567 lines (502 loc) · 11.4 KB
/
parser.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
#include "gc.h"
/*
* Parser (tokens -> tree)
*
* (this code was copied and adapted from
* my other project, the wannabe compiler)
*
* BUGS: seems to get into infinite recursions
* or segfaults on certain occurences of
* invalid syntax
*
* The above has been addressed to some extent
* on 2014-04-24.
*/
#include <stdio.h>
#include <stdlib.h>
#include "tokenizer.h"
#include "tokens.h"
#include "tree.h"
#include "diagnostics.h"
#include <string.h>
token_t *tokens;
int indx;
int tok_count;
exp_tree_t block();
exp_tree_t expr();
exp_tree_t pow_expr(int);
exp_tree_t sum_expr();
exp_tree_t mul_expr();
exp_tree_t unary_expr(int);
exp_tree_t signed_expr();
void printout(exp_tree_t et);
extern void fail(char* mesg);
/* Give the current token */
token_t peek()
{
if (indx >= tok_count) {
return tokens[tok_count - 1];
}
else
return tokens[indx];
}
/*
* Generate a pretty error when parsing fails.
* this is a hack-wrapper around the routine
* in diagnostics.c that deals with certain
* special cases special to the parser.
*/
void parse_fail(char *message)
{
token_t tok;
int line, chr;
if (tokens[indx].from_line
!= tokens[indx - 1].from_line) {
/* end of the line */
line = tokens[indx - 1].from_line;
chr = strstr(code_lines[line], "\n")
- code_lines[line] + 1;
} else {
line = tokens[indx].from_line;
chr = tokens[indx].from_char;
}
compiler_fail(message, NULL, line, chr);
}
/*
* Fail if the next token isn't of the desired type.
* Otherwise, return it and advance.
*/
token_t need(char type)
{
char buf[1024];
if (tokens[indx++].type != type) {
fflush(stdout);
--indx;
sprintf(buf, "%s expected", tok_nam[type]);
parse_fail(buf);
} else
return tokens[indx - 1];
}
/* main parsing loop */
exp_tree_t parse(token_t *t)
{
exp_tree_t program;
exp_tree_t *subtree;
program = new_exp_tree(BLOCK, NULL);
int i;
/* count the tokens */
for (i = 0; t[i].start; ++i)
;
tok_count = i;
/* the loop */
tokens = t;
indx = 0;
while (tokens[indx].start) {
subtree = alloc_exptree(block());
/* parse fails at EOF */
if (!valid_tree(*subtree))
break;
add_child(&program, subtree);
}
return program;
}
/*
* Now the real fun starts. Each routine is
* named after the part of the grammar that
* it deals with. When it doesn't find anything
* that it's been trained to find, a routine
* gives back "null_tree".
*/
/*
* variables (any text symbol)
* quoted symbols : ' text
*/
exp_tree_t lval()
{
token_t tok = peek();
token_t tok2;
exp_tree_t tree, subtree;
int cc = 0;
if (tok.type == TOK_NAN) {
++indx; /* eat @ */
tok = peek();
++indx;
return new_exp_tree(MATCH_NAN, &tok);
} else if (tok.type == TOK_MVAR) {
++indx; /* eat ? */
tok = peek();
++indx; /* eat ident */
return new_exp_tree(MATCH_VAR, &tok);
}
else if (tok.type == TOK_MNUM) {
++indx; /* eat # */
tok = peek();
++indx; /* eat int */
return new_exp_tree(MATCH_NUM, &tok);
}
else if (tok.type == TOK_QUOT) {
++indx; /* eat quote */
tok = peek();
++indx; /* eat ident */
return new_exp_tree(SYMBOL, &tok);
}
else if (tok.type == TOK_IDENT) {
++indx; /* eat the identifier */
if (peek().type == TOK_NOTSYMBOL) {
++indx;
tok2 = peek();
++indx;
if (tok2.type != TOK_IDENT)
parse_fail("plain symbol expected");
tree = new_exp_tree(MATCH_IND_SYM, NULL);
subtree = new_exp_tree(VARIABLE, &tok);
add_child(&tree, alloc_exptree(subtree));
subtree = new_exp_tree(VARIABLE, &tok2);
add_child(&tree, alloc_exptree(subtree));
return tree;
} else if (peek().type == TOK_PIPE) {
++indx;
tok2 = peek();
++indx;
if (tok2.type != TOK_IDENT)
parse_fail("plain symbol expected");
tree = new_exp_tree(MATCH_IND, NULL);
subtree = new_exp_tree(VARIABLE, &tok);
add_child(&tree, alloc_exptree(subtree));
subtree = new_exp_tree(VARIABLE, &tok2);
add_child(&tree, alloc_exptree(subtree));
return tree;
} else if (peek().type == TOK_COLON
|| peek().type == TOK_CC) {
cc = peek().type == TOK_CC;
++indx;
tok2 = peek();
++indx;
if (tok2.type != TOK_IDENT)
parse_fail("plain symbol expected");
tree = new_exp_tree(cc ? MATCH_CONTX
: MATCH_CONT, NULL);
subtree = new_exp_tree(VARIABLE, &tok);
add_child(&tree, alloc_exptree(subtree));
subtree = new_exp_tree(VARIABLE, &tok2);
add_child(&tree, alloc_exptree(subtree));
return tree;
} else
return new_exp_tree(VARIABLE, &tok);
}
return null_tree;
}
exp_tree_t block()
{
return expr();
}
/*
expr := sum_expr ['=' sum_expr]
*/
exp_tree_t expr()
{
exp_tree_t tree, subtree, subtree2;
exp_tree_t subtree3;
exp_tree_t lv;
token_t oper;
token_t tok;
int save = indx;
/*
* 2014-04-24
*
* parser segfaulted on "diff(".
*
* knipil says:
* "Make expr() bail out when there's no tokens left,
* and the problem will go away."
*/
if (indx >= tok_count)
return null_tree;
/* sum_expr ['=' sum_expr] */
if (valid_tree(subtree = sum_expr())) {
if (peek().type != TOK_ASGN) {
return subtree;
}
if (peek().type == TOK_ASGN) {
tree = new_exp_tree(EQL, NULL);
++indx; /* eat comp-op */
add_child(&tree, alloc_exptree(subtree));
if (!valid_tree(subtree2 = sum_expr()))
parse_fail("expression expected");
add_child(&tree, alloc_exptree(subtree2));
}
return tree;
}
return null_tree;
}
/* sum_expr := mul_expr { add-op mul_expr } */
exp_tree_t sum_expr()
{
exp_tree_t child, tree, new;
exp_tree_t *child_ptr, *tree_ptr, *new_ptr;
exp_tree_t *root;
int prev;
token_t oper;
if (!valid_tree(child = mul_expr()))
return null_tree;
if (!is_add_op((oper = peek()).type))
return child;
child_ptr = alloc_exptree(child);
switch (oper.type) {
case TOK_PLUS:
tree = new_exp_tree(ADD, NULL);
break;
case TOK_MINUS:
tree = new_exp_tree(SUB, NULL);
break;
}
prev = oper.type;
++indx; /* eat add-op */
tree_ptr = alloc_exptree(tree);
add_child(tree_ptr, child_ptr);
while (1) {
if (!valid_tree(child = mul_expr()))
parse_fail("expression expected");
/* add term as child */
add_child(tree_ptr, alloc_exptree(child));
/* bail out early if no more operators */
if (!is_add_op((oper = peek()).type))
return *tree_ptr;
switch (oper.type) {
case TOK_PLUS:
new = new_exp_tree(ADD, NULL);
break;
case TOK_MINUS:
new = new_exp_tree(SUB, NULL);
break;
}
++indx; /* eat add-op */
new_ptr = alloc_exptree(new);
add_child(new_ptr, tree_ptr);
tree_ptr = new_ptr;
}
return *tree_ptr;
}
/* #define IMPMUL_DEBUG */
/* mul_expr := pow_expr { ( mul-op signed_expr) | (pow_expr) } */
exp_tree_t mul_expr()
{
/* this routine is mostly a repeat of sum_expr() */
exp_tree_t child, child2, tree, new;
exp_tree_t *child_ptr, *tree_ptr, *new_ptr;
exp_tree_t *root;
int prev;
token_t oper;
int impmul = 0;
if (!valid_tree(child = signed_expr()))
return null_tree;
/*
* maybe an implicit multiplication...
*/
if (!is_mul_op((oper = peek()).type)) {
if (indx < tok_count) {
if (valid_tree(child2 = pow_expr(1))) {
tree = new_exp_tree(MULT, NULL);
impmul = 1;
#ifdef IMPMUL_DEBUG
printf("IMPMUL\n");
printout_tree(child2);
printf("\n");
#endif
} else {
return child;
}
} else {
return child;
}
}
child_ptr = alloc_exptree(child);
if (!impmul) {
switch (oper.type) {
case TOK_DIV:
tree = new_exp_tree(DIV, NULL);
break;
case TOK_MUL:
tree = new_exp_tree(MULT, NULL);
break;
}
prev = oper.type;
++indx; /* eat add-op */
}
tree_ptr = alloc_exptree(tree);
add_child(tree_ptr, child_ptr);
if (impmul)
child = child2;
while (1) {
if (!impmul) {
if (!valid_tree(child = signed_expr()))
parse_fail("expression expected");
}
/* add term as child */
add_child(tree_ptr, alloc_exptree(child));
/*
* it could be an implicit multiplication,
* as in x log(x) or 5x
* -- or the the end of this construction...
*/
impmul = 0;
if (!is_mul_op((oper = peek()).type)) {
if (indx < tok_count) {
#ifdef IMPMUL_DEBUG
printf("trying special (tokindx=%d/%d)...\n", indx, tok_count);
//getchar();
#endif
if (valid_tree(child = pow_expr(1))) {
oper.type = TOK_MUL;
impmul = 1;
#ifdef IMPMUL_DEBUG
printf("IMPMUL\n");
#endif
} else {
#ifdef IMPMUL_DEBUG
printf("no\n");
#endif
return *tree_ptr;
}
} else {
return *tree_ptr;
}
}
if (!impmul) {
switch (oper.type) {
case TOK_DIV:
new = new_exp_tree(DIV, NULL);
break;
case TOK_MUL:
new = new_exp_tree(MULT, NULL);
break;
}
++indx; /* eat add-op */
} else {
new = new_exp_tree(MULT, NULL);
}
new_ptr = alloc_exptree(new);
add_child(new_ptr, tree_ptr);
tree_ptr = new_ptr;
}
return *tree_ptr;
}
/*
signed_expr := [-] pow_expr
*/
exp_tree_t signed_expr()
{
token_t tok = peek();
exp_tree_t tree, subtree;
if (tok.type == TOK_MINUS) {
++indx;
tree = new_exp_tree(NEGATIVE, NULL);
if (valid_tree(subtree = pow_expr(0)))
add_child(&tree, alloc_exptree(subtree));
else
parse_fail("expression expected after sign");
return tree;
} else
return pow_expr(0);
}
/*
pow_expr := unary_expr ['^' expr]
*/
exp_tree_t pow_expr(int special)
{
exp_tree_t tree, subtree, subtree2;
exp_tree_t subtree3;
exp_tree_t lv;
token_t oper;
token_t tok;
int save = indx;
if (valid_tree(subtree = unary_expr(special))) {
if (peek().type != TOK_EXP) {
return subtree;
}
else {
tree = new_exp_tree(EXP, NULL);
++indx; /* eat comp-op */
add_child(&tree, alloc_exptree(subtree));
if (!valid_tree(subtree2 = pow_expr(special)))
parse_fail("expression expected");
add_child(&tree, alloc_exptree(subtree2));
}
return tree;
}
return null_tree;
}
/*
unary_expr := lvalue | integer
| '(' expr ')'
| ident '(' expr1, expr2, exprN ')'
| - expr
*/
exp_tree_t unary_expr(int special)
{
exp_tree_t tree = null_tree, subtree = null_tree;
exp_tree_t subtree2 = null_tree, subtree3;
token_t tok;
int matchfunc = 0;
/* $ident (expr1, ..., exprN) */
if (peek().type == TOK_DOLLAR) {
++indx;
matchfunc = 1;
}
/* ident ( expr1, expr2, exprN ) */
if (peek().type == TOK_IDENT) {
tok = peek();
++indx;
if (peek().type == TOK_LPAREN) {
++indx; /* eat ( */
subtree = new_exp_tree(matchfunc ? MATCH_FUNC :
FUNC, &tok);
while (peek().type != TOK_RPAREN) {
subtree2 = expr();
if (!valid_tree(subtree2))
parse_fail("expression expected");
add_child(&subtree, alloc_exptree(subtree2));
if (peek().type != TOK_RPAREN)
need(TOK_COMMA);
}
++indx;
/*
* If there's already a negative sign,
* use that as a parent tree
*/
if (valid_tree(tree))
add_child(&tree, alloc_exptree(subtree));
else
tree = subtree;
return tree;
} else
--indx;
}
/* parenthesized expression */
if(peek().type == TOK_LPAREN) {
++indx; /* eat ( */
if (!valid_tree(tree = expr()))
parse_fail("expression expected");
need(TOK_RPAREN);
return tree;
}
tok = peek();
if (tok.type == TOK_INTEGER) {
tree = new_exp_tree(NUMBER, &tok);
++indx; /* eat number */
return tree;
}
if (valid_tree(tree = lval())) {
return tree;
}
if (!special && tok.type == TOK_MINUS) {
++indx;
tree = new_exp_tree(NEGATIVE, NULL);
if (valid_tree(subtree = pow_expr(0)))
add_child(&tree, alloc_exptree(subtree));
else
parse_fail("expression expected after sign");
return tree;
}
return null_tree;
}