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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
|
{
Semantic routines for the Yacc parser.
Copyright (c) 1990-92 Albert Graef <ag@muwiinfa.geschichte.uni-mainz.de>
Copyright (C) 1996 Berend de Boer <berend@pobox.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
$Revision: 1.2 $
$Modtime: 96-08-01 6:03 $
$History: YACCSEM.PAS $
*
* ***************** Version 2 *****************
* User: Berend Date: 96-10-10 Time: 21:16
* Updated in $/Lex and Yacc/tply
* Updated for protected mode, windows and Delphi 1.X and 2.X.
}
unit YaccSem;
interface
var
act_prec : Integer;
(* active precedence level in token and precedence declarations (0 in
%token declaration) *)
act_type : Integer;
(* active type tag in token, precedence and type declarations *)
procedure yyerror ( msg : String );
(* YaccLib.yyerror redefined to ignore 'syntax error' message; the parser
does its own error handling *)
function sym ( k : Integer ) : Integer;
(* returns internal symbol number for the symbol k; if k is yet undefined,
a new nonterminal or literal symbol is created, according to the
appearance of symbol k (nonterminal if an ordinary identifier, literal
otherwise) *)
function ntsym ( k : Integer ) : Integer;
(* like sym, but requires symbol k to be a nonterminal symbol; if it
is already defined a literal, an error message is issued, and a dummy
nonterminal symbol returned *)
function litsym ( k : Integer; n : Integer ) : Integer;
(* same for literal symbols; if n>0 it denotes the literal number to be
assigned to the symbol; when a new literal identifier is defined, a
corresponding constant definition is also written to the definition
file *)
procedure next_section;
(* find next section mark (%%) in code template *)
procedure definitions;
(* if necessary, write out definition of the semantic value type YYSType *)
procedure copy_code;
(* copy Turbo Pascal code section ( %{ ... %} ) to output file *)
procedure copy_action;
(* copy an action to the output file *)
procedure copy_single_action;
(* like copy_action, but action must be single statement terminated
with `;' *)
procedure copy_rest_of_file;
(* copies the rest of the source file to the output file *)
procedure start_rule ( sym : Integer );
(* start a new rule with lhs nonterminal symbol sym *)
procedure start_body;
(* start a new rule body (rhs) *)
procedure end_body;
(* end a rule body *)
procedure add_symbol ( sym : Integer );
(* add the denoted symbol to the current rule body *)
procedure add_action;
(* add an action to the current rule body *)
procedure add_rule_prec ( sym : Integer );
(* add the precedence of terminal symbol sym to the current rule *)
procedure generate_parser;
(* generate the parse table *)
implementation
uses YaccBase, YaccTabl, YaccClos, YaccLR0, YaccLook,
YaccPars, YaccMsgs;
procedure yyerror ( msg : String );
begin
if msg='syntax error' then
(* ignore *)
else
fatal(msg)
end(*yyerror*);
function act_char : char;
begin
if cno>length(line) then
if eof(yyin) then
act_char := #0
else
act_char := nl
else
act_char := line[cno]
end(*act_char*);
function lookahead_char : char;
begin
if succ(cno)>length(line) then
if eof(yyin) then
lookahead_char := #0
else
lookahead_char := nl
else
lookahead_char := line[succ(cno)]
end(*lookahead_char*);
procedure next_char;
begin
if cno>length(line) then
if eof(yyin) then
{ nop }
else
begin
readln(yyin, line);
inc(lno); cno := 1
end
else
inc(cno)
end(*next_char*);
var
(* Current rule: *)
act_rule : RuleRec;
(* Actions: *)
n_act : Integer;
p_act : Boolean;
function sym ( k : Integer ) : Integer;
var s : Integer;
begin
if is_def_key(k, s) then
sym := s
else if sym_table^[k].pname^[1]='''' then
begin
s := new_lit;
def_key(k, s);
sym := s;
end
else
begin
s := new_nt;
def_key(k, s);
sym := s;
end
end(*sym*);
function ntsym ( k : Integer ) : Integer;
var s : Integer;
begin
if is_def_key(k, s) then
if s<0 then
ntsym := s
else
begin
error(nonterm_expected);
ntsym := -1;
end
else if sym_table^[k].pname^[1]='''' then
begin
error(nonterm_expected);
ntsym := -1;
end
else
begin
s := new_nt;
def_key(k, s);
ntsym := s;
end
end(*ntsym*);
function litsym ( k : Integer; n : Integer ) : Integer;
var s : Integer;
begin
if is_def_key(k, s) then
if s>=0 then
begin
if n>0 then error(double_tokennum_def);
litsym := s;
end
else
begin
error(literal_expected);
litsym := 1;
end
else if sym_table^[k].pname^[1]='''' then
begin
if n>0 then
begin
add_lit(n);
s := n;
end
else
s := new_lit;
def_key(k, s);
litsym := s;
end
else
begin
if n>0 then
begin
add_lit(n);
s := n;
end
else
s := new_lit;
def_key(k, s);
writeln(yyout, 'const ', pname(s), ' = ', s, ';');
litsym := s;
end;
end(*litsym*);
procedure next_section;
var line : String;
begin
while not eof(yycod) do
begin
readln(yycod, line);
if line='%%' then exit;
writeln(yyout, line);
end;
end(*next_section*);
procedure definitions;
var i : Integer;
begin
if n_types>0 then
begin
writeln(yyout);
writeln(yyout, 'type YYSType = record case Integer of');
for i := 1 to n_types do
writeln(yyout, ' ':15, i:3, ' : ( ',
'yy', sym_table^[type_table^[i]].pname^, ' : ',
sym_table^[type_table^[i]].pname^, ' );');
writeln(yyout, ' ':15, 'end(*YYSType*);');
end;
end(*definitions*);
procedure copy_code;
var str_state : Boolean;
begin
str_state := false;
while act_char<>#0 do
if act_char=nl then
begin
writeln(yyout);
next_char;
end
else if act_char='''' then
begin
write(yyout, '''');
str_state := not str_state;
next_char;
end
else if not str_state and (act_char='%') and (lookahead_char='}') then
exit
else
begin
write(yyout, act_char);
next_char;
end;
end(*copy_code*);
procedure scan_val;
(* process a $ value in an action
(not very pretty, but it does its job) *)
var tag, numstr : String; i, code : Integer;
begin
tokleng := 0;
next_char;
if act_char='<' then
begin
(* process type tag: *)
next_char;
tag := '';
while (act_char<>nl) and (act_char<>#0) and (act_char<>'>') do
begin
tag := tag+act_char;
next_char;
end;
if act_char='>' then
begin
if not search_type(tag) then
begin
tokleng := length(tag);
error(unknown_identifier);
end;
next_char;
end
else
error(syntax_error);
end
else
tag := '';
tokleng := 0;
if act_char='$' then
begin
(* left-hand side value: *)
write(yyout, 'yyval');
(* check for value type: *)
if (tag='') and (n_types>0) then with act_rule do
if sym_type^[lhs_sym]>0 then
tag := sym_table^[sym_type^[lhs_sym]].pname^
else
begin
tokleng := 1;
error(type_error);
end;
if tag<>'' then write(yyout, '.yy', tag);
next_char;
end
else
begin
(* right-hand side value: *)
if act_char='-' then
begin
numstr := '-';
next_char;
end
else
numstr := '';
while ('0'<=act_char) and (act_char<='9') do
begin
numstr := numstr+act_char;
next_char;
end;
if numstr<>'' then
begin
val(numstr, i, code);
if code=0 then
if i<=act_rule.rhs_len then
begin
write(yyout, 'yyv[yysp-', act_rule.rhs_len-i, ']');
(* check for value type: *)
if (tag='') and (n_types>0) then with act_rule do
if i<=0 then
begin
tokleng := length(numstr)+1;
error(type_error);
end
else if sym_type^[rhs_sym[i]]>0 then
tag := sym_table^[sym_type^[rhs_sym[i]]].pname^
else
begin
tokleng := length(numstr)+1;
error(type_error);
end;
if tag<>'' then write(yyout, '.yy', tag);
end
else
begin
tokleng := length(numstr);
error(range_error);
end
else
error(syntax_error)
end
else
error(syntax_error)
end
end(*scan_val*);
procedure copy_action;
var str_state : Boolean;
begin
str_state := false;
while act_char=' ' do next_char;
write(yyout, ' ':9);
while act_char<>#0 do
if act_char=nl then
begin
writeln(yyout);
next_char;
while act_char=' ' do next_char;
write(yyout, ' ':9);
end
else if act_char='''' then
begin
write(yyout, '''');
str_state := not str_state;
next_char;
end
else if not str_state and (act_char='}') then
begin
writeln(yyout);
exit;
end
else if not str_state and (act_char='$') then
scan_val
else
begin
write(yyout, act_char);
next_char;
end;
end(*copy_action*);
procedure copy_single_action;
var str_state : Boolean;
begin
str_state := false;
while act_char=' ' do next_char;
write(yyout, ' ':9);
while act_char<>#0 do
if act_char=nl then
begin
writeln(yyout);
next_char;
while act_char=' ' do next_char;
write(yyout, ' ':9);
end
else if act_char='''' then
begin
write(yyout, '''');
str_state := not str_state;
next_char;
end
else if not str_state and (act_char=';') then
begin
writeln(yyout, ';');
exit;
end
else if not str_state and (act_char='$') then
scan_val
else
begin
write(yyout, act_char);
next_char;
end;
end(*copy_single_action*);
procedure copy_rest_of_file;
begin
while act_char<>#0 do
if act_char=nl then
begin
writeln(yyout);
next_char;
end
else
begin
write(yyout, act_char);
next_char;
end;
end(*copy_rest_of_file*);
procedure start_rule ( sym : Integer );
begin
if n_rules=0 then
begin
(* fix start nonterminal of the grammar: *)
if startnt=0 then startnt := sym;
(* add augmented start production: *)
with act_rule do
begin
lhs_sym := -1;
rhs_len := 2;
rhs_sym[1] := startnt;
rhs_sym[2] := 0; (* end marker *)
end;
add_rule(newRuleRec(act_rule));
end;
act_rule.lhs_sym := sym;
end(*start_rule*);
procedure start_body;
begin
act_rule.rhs_len := 0;
p_act := false;
writeln(yyout, n_rules:4, ' : begin');
end(*start_body*);
procedure end_body;
begin
if not p_act and (act_rule.rhs_len>0) then
(* add default action: *)
writeln(yyout, ' ':9, 'yyval := yyv[yysp-',
act_rule.rhs_len-1, '];');
add_rule(newRuleRec(act_rule));
writeln(yyout, ' ':7, 'end;');
end(*end_body*);
procedure add_rule_action;
(* process an action inside a rule *)
var k : Integer; r : RuleRec;
begin
writeln(yyout, ' ':7, 'end;');
inc(n_act);
k := get_key('$$'+intStr(n_act));
with r do
begin
lhs_sym := new_nt;
def_key(k, lhs_sym);
rhs_len := 0;
end;
with act_rule do
begin
inc(rhs_len);
if rhs_len>max_rule_len then fatal(rule_table_overflow);
rhs_sym[rhs_len] := r.lhs_sym;
end;
add_rule(newRuleRec(r));
rule_prec^[n_rules+1] := rule_prec^[n_rules];
rule_prec^[n_rules] := 0;
writeln(yyout, n_rules:4, ' : begin');
end(*add_rule_action*);
procedure add_symbol ( sym : Integer );
begin
if p_act then add_rule_action;
p_act := false;
with act_rule do
begin
inc(rhs_len);
if rhs_len>max_rule_len then fatal(rule_table_overflow);
rhs_sym[rhs_len] := sym;
if sym>=0 then rule_prec^[n_rules+1] := sym_prec^[sym]
end
end(*add_symbol*);
procedure add_action;
begin
if p_act then add_rule_action;
p_act := true;
end(*add_action*);
procedure add_rule_prec ( sym : Integer );
begin
rule_prec^[n_rules+1] := sym_prec^[sym];
end(*add_rule_prec*);
procedure generate_parser;
begin
if startnt=0 then error(empty_grammar);
if errors=0 then
begin
write('sort ... ');
sort_rules; rule_offsets;
write('closures ... ');
closures;
write('first sets ... ');
first_sets;
write('LR0 set ... ');
LR0Set;
write('lookaheads ... ');
lookaheads;
writeln;
write('code generation ... ');
parse_table;
end;
end(*generate_parser*);
begin
n_act := 0;
end(*YaccSem*).
|