parser.y
14.4 KB
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
567
568
569
570
/*****************************************************************************/
/* */
/* Projecto de Compiladores ---------------------------------- VSPL */
/* Level 0 */
/* */
/* Bison Parser Specification. */
/* */
/* Salvador Pinto Abreu */
/* */
/*****************************************************************************/
/*****************************************************************************/
/* */
/* Prelude. */
/* */
/*****************************************************************************/
%{
#include <stdio.h>
#include <ctype.h>
#include <strings.h>
#include <ctype.h>
#include "types.h"
#define GUARD_STACK_SIZE 16
static int guardStack[GUARD_STACK_SIZE];
static int *guardSp = guardStack+GUARD_STACK_SIZE;
static int guardNo = 0;
#define ENTER_GUARD() (*--guardSp = ++guardNo)
#define EXIT_GUARD() (*guardSp++)
#define GUARD(N) (guardSp[N])
#define APT(pop, push) \
{ \
if (pop) \
printf ("[%s|_Tail] ==> [", pop); \
else \
printf ("_Tail ==> ["); \
printf push; \
printf ("|_Tail].\n"); \
}
#define PUSH(x) \
{ \
printf ("_Tail ==> ["); \
printf x; \
printf ("|_Tail].\n"); \
}
#define CONS() APT ("T,H", ("[H|T]"))
#define CONS_SWAPPED() APT ("H,T", ("[H|T]"))
#define TAIL() APT ("T", ("[T]"))
#define NIL() PUSH (("[]"))
#define TAIL_OPEN() APT ("T", ("[T|_]"))
#define NIL_OPEN() PUSH (("_"))
#define BINARY(op) APT ("B,A", ("op(A,B)"))
#define UNARY(op) APT ("X", ("op(X)"))
%}
%union {
UNION_DEFS();
}
/*****************************************************************************/
/* */
/* Terminal declarations. */
/* */
/*****************************************************************************/
%token ID /* An identifier */
%token INT /* The INT reserved word */
%token REAL /* The REAL reserved word */
%token BOOL /* The BOOL reserved word */
%token STRING /* The VOID reserved word */
%token MAP /* The MAP reserved word */
%token CLASS /* The CLASS reserved word */
%token INT_LIT /* An INT literal */
%token REAL_LIT /* A REAL literal */
%token BOOL_LIT /* A BOOL literal */
%token STRING_LIT /* A STRING literal */
%token RETURN /* The RETURN reserved word */
%token RECURSE /* The RECURSE reserved word */
%token BREAK /* The BREAK reserved word */
%token SKIP /* The SKIP reserved word */
%token RETRY /* The RETRY reserved word */
%token ELSE /* The ELSE reserved word */
%token COND /* The COND reserved word (?) */
%token WHILE /* The WHILE reserved word (*) */
%token IDENTICAL /* The "==" token */
%token ASSIGN /* The ":=" token */
%token RETURNS /* The "->" token */
%token AND /* The AND operator */
%token OR /* The OR operator */
%token NOT /* The NOT operator */
%token LT /* The "<" operator */
%token LE /* The "<=" operator */
%token EQ /* The "=" operator */
%token NE /* The "~=" operator */
%token GE /* The ">=" operator */
%token GT /* The ">" operator */
/*****************************************************************************/
/* */
/* Operator Precedences. */
/* */
/*****************************************************************************/
%nonassoc ':' ';' /* ID/type separator */
%nonassoc RETURN ASSIGN RETURNS /* Must do it because of <exp> */
%right ',' /* Tuple operator */
%left OR
%left AND
%right NOT
%nonassoc LT LE EQ NE GE GT
%left '+' '-'
%left '*' '/' '%'
%nonassoc UMINUS
%nonassoc '(' ')' '[' ']' '{' '}' /* Well, not really, but... */
/* Have to list these after "->" */
%left '.' /* Qualified name constructor */
/*****************************************************************************/
/* */
/* Grammar symbol type declarations. */
/* */
/*****************************************************************************/
%type <id> ID
%type <lit.ival> INT_LIT
%type <lit.rval> REAL_LIT
%type <lit.bval> BOOL_LIT
%type <lit.sval> STRING_LIT
/*****************************************************************************/
/* */
/* Start Symbol. */
/* */
/*****************************************************************************/
%start program
%%
program :
{ *--guardSp = 0; }
globals /* Global declarations */
;
/*****************************************************************************/
/* */
/* Declarations. */
/* */
/*****************************************************************************/
decl : /* Name declaration: */
ids ':' type /* Variable, Explicit type, Undef */
{ APT ("T,I", ("decl_list(I, var, T)")); }
| ID ':' type /* Variable, Explicit type, Undef */
{ APT ("T", ("decl(id('%s'), var, T)", $1)); }
| ID ':' type ASSIGN exp /* Variable, Explicit type, Init */
{ APT ("V,T", ("decl(id('%s'), var(V), T)", $1)); }
| ID ':' type EQ exp /* Constant, Explicit type */
{ APT ("V,T", ("decl(id('%s'), const(V), T)", $1)); }
| ID EQ exp /* Constant, Implicit type */
{ APT ("V", ("decl(id('%s'), const(V), T)", $1)); }
;
/******************************************************************************/
formals : /* Formal declarations: */
{ NIL (); }
| formal
{ }
| formal ';' formal_list
{ CONS (); }
;
formal_list : /* List of formal declarations */
formal
{ TAIL (); }
| formal ';' formal_list
{ CONS (); }
| error
{ yyerrok; yyclearin;
yyerror ("expected a formal declaration.");
PUSH (("error(formals)")); }
;
formal : /* Formal declaration: */
ids /* Implicit type */
{ APT ("I", ("decl_list(I, formal, T)")); }
| ID /* Implicit type */
{ PUSH (("decl(id('%s'), formal, T)", $1)); }
| ids ':' type /* Explicit type */
{ APT ("T,I", ("decl_list(I, formal, T)")); }
| ID ':' type /* Explicit type */
{ APT ("T", ("decl(id('%s'), formal, T)", $1)); }
;
/******************************************************************************/
fields : /* Field declarations: */
field
{ }
| field ';' field_list
{ CONS (); }
;
field_list : /* List of field declarations */
field
{ TAIL (); }
| field ';' field_list
{ CONS (); }
| error
{ yyerrok; yyclearin;
yyerror ("expected s field declaration.");
PUSH (("error(fields)")); }
;
field : /* Field declaration: */
ids ':' type /* Explicit type */
{ APT ("T,I", ("field_list(I, T)")); }
| ID ':' type /* Explicit type */
{ APT ("T", ("field(id('%s'), T)", $1)); }
;
/******************************************************************************/
globals : /* Global declarations: */
{ NIL (); }
| global
{ }
| global ';' global_list
{ CONS (); }
;
global_list :
global
{ TAIL (); }
| global ';' global_list
{ CONS (); }
| error
{ yyerrok; yyclearin;
yyerror ("expected a global declaration.");
PUSH (("error(decls)")); }
;
global : /* Global declaration: */
ids ':' type
{ APT ("T,I", ("decl_list(I, var, T)")); }
| ID ':' type
{ APT ("T", ("decl(id('%s'), var, T)", $1)); }
| ID ':' type ASSIGN exp
{ APT ("V,T", ("decl(id('%s'), var(V), T)", $1)); }
| ID ASSIGN exp
{ APT ("V", ("decl(id('%s'), const(V), T)", $1)); }
| ID ':' global_type EQ global_exp
{ APT ("V,T", ("decl(id('%s'), const(V), T)", $1)); }
| ID EQ global_exp
{ APT ("V", ("decl(id('%s'), const(V), T)", $1)); }
;
/******************************************************************************/
ids : ID ',' ids_aux
{ PUSH (("id('%s')", $1)); CONS_SWAPPED (); }
;
ids_aux : ID
{ PUSH (("id('%s')", $1)); TAIL (); }
| ID ',' ids_aux
{ PUSH (("id('%s')", $1)); CONS_SWAPPED (); }
;
/*****************************************************************************/
/* */
/* Type signatures. */
/* */
/*****************************************************************************/
types : /* Type list */
{ NIL (); }
| type /* Single type (end of list) */
{ }
| type ',' type_list /* Type tuple (list) */
{ CONS (); }
;
type_list :
type
{ TAIL (); }
| type ',' type_list
{ CONS (); }
;
type : /* Type signature (type expression) */
'(' types ')' /* Bracketing */
{ }
| INT /* Integer */
{ PUSH (("int")); }
| REAL /* Real */
{ PUSH (("real")); }
| BOOL /* Boolean */
{ PUSH (("bool")); }
| STRING /* String */
{ PUSH (("string")); }
| '[' exp ']' type /* Array type */
{ APT ("T,B", ("array(T, B)")); }
| '{' fields '}' /* Record/Class type */
{ APT ("F", ("class(F)")); }
| error
{ yyerrok; yyclearin;
yyerror ("expected a type.");
PUSH (("error(type)")); }
;
/******************************************************************************/
global_type : /* Type signature valid only at top */
type /* An ordinary type */
{ }
| type RETURNS type /* Function type */
{ APT ("R,D", ("map(D, R)")); }
;
/*****************************************************************************/
/* */
/* Expressions. */
/* */
/*****************************************************************************/
exp : /* Generic expression */
sexp
{ }
| sexp ',' exp_list
{ CONS (); }
;
exp_list :
sexp
{ TAIL (); }
| sexp ',' exp_list
{ CONS (); }
;
sexp :
'(' exp ')'
{ }
| sexp OR sexp /* Boolean operators */
{ BINARY (or); }
| sexp AND sexp
{ BINARY (and); }
| NOT sexp
{ UNARY (not); }
| sexp LT sexp /* Relational operators */
{ BINARY (lt); }
| sexp LE sexp
{ BINARY (le); }
| sexp EQ sexp
{ BINARY (eq); }
| sexp NE sexp
{ BINARY (ne); }
| sexp GE sexp
{ BINARY (ge); }
| sexp GT sexp
{ BINARY (gt); }
| sexp '+' sexp /* Arithmetic operators */
{ BINARY (add); }
| sexp '-' sexp
{ BINARY (sub); }
| sexp '*' sexp
{ BINARY (mul); }
| sexp '/' sexp
{ BINARY (div); }
| sexp '%' sexp
{ BINARY (mod); }
| '-' sexp %prec UMINUS
{ UNARY (neg); }
| sexp '.' ID /* Qualified names */
{ PUSH (("id('%s')", $3)); BINARY (name); }
| sexp '[' exp ']' /* Array references */
{ BINARY (array); }
| sexp '(' exp ')' /* Function references */
{ BINARY (map); }
| sexp '(' ')' /* Function references */
{ NIL (); BINARY (map); }
| RECURSE '(' exp ')' /* Recursion */
{ UNARY (recurse); }
| RECURSE '(' ')' /* Recursion */
{ NIL (); UNARY (recurse); }
| ID /* Simple names */
{ PUSH (("id('%s')", $1)); }
| INT_LIT /* Integer constant */
{ PUSH (("lit(int, %d)", $1)); }
| REAL_LIT /* Real constant */
{ PUSH (("lit(real, %g)", $1)); }
| BOOL_LIT /* Boolean constant */
{ PUSH (("lit(bool, %s)", $1? "true": "false")); }
| STRING_LIT /* String constant */
{ PUSH (("lit(string, '%s')", $1)); }
;
/******************************************************************************/
global_exp :
sexp /* An ordinary expression */
{ }
| MAP '(' formals ')' /* Function literal */
'[' stats ']'
{ APT ("S,F", ("lit(map(TF,TR), map(F,S,Symbols))")); }
| MAP '(' formals ')' RETURNS type /* Function literal */
'[' stats ']'
{ APT ("S,TR,F", ("lit(map(TF,TR), map(F,S,Symbols))")); }
;
/******************************************************************************/
primary : /* Restricted expression */
prim
{ }
| prim ',' prim_list
{ CONS (); }
;
prim_list :
prim
{ TAIL (); }
| prim ',' prim_list
{ CONS (); }
;
prim :
'(' primary ')'
{ }
| ID
{ PUSH (("id('%s')", $1)); }
| prim '.' prim
{ BINARY (name); }
| prim '[' exp ']'
{ BINARY (array); }
;
/*****************************************************************************/
/* */
/* Statements. */
/* */
/*****************************************************************************/
stats : /* EMPTY */
{ NIL (); }
| stat_list
{ }
;
stat_list :
stat
{ TAIL (); }
| stat ';'
{ TAIL (); }
| stat ';' stat_list
{ CONS (); }
| error
{ yyerrok; yyclearin;
yyerror ("expected a valid statement.");
PUSH (("error(stats)")); }
;
stat : /* A statement: */
decl /* Declaration */
{ }
| action /* Assignment or call */
{ }
| RETURN exp
{ APT ("E", ("return(E)")); }
| BREAK
{ PUSH (("break(%d)", GUARD(0))); }
| SKIP
{ PUSH (("skip(%d)", GUARD(0))); }
| RETRY
{ PUSH (("retry(%d)", GUARD(0))); }
| BREAK INT_LIT
{ PUSH (("break(%d)", GUARD($2))); }
| SKIP INT_LIT
{ PUSH (("skip(%d)", GUARD($2))); }
| RETRY INT_LIT
{ PUSH (("retry(%d)", GUARD($2))); }
| COND
{ ENTER_GUARD(); }
'[' clauses ']'
{ APT ("B", ("cond(B, %d)", EXIT_GUARD())); }
| WHILE
{ ENTER_GUARD(); }
'[' clauses ']'
{ APT ("B", ("while(B, %d)", EXIT_GUARD())); }
| '[' stats ']'
{ }
;
clauses :
exp RETURNS stats
{ APT ("S,E", ("[clause(E, S)]")); }
| exp RETURNS stats '|' clauses
{ APT ("Cs,S,E", ("[clause(E, S)|Cs]")); }
| exp RETURNS stats '|' ELSE RETURNS stats
{ APT ("ES,S,E", ("[clause(E, S),else(ES)]")); }
| error ']'
{ yyerrok; yyclearin;
yyerror ("syntax error in guarded clause.");
PUSH (("error(clause)")); }
;
action :
primary ASSIGN exp /* Assignment */
{ BINARY (assign); }
| primary '(' ')' /* Function references */
{ NIL (); BINARY (map); }
| primary '(' exp ')' /* Function call */
{ BINARY (map); }
| RECURSE '(' ')' /* Recursion */
{ NIL (); UNARY (recurse); }
| RECURSE '(' exp ')' /* Recursion */
{ UNARY (recurse); }
;
%%
yyerror(s)
char *s;
{
if (strcmp(s, "syntax error")) {
fprintf (stderr, "%s\n", s);
}
}