-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpascal.y
373 lines (332 loc) · 7.33 KB
/
pascal.y
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
%{
#include <stdio.h>
#include "types.h"
extern int yylineno;
extern char yytext[];
extern char numbertext[80];
extern char *laststring;
extern char lastident[];
extern char *create_string();
#define YYSTYPE item
int lexical_level = 0;
static int param_level = 0;
static char *its_a = "error - uninitialised";
%}
%start program
%token UNSIGNED_INT UNSIGNED_REAL STRING IDENTIFIER
%token NE LE GE BECOMES DIV MOD NIL IN OR AND NOT DOTDOT
%token IF THEN ELSE CASE OF REPEAT UNTIL WHILE DO FOR TO DOWNTO
%token SBEGIN END WITH GOTO CONST VAR TYPE ARRAY RECORD SET SFILE FUNCTION
%token PROCEDURE LABEL PACKED PROGRAM
%% /*start of rules*/
program : PROGRAM {its_a="program";} newident external_files ';'
block '.'
;
external_files : /*empty*/
| '(' {its_a="external-file";} newident_list ')'
;
block : opt_declarations statement_part
;
opt_declarations: /*empty*/
| declarations
;
declarations : declarations declaration /*should be left-recursive*/
| declaration
;
declaration : label_dcl_part
| const_dcl_part
| type_dcl_part
| var_dcl_part
| proc_dcl_part
;
label_dcl_part : LABEL labels ';'
;
labels : labels ',' label
| label
;
label : UNSIGNED_INT /* 0 <= value <= 9999 [6.1.6] */
;
const_dcl_part : CONST const_defs ';'
;
const_defs : const_defs ';' const_def
| const_def
;
const_def : {its_a="constant";} newident '=' constant
;
constant : unsigned_num
| '+' unsigned_num
| '-' unsigned_num
| ident /*check it is constant*/
| '+' ident
| '-' ident
| STRING /*type is char if len=1*/
;
unsigned_num : UNSIGNED_INT
| UNSIGNED_REAL
;
type_dcl_part : TYPE type_defs ';'
;
type_defs : type_defs ';' type_def
| type_def
;
type_def : {its_a="type";} newident '=' type
;
type : simple_type
| PACKED struct_type
| struct_type
| '^' IDENTIFIER /*check forward reference semantics*/
;
simple_type : '(' {its_a="enumerated-literal";} newident_list ')'
| constant DOTDOT constant
| ident
;
struct_type : ARRAY '[' index_t_list ']' OF type
| RECORD /*consider this a scope*/ field_list END
| SET OF simple_type
| SFILE OF type
;
index_t_list : index_t_list ',' simple_type
| simple_type
;
field_list : fixed_part
| fixed_part ';' variant_part
| variant_part
;
fixed_part : fixed_part ';' record_section
| record_section
;
record_section : {its_a="field";} newident_list ':' type
| /*empty*/
;
variant_part : CASE {its_a="field";} tag_field OF variants
;
tag_field : newident ':' ident
| ident /*type*/
;
variants : variants ';' variant
| variant
;
variant : case_label_list ':' '(' field_list ')'
| /*empty*/
;
var_dcl_part : VAR variable_dcls ';'
;
variable_dcls : variable_dcls ';' variable_dcl
| variable_dcl
;
variable_dcl : {its_a="variable";} newident_list ':' type
;
newident_list : new_id_list {its_a="don't know";}
;
new_id_list : new_id_list ',' newident
| newident
;
proc_dcl_part : proc_or_func
;
proc_or_func : proc_heading ';' body ';' /*check if forward or fwd refd*/
{lexical_level--;
}
| func_heading ';' body ';' /*also func heading may be -type */
{lexical_level--;
}
;
proc_heading : PROCEDURE
{if(param_level==0)its_a="procedure";}
newident {lexical_level++;}
formal_params
;
func_heading : FUNCTION
{if(param_level==0)its_a="function";}
newident {lexical_level++;}
function_form
;
function_form : /*empty*/ /*if forward referenced*/
| formal_params ':' ident
;
body : block
/* result determined in block */
| IDENTIFIER /*directive-FORWARD*/
;
formal_params : /*empty*/
| '(' {param_level++;} formal_p_sects ')' {param_level--;}
;
formal_p_sects : formal_p_sects ';' formal_p_sect
| formal_p_sect
;
formal_p_sect : {its_a="value-parameter";} param_group
| VAR {its_a="var-parameter";} param_group
| {its_a="procedure-parameter";} proc_heading
{lexical_level--;}
| {its_a="function-parameter";} func_heading {lexical_level--;}
;
param_group : newident_list ':' paramtype
;
paramtype : ident
| ARRAY '[' index_specs ']' OF paramtype
| PACKED ARRAY '[' index_spec ']' OF ident
;
index_specs : index_specs ';' index_spec
| index_spec
;
index_spec : {its_a="conformant-bound";} newident DOTDOT newident ':' ident
;
statement_part : compound_stmt
;
compound_stmt : SBEGIN statements END
;
statements : statements ';' statement
| statement
;
statement : /*empty*/
| label ':' statement
| compound_stmt
| assignment
| procedure_call
| GOTO label
| IF expression THEN statement
| IF expression THEN statement ELSE statement
| CASE expression OF case_list END
| WHILE expression DO statement
| REPEAT statements UNTIL expression
| FOR ident BECOMES expression direction expression DO statement
| WITH rec_var_list DO statement
;
direction : TO
| DOWNTO
;
assignment : variable BECOMES expression /* must test for fn_ident */
/* | ident BECOMES expression */
;
procedure_call : ident actual_params
;
actual_params : /*empty*/
| '(' actuals_list ')'
;
actuals_list : actuals_list ',' actual_param
| actual_param
;
actual_param : expression /* which could be a variable or a proc/fn id */
| expression colon_things /* only in i/o */
;
colon_things : ":" expression /*integer*/
| ":" expression ":" expression
;
case_list : case_list ';' case_list_elem
| case_list_elem
;
case_list_elem : case_label_list ':' statement
| /*empty*/
;
case_label_list : case_label_list ',' case_label
| case_label
;
case_label : constant
;
rec_var_list : rec_var_list ',' record_var
| record_var
;
expression : simple_expr
| simple_expr relational_op simple_expr
;
relational_op : '='
| '<'
| '>'
| LE
| GE
| NE
| IN
;
simple_expr : term
| '+' term
| '-' term
| simple_expr add_op term
;
add_op : '+'
| '-'
| OR
;
term : factor
| term mult_op factor
;
mult_op : '*'
| '/'
| DIV
| MOD
| AND
;
factor : variable /* could be a const_ident of fn_call*/
| unsigned_lit
| '(' expression ')'
/* | function_call */
| set
| NOT factor
;
unsigned_lit : unsigned_num
| STRING /*type is char if len=1*/
| NIL
;
/*
function_call : ident actual_params
;
*/
set : '[' member_list ']'
;
member_list : /*empty*/
| members
;
members : members ',' member
| member
;
member : expression
| expression DOTDOT expression
;
/* kludge */
variable : ident actual_params /* check variable, const_id, fn_call */
| variable '[' expressions ']'
| variable '.' ident
| variable '^'
;
expressions : expressions ',' expression
| expression
;
record_var : variable
;
ident : IDENTIFIER
;
newident : IDENTIFIER
{
if(param_level<2)
printf("%s\t%s\n", its_a, lastident);
}
;
%% /*start of routines*/
yyerror(msg) char *msg;
{
if(msg==NULL || *msg=='\0')
fprintf(stderr, "Error at %s near line %d\n",
token_name(yychar), yylineno);
else
fprintf(stderr, "Error at %s near line %d : %s\n",
token_name(yychar), yylineno, msg);
exit(1);
}
parser_info()
{
printf("\n%d line%s parsed\n", yylineno, plural(yylineno));
}
internal_error(s,a1,a2,a3,a4)
{
fprintf(stderr, "Internal error: ");
fprintf(stderr, s, a1, a2, a3, a4);
exit(2);
}
warning(fmt, a1, a2, a3, a4)
{
fprintf(stderr, "Warning line %d: ", yylineno);
fprintf(stderr, fmt, a1, a2, a3, a4);
fprintf(stderr, "\n");
}
main()
{
yyparse();
}