-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathll1.p
803 lines (699 loc) · 22 KB
/
ll1.p
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
program ll1_program (input, output, stderr);
{author:
G.L.J.M. Janssen
Building: EH, room 7.27, phone +31-40-473387
e-mail: [email protected]
Eindhoven University of Technology
Dept. of Electrical Engineering
P.O. Box 513
5600 MB Eindhoven
The Netherlands
references:
}
uses
lex_unit,
names_unit,
ll1_unit;
var i : integer;
procedure sbnf_parser;
var
savep,
startp, finalp : nodeptr;
atombeginsyms,
primbeginsyms,
factbeginsyms,
termfollowsyms : setofsymbol;
function getlhsnonterm : boolean;
begin
if not (symbol in [ LESSop, identifier ]) then begin
error (21);
skipuntil ([ LESSop, identifier, fileend ]);
end; {if}
if (symbol in [ LESSop, identifier ]) then begin
if symbol = LESSop then begin
getsymbol;
if symbol <> identifier then begin
error (15);
{identifier expected}
skipuntil ([identifier, fileend]);
end; {if}
if symbol = identifier then begin
getsymbol;
if symbol <> GREATERop then error (14);
{ ">" expected; inserted}
getlhsnonterm := true;
symbol := nonterminalsym;
end
else {fileend}
getlhsnonterm := false;
end
else begin {identifier}
getlhsnonterm := true;
symbol := nonterminalsym;
end; {if}
end
else {fileend}
getlhsnonterm := false;
end; {getlhsnonterm}
procedure production_rule (var startp, finalp : nodeptr);
var
ntindex : tokenidrange;
savefinalp,
savep : nodeptr;
procedure createleafnode (sort : kinds;
var startp, finalp : nodeptr
);
begin
case sort of
epsilon :
begin
new (startp{, epsilon});
startp^.state := yes;
end; {epsilon}
termornonterm :
begin
new (startp{, termornonterm});
with startp^ do begin
getindex (nameid);
{update rhs links:}
with tokeninfo[nameid] do begin
{push at front:}
nextrhsoccur := {token..}lastrhsoccur;
{token..}lastrhsoccur := startp;
{token..}rhscount := {token..}rhscount + 1;
end; {with}
if tokenkind[nameid] {= nonterm} then begin
state := undecided;
{information in possible lhs-node for this non-term
intentionally not used because of possibility of
useless productions
}
end
else begin
state := no;
end; {if}
end; {with}
end;
illegal :
begin
new (startp{, illegal});
startp^.state := no;
end; {illegal}
grammarkind,
rulekind,
zeroormore, oneormore,
condoneormore, optional,
factorsequence,
alternative : {not applicable};
end; {case}
with startp^ do begin
{ backlink := nil;}
kind := sort;
firstdet := false;
firstset := nil;
succp := nil;
end; {with}
finalp := startp;
{post: startp = finalp (<> nil)}
end; {createleafnode}
procedure expression (var startp, finalp : nodeptr);
procedure term (var startp, finalp : nodeptr);
procedure factor (var startp, finalp : nodeptr);
function atomic : boolean;
var savesymbol : symbolkind;
begin
if symbol in atombeginsyms then begin
atomic := true;
if symbol in [ LESSop, identifier, EMPTYkw, EOLkw, TABkw ] then
begin {perhaps: non-terminal or pseudo}
if symbol = LESSop then begin
getsymbol;
if symbol = GREATERop then
symbol := epsilonsym
else begin {identifier or keyword expected}
if not (symbol in [ identifier,
EMPTYkw, EOLkw, TABkw ] ) then begin
error (22);
{identifier or reserved word expected}
skipuntil ([identifier, EMPTYkw, EOLkw, TABkw, fileend]);
end; {if}
if symbol <> fileend then begin
savesymbol := symbol;
getsymbol;
if symbol <> GREATERop then error (14);
{ ">" expected; inserted}
if savesymbol = identifier then
symbol := nonterminalsym
else begin {reserved word}
case savesymbol of
EMPTYkw : symbol := epsilonsym;
EOLkw :
begin
identstr := blankidentstr;
identstr[1] := EOLINE;
symlength := 1;
symbol := terminalsym;
end;
TABkw :
begin
identstr := blankidentstr;
identstr[1] := TAB;
symlength := 1;
symbol := terminalsym;
end;
end; {case}
end; {if}
{identstr still contains the identifier when no error}
end
else {fileend}
atomic := false;
end; {if}
end
else {identifier or reserved word}
if symbol = identifier then
symbol := nonterminalsym
else begin {reserved word}
case symbol of
EMPTYkw : symbol := epsilonsym;
EOLkw :
begin
identstr := blankidentstr;
identstr[1] := EOLINE;
symlength := 1;
symbol := terminalsym;
end;
TABkw :
begin
identstr := blankidentstr;
identstr[1] := TAB;
symlength := 1;
symbol := terminalsym;
end;
end; {case}
end; {if}
end; {if}
{else proper atomic symbol}
end
else
atomic := false;
end; {atomic}
procedure primary (var startp, finalp : nodeptr);
begin {primary}
{writeln (stderr, 'primary');}
if atomic then begin
{writeln (stderr, 'atom');}
case symbol of
epsilonsym :
createleafnode (epsilon, startp, finalp);
terminalsym :
createleafnode (termornonterm, startp, finalp);
nonterminalsym :
createleafnode (termornonterm, startp, finalp);
end; {case}
getsymbol;
end
else
case symbol of
lparen:
begin
{writeln (stderr, 'compound');}
getsymbol;
expression (startp, finalp);
if symbol = rparen then
getsymbol
else error (16);
{ ")" expected; inserted}
end; {lparen}
otherwise
begin
error (12);
{ error in primary }
createleafnode (illegal, startp, finalp);
end; {otherwise}
end; {case-if}
{post: startp = finalp (<> nil)}
end; {primary}
begin {factor}
{writeln (stderr, 'factor');}
case symbol of
{ [ E ] }
lbracket: {option}
begin
{writeln (stderr, 'option');}
getsymbol;
expression (startp, finalp);
if startp^.state = yes then
error (203);
{LL(1) conflict : expr. in optional construct may be empty}
new (finalp{, optional});
with finalp^ do begin
{ backlink := nil;}
state := yes;
kind := optional;
succp := nil;
altp := startp;
firstdet := false;
firstset := nil;
end; {with}
startp^.backlink := finalp;
startp := finalp;
if symbol = rbracket then
getsymbol
else error (17);
{ "]" expected; inserted}
end; {lbracket}
{ (: E :) }
{ (: E :) + }
{ (: E :) * }
{ (: E / A :) }
{ (: E / A :) + }
{ (: E / A :) * }
lbrace: {list}
begin
{writeln (stderr, 'list');}
getsymbol;
new (startp{, assume: zeroormore});
with startp^ do begin
{ backlink := nil;}
kind := zeroormore;
succp := nil;
expression (altp, finalp);
{ temp. value, corrected later: }
state := altp^.state;
altp^.backlink := startp;
firstdet := false;
firstset := nil;
end; {with}
if symbol = separatesym then begin
{ (: E / A :) }
{ (: E / A :) + }
{ (: E / A :) * }
getsymbol;
if atomic then begin
case symbol of
{ (: E / <> :) ==> (: E :) }
epsilonsym : ;
terminalsym,
nonterminalsym :
begin
createleafnode (termornonterm, finalp^.succp, finalp);
finalp^.backlink := startp;
startp^.kind := condoneormore;
end;
end; {case}
getsymbol;
end
else
error (20);
{atomic symbol expected}
end; {if symbol =separatesym}
if symbol = rbrace then getsymbol
else error (18);
{ expected; inserted}
if symbol in [ PLUSsym, TIMESsym] then begin
{ (: E :) + }
{ (: E :) * }
{ (: E / A :) + }
{ (: E / A :) * }
case symbol of
PLUSsym :
with startp^ do
if kind = zeroormore then
kind := oneormore;
TIMESsym :
if startp^.kind = condoneormore then begin
if startp^.state = yes then
error (204);
{LL(1) conflict : E in (: E / A :)* construct may be empty}
finalp := startp;
new (startp{, optional});
with startp^ do begin
{ backlink := nil;}
state := yes;
kind := optional;
succp := nil;
altp := finalp;
firstdet := false;
firstset := nil;
end; {with}
finalp^.backlink := startp;
end
else begin {kind = zeroormore}
if startp^.state = yes then
error (208)
{LL(1) conflict : E in (: E :)* construct may be empty}
else
startp^.state := yes;
end; {if}
end; {case}
getsymbol;
end
else {no PLUS or TIMES sym}
{ (: E :) }
{ (: E / A :) }
{same action as if TIMES where present}
if startp^.kind = condoneormore then begin
{ (: E / A :) }
if startp^.state = yes then
error (205);
{LL(1) conflict : E in (: E / A :) construct may be empty}
finalp := startp;
new (startp{, optional});
with startp^ do begin
{ backlink := nil;}
state := yes;
kind := optional;
succp := nil;
altp := finalp;
firstdet := false;
firstset := nil;
end; {with}
finalp^.backlink := startp;
end
else begin {kind = zeroormore}
{ (: E :) }
if startp^.state = yes then
error (209)
{LL(1) conflict : E in (: E :) construct may be empty}
else
startp^.state := yes;
end; {if}
finalp := startp;
end; {lbrace}
otherwise
begin
primary (startp, finalp);
if symbol in [ PLUSsym, TIMESsym, questsym] then begin
case symbol of
PLUSsym :
begin
new (finalp{, oneormore});
with finalp^ do begin
state := startp^.state;
kind := oneormore;
firstdet := false;
firstset := nil;
end; {with}
end;
TIMESsym :
begin
if startp^.state = yes then
error (206);
{LL(1) conflict : Primary in "Primary *" may be empty}
new (finalp{, zeroormore});
with finalp^ do begin
state := yes;
kind := zeroormore;
firstdet := false;
firstset := nil;
end; {with}
end;
questsym :
begin
if startp^.state = yes then
error (207);
{LL(1) conflict : Primary in "Primary ?" may be empty}
new (finalp{, optional});
with finalp^ do begin
state := yes;
kind := optional;
firstdet := false;
firstset := nil;
end; {with}
end;
end; {case}
with finalp^ do begin
{ backlink := nil;}
succp := nil;
altp := startp;
end; {with}
startp^.backlink := finalp;
startp := finalp;
getsymbol;
end; {if}
end; {otherwise}
end; {case}
{post: startp = finalp (<> nil)}
end; {factor}
begin {term}
{writeln (stderr, 'term');}
if symbol in factbeginsyms then begin
factor (startp, finalp);
if symbol in factbeginsyms then begin {note: startp = finalp}
new (startp{, factorsequence});
with startp^ do begin
{ backlink := nil;}
state := finalp^.state;
kind := factorsequence;
succp := nil;
altp := finalp;
firstdet := false;
firstset := nil;
end; {with}
finalp^.backlink := startp;
repeat
factor (finalp^.succp, finalp);
finalp^.backlink := startp;
with startp^ do
case state of
yes : {all factor states yes then term state yes}
state := finalp^.state;
no :
if finalp^.state = undecided then
state := undecided;
undecided : ; {once undecided, always undecided}
end; {case-with}
until not (symbol in factbeginsyms);
finalp := startp;
end; {if}
end
else {probably empty term; check followsyms:}
if symbol in termfollowsyms then {empty term}
createleafnode (epsilon, startp, finalp)
else begin
error (3); {illegal symbol}
skipuntil (termfollowsyms + [ fileend ]);
createleafnode (illegal, startp, finalp);
end; {if}
{post: startp = finalp (<> nil)}
end; {term}
begin {expression}
{writeln (stderr, 'expression');}
term (startp, finalp);
if symbol = choicesym then begin { 2 or more terms}
{create alternative kind descriptor and link first term to it:}
new (startp{, alternative});
with startp^ do begin
{ backlink := nil;}
state := finalp^.state;
kind := alternative;
succp := nil;
altp := finalp;
firstdet := false;
firstset := nil;
end; {with startp^}
finalp^.backlink := startp;
repeat {for all Ti, 2 <= i <= n}
getsymbol;
term (finalp^.succp, finalp);
finalp^.backlink := startp;
with startp^ do begin
case state of
yes : {1 term yes then expr. yes}
{Note: state remains the same!}
if finalp^.state = yes then {more than 1 eps term}
{==> kind <> termornonterm}
begin
error (202);
{LL(1) condition violated because of more than 1 empty term}
end; {if}
no :
if finalp^.state = yes then
state := yes;
undecided : {terms undecided or no then expr. undecided}
state := finalp^.state;
{else remains undecided}
end; {case}
end; {with startp}
until (symbol <> choicesym);
finalp := startp;
end; {if symbol = choice ...}
{post: startp = finalp (<> nil)}
end; {expression}
begin {production_rule}
{writeln (stderr, 'production_rule : ');}
{symbol = (lhs) nonterminalsym}
{combine rules with same lhs non-terminal:}
getindex (ntindex);
{read replacement symbol:}
getsymbol;
if symbol = replacesym then getsymbol else error (13);
{"::=" expected; inserted}
if not lhsnonterm[ntindex] then begin {not yet occurred on lhs}
nrproductions := nrproductions + 1;
lhsnonterm[ntindex] := true;
{create rulekind node:}
new (startp{, rulekind});
with startp^ do begin
{ backlink := nil;}
kind := rulekind;
succp := nil;
nameid := ntindex;
linenr := linecount; {line where this lhs first occurs}
visited := false;
expression (altp, finalp);
state := finalp^.state;
firstdet := false;
firstset := nil;
end; {with}
finalp^.backlink := startp;
tokeninfo[ntindex].lhsoccur := startp;
finalp := startp; {both <> nil}
{ready}
end
else begin {rule with this non-terminal on lhs exists}
{make sure value of finalp does not change for caller:}
savefinalp := finalp;
{get pointer to lhs:}
startp := tokeninfo[ntindex].lhsoccur;
{check if E has alternative kind of top node:}
with startp^ do begin
if altp^.kind <> alternative then begin
{create new alternative node:}
{this code is executed at most once}
{save link:}
finalp := altp;
new (altp{, alternative});
with altp^ do begin
kind := alternative;
backlink := startp;
succp := nil;
state := finalp^.state;
{restore link:}
{startp^.altp^.}altp := finalp;
firstdet := false;
firstset := nil;
end; {with altp^}
finalp^.backlink := altp;
{startp^.state = startp^.altp^.state}
end; {if}
{altp^.kind = alternative}
{save all terms that are already there:}
savep := altp^.altp;
{new term in front:}
expression (altp^.altp, finalp);
finalp^.backlink := altp;
finalp^.succp := savep;
with altp^ do begin
{same state check as with expr.}
case state of
yes : {1 term yes then expr. yes}
if finalp^.state = yes then {more than 1 eps term}
{==> kind <> termornonterm}
begin
error (202);
{LL(1) condition violated because of more than 1 empty term}
end; {if}
no :
if finalp^.state = yes then
state := yes;
undecided :
state := finalp^.state;
{else remains undecided}
end; {case}
end; {with altp^}
{pass state onto rulekind node:}
state := altp^.state;
{if first rule then also pass state onto grammarkind node:}
if {startp^.}backlink^.kind = grammarkind then begin
backlink^.state := state;
end; {if}
end; {with startp^}
{NOTE: because of chaining action in sbnf_parser routine, startp should
return nil when no new rule created and finalp must be unchanged in that
case
}
finalp := savefinalp;
startp := nil;
end; {if}
if symbol = period then getsymbol else error (19);
{"." expected; inserted}
end; {production_rule}
begin {sbnf_parser}
{writeln (stderr, 'sbnf_parser');}
atombeginsyms := [ epsilonsym, terminalsym, LESSop, identifier,
EMPTYkw, EOLkw, TABkw ];
primbeginsyms := atombeginsyms + [ lparen ];
factbeginsyms := primbeginsyms + [ lbrace, lbracket ];
termfollowsyms := [ rbracket, rbrace, separatesym, questsym, rparen,
choicesym, period ];
new (startp{, grammarkind});
with startp^ do begin
backlink := nil;
state := undecided;
kind := grammarkind;
succp := nil;
altp := nil;
firstdet := false;
firstset := nil;
end; {with}
getsymbol;
if symbol <> fileend then begin
if getlhsnonterm then begin
with startp^ do begin
production_rule ({startp^.}altp, finalp);
{inherit state of first rule:}
{startp^.}state := altp^.state;
{first rule backlinks to grammar node:}
finalp^.backlink := startp;
end; {with startp^}
while symbol <> fileend do
if getlhsnonterm then begin
{save pointer to last rule:}
savep := finalp;
production_rule (finalp^.succp, finalp);
{if rule with same lhs was found then succp is made nil
finalp still points to last rule
else finalp points new last rule.
}
{establish backlink:}
{NOTE: the interpretation of backlink for rulekind nodes:
backlink refers to the previous rule for second and
following rules. backlink of first rule points back to
the grammarkind node.
}
if finalp <> savep then {new rulekind node added}
finalp^.backlink := savep;
{else no action}
end; {if-while}
{here: symbol=fileend}
end; {if}
{else symbol=fileend}
end; {if}
{writeln(stderr, 'Successfully parsed input.');}
{post: all non-terminals on rhs have undecided state;
nrnonterminals >= nrproductions
}
processgrammar (startp);
end; {sbnf_parser}
begin {main}
{Parse cmdline arguments:}
for i := 1 to paramCount() do begin
{writeLn(stderr, i:2, '. argument: ', paramStr(i));}
end;
if paramCount() > 0 then begin
{$I-}
assign(input, paramStr(1));
reset(input);
{$I+}
if IoResult <> 0 then begin
writeln(stderr, '(E): cannot read file: ', paramStr(1));
Halt;
end;
end;
initLex;
initNameTable;
sbnf_parser;
finalLex;
close(listing);
end.