-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathPARSER
1755 lines (1401 loc) · 70.9 KB
/
PARSER
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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(FILECREATED "17-Apr-86 16:28:32" {PHYLUM}<STANSBURY>PARSER>RELEASE.1>PARSER.;10 70376
changes to: (FNS PG.BACKUP PG.RESOLVE PG.BUILD.LOOKAHEAD.SETS PG.CODE.PARSER)
previous date: " 4-Apr-86 16:29:22" {PHYLUM}<STANSBURY>PARSER>RELEASE.1>PARSER.;7)
(* Copyright (c) 1983, 1984, 1986 by Xerox Corporation. All rights reserved.)
(PRETTYCOMPRINT PARSERCOMS)
(RPAQQ PARSERCOMS ((* * Parser generation system.)
(FNS MAKEPARSER PG.INITIALIZE.GRAMMAR)
(RECORDS PARSERSPEC GRAMMAR)
(DECLARE: EVAL@COMPILE DONTCOPY (RECORDS FPRODUCTION ALTERNATIVE))
(MACROS SELF STRICTEOF TCONC.FRONT)
(* * State machine generation. For an explanation of the parser technology, see
"Theory & Construction of LR(k) Parsers"
(Preliminary Version)
, Benjamin M. Brosgol, Center for Research in Computing Technology, Harvard University,
March 1973)
(FNS PG.LR0FSM PG.AFSMS PG.ROOTPRODUCTION PG.AFSM.ADDPROD PG.CONNECT PG.ADD.ARC
PG.DETERMINISTIC PG.DETERMINISTIC1 PG.CONNECT.AFSMS PG.CONNECT.AFSMS1 PG.SINGLETON
PG.CONNECTED PG.OWNS.LINKS PG.RABIN.SCOTT PG.RABIN.SCOTT1 PG.REPLACEMENT
PG.TRANSITION.SYMBOLS PG.NEXT.STATES PG.COMPOSITE.STATE.NAME PG.DISCONNECT
PG.NONTERMINALP PG.TERMINALP)
(FNS PG.LALRKFSM PG.BUILD.BackLinks PG.CONNECT.BACK PG.STATETYPE PG.RESOLVE
PG.PRINT.INADEQUATE PG.BUILD.LOOKAHEAD.SETS PG.DISJOINT PG.LLA PG.LLA.LOOKAHEAD
PG.LLA.READ PG.LLA.REDUCE PG.BACKUP PG.LOOKAHEAD.SOURCE PG.REDUCE.TARGET PG.LOOKAHEADP)
(FNS PG.OLD.LLA)
(DECLARE: EVAL@COMPILE DONTCOPY (RECORDS PRODUCTION FSM STATE LINK))
(INITRECORDS PRODUCTION FSM STATE LINK)
(FNS PG.SMASH.FSM)
(FNS PG.PPL PG.PPM PG.PPP PG.PPS)
(FNS PG.STATES PG.STATES1 PG.STATE.ORDER)
(* * Lisp code generation)
(FNS PG.CODE.PARSER PG.CODE.STATES)
(FNS PG.CODE.LOOKAHEAD PG.CODE.LOOKAHEAD.ALL.TOKENS PG.CODE.LOOKAHEAD.TOKEN
PG.CODE.LOOKAHEAD.SWITCH PG.CODE.LOOKAHEAD.MATCH)
(FNS PG.CODE.READ PG.CODE.READ.TOKEN PG.CODE.READ.SWITCH)
(FNS PG.CODE.REDUCE PG.CODE.REDUCE.STACKS PG.CODE.REDUCE.SWITCH)
(* * Other)
(FNS PRINT.FUNCTION)))
(* * Parser generation system.)
(DEFINEQ
(MAKEPARSER
[LAMBDA (PARSERSPEC SORTED?) (* hts: " 3-Apr-86 16:20")
(* * Constructs a parser according to PARSERSPEC)
(LET ((GRAMMAR (fetch GRAMMAR of PARSERSPEC))
(NAME (fetch PARSERNAME of PARSERSPEC)))
(PG.INITIALIZE.GRAMMAR GRAMMAR)
(LET [(M (PG.LALRKFSM (PG.LR0FSM GRAMMAR]
(/PUTD NAME (PG.CODE.PARSER M PARSERSPEC SORTED?))
(PG.SMASH.FSM M))
NAME])
(PG.INITIALIZE.GRAMMAR
[LAMBDA (G) (* hts: "28-Feb-86 17:45")
(* * Builds the SymbolTable for GRAMMAR G. The SymbolTable is a hashtable which maps each symbol in the alphabet of
grammar G onto {NONTERMINAL, TERMINAL}. Also checks the grammar for syntactic correctness)
(LET ((PRODS (fetch PRODUCTIONS of G))
(TABLE (fetch (GRAMMAR SymbolTable) of G)))
(* * Note the nonterminals)
(CLRHASH TABLE)
(for P in PRODS
do (if (NOT (type? FPRODUCTION P))
then (ERROR "Improper grammar format"))
(PUTHASH (fetch LEFTHAND of P)
(QUOTE NONTERMINAL)
TABLE))
(* * Note the terminals)
[for P in PRODS do (for A in (fetch ALTERNATIVES of P)
do (if (NOT (type? ALTERNATIVE A))
then (ERROR "Improper grammar format"))
(for SYMBOL in (fetch RULE of A)
do (if (NOT (ATOM SYMBOL))
then (ERROR "Improper grammar format"))
(if (NULL (GETHASH SYMBOL TABLE))
then (PUTHASH SYMBOL (QUOTE TERMINAL)
TABLE))]
(PUTHASH (QUOTE EOF)
(QUOTE TERMINAL)
TABLE)
G])
)
[DECLARE: EVAL@COMPILE
(DATATYPE PARSERSPEC (PARSERNAME (* name to be given to parser)
GRAMMAR (* specifies language to be parsed)
READFN (* called to read next token)
CLASSFN (* called to determine class of a token)
INSTANCEFN (* called to determine instance of a token)
EOFFN (* called to verify that token read may be interpreted
as EOF)
STACKINITFN (* initializes empty stacks)
PUSHFN (* push method for stacks)
POPFN (* pop method for stacks)
TOPFN (* tells what's on top of the stack)
LAQUEUEINITFN (* initializes main lookahead queue)
QUEUEINITFN (* initializes empty temp lookahead queue)
ENQUEUEFN (* enqueueing method for queues)
DEQUEUEFN (* dequeueing method for queues)
QUEUENOTEMPTYFN (* tells if queue is empty)
SAVESTATEFN (* bundles up state to be saved)
)
CLASSFN _ (QUOTE SELF)
INSTANCEFN _ (QUOTE SELF)
EOFFN _ (QUOTE STRICTEOF)
STACKINITFN _ (QUOTE NILL)
PUSHFN _ (QUOTE push)
POPFN _ (QUOTE pop)
TOPFN _ (QUOTE CAR)
LAQUEUEINITFN _ (QUOTE SELF)
QUEUEINITFN _ (QUOTE CONS)
ENQUEUEFN _ (QUOTE TCONC)
DEQUEUEFN _ (QUOTE TCONC.FRONT)
QUEUENOTEMPTYFN _ (QUOTE CDR)
SAVESTATEFN _ (QUOTE SELF))
(DATATYPE GRAMMAR (StartSymbol (* the atom which is the start symbol)
PRODUCTIONS (* rules; should be a list of FPRODUCTIONs)
SymbolTable (* hasharray which tells class of symbols)
)
SymbolTable _ (HASHARRAY 33))
]
(/DECLAREDATATYPE (QUOTE PARSERSPEC)
(QUOTE (POINTER POINTER POINTER POINTER POINTER POINTER POINTER POINTER POINTER
POINTER POINTER POINTER POINTER POINTER POINTER POINTER))
(QUOTE ((PARSERSPEC 0 POINTER)
(PARSERSPEC 2 POINTER)
(PARSERSPEC 4 POINTER)
(PARSERSPEC 6 POINTER)
(PARSERSPEC 8 POINTER)
(PARSERSPEC 10 POINTER)
(PARSERSPEC 12 POINTER)
(PARSERSPEC 14 POINTER)
(PARSERSPEC 16 POINTER)
(PARSERSPEC 18 POINTER)
(PARSERSPEC 20 POINTER)
(PARSERSPEC 22 POINTER)
(PARSERSPEC 24 POINTER)
(PARSERSPEC 26 POINTER)
(PARSERSPEC 28 POINTER)
(PARSERSPEC 30 POINTER)))
(QUOTE 32))
(/DECLAREDATATYPE (QUOTE GRAMMAR)
(QUOTE (POINTER POINTER POINTER))
(QUOTE ((GRAMMAR 0 POINTER)
(GRAMMAR 2 POINTER)
(GRAMMAR 4 POINTER)))
(QUOTE 6))
(DECLARE: EVAL@COMPILE DONTCOPY
[DECLARE: EVAL@COMPILE
(RECORD FPRODUCTION (LEFTHAND . ALTERNATIVES)
(* * LEFTHAND is the left-hand side of the rule; ALTERNATIVES is a list of ALTERNATIVEs, which are alternative
right-hand sides)
[TYPE? (AND (LISTP DATUM)
(LITATOM (fetch LEFTHAND of DATUM))
(LISTP (fetch ALTERNATIVES of DATUM])
(RECORD ALTERNATIVE (RULE AUGMENT)
[TYPE? (AND (LISTP DATUM)
(EQ 2 (LENGTH DATUM])
]
)
(DECLARE: EVAL@COMPILE
(PUTPROPS SELF MACRO ((A B)
A))
[PUTPROPS STRICTEOF MACRO ((CLASS)
(EQ CLASS (QUOTE EOF]
[PUTPROPS TCONC.FRONT MACRO (LAMBDA (PTR)
(* Removes and returns the first element of a TCONC list; returns
NIL if empty.)
(PROG1 (CAAR PTR)
(* Return the first element.)
(if (EQ (CAR PTR)
(CDR PTR))
then
(* * If there is only one element in the list, the pointer to the tail of the list must be set to NIL.
If the list is empty, this will be a no-op.)
(RPLACA PTR NIL)
(RPLACD PTR NIL)
else
(* * Otherwise remove the first element of the list (for n elements in list, n>=1.))
(RPLACA PTR (CDAR PTR)))]
)
(* * State machine generation. For an explanation of the parser technology, see
"Theory & Construction of LR(k) Parsers" (Preliminary Version) , Benjamin M. Brosgol, Center
for Research in Computing Technology, Harvard University, March 1973)
(DEFINEQ
(PG.LR0FSM
[LAMBDA (G) (* hts: " 3-Apr-86 16:08")
(* * Builds and returns the "LR(0)-FSM" of grammar G.)
(BLOCK)
(* * First build the "[A]-FSM" for each production in the grammar, and save them in the list NTSL
(whose CAR will be the start state of the root production of the grammar))
(LET ((NTSL (PG.AFSMS G)))
(* * Connect the "[A]-FSMs" together. This is the so-called dotted-linking and -unlinking stage.)
(LET [(M (PG.CONNECT.AFSMS NTSL (fetch (GRAMMAR SymbolTable) of G]
(* * Using the Rabin-Scott algorithm, remove nondeterminism from the FSM.)
(PG.RABIN.SCOTT M])
(PG.AFSMS
[LAMBDA (G) (* hts: " 3-Apr-86 16:08")
(* * Constructs the "[A]-FSM" for each non terminal by linking the productions together as single strand transition
nets. Returns a list of their start states, with the start state of the root production as the first element of
that list.)
(* * Details: There is a single final state, FINAL, that all the "[A]-FSMs" share. The start states should be
accumulated in order so that the "LR(0)-FSM" and machines built from it will be more comprehensible to humans --
hence the use of TCONC. The hasharray mapping NAME.TO.START.STATE makes it faster to find the start state of the
"[A]-FSM" associated with a given nonterminal (faster than looking on the TCONC list). Finally, the "[A]-FSMs" had
better all be deterministic.)
(BLOCK)
(for P in (CONS (PG.ROOTPRODUCTION G)
(fetch PRODUCTIONS of G))
bind START FINAL START.STATES NAME.TO.START.STATE
first (SETQ FINAL (create STATE
NAME _ (QUOTE FINAL)))
(SETQ START.STATES (CONS))
(SETQ NAME.TO.START.STATE (HASHARRAY 33))
do (SETQ START (GETHASH (fetch LEFTHAND of P)
NAME.TO.START.STATE))
(if (NOT (type? STATE START))
then (SETQ START (create STATE
NAME _ (fetch LEFTHAND of P)))
(TCONC START.STATES START)
(PUTHASH (fetch LEFTHAND of P)
START NAME.TO.START.STATE))
(for A in (fetch ALTERNATIVES of P) do (PG.AFSM.ADDPROD
(create PRODUCTION
LHS _ (fetch LEFTHAND
of P)
RHS _ (fetch RULE
of A)
AUGMENT _
(fetch (ALTERNATIVE
AUGMENT)
of A))
START FINAL))
finally (SETQ START.STATES (CAR START.STATES))
(* Get rid of TCONC cell)
(if (NOT (for S in START.STATES always (PG.DETERMINISTIC S)))
then (SHOULDNT "Nondeterminism"))
(if (NEQ (QUOTE START)
(fetch NAME of (CAR START.STATES)))
then (SHOULDNT))
(RETURN START.STATES])
(PG.ROOTPRODUCTION
[LAMBDA (G) (* hts: "28-Feb-86 17:45")
(* * Generates and returns a production rule which can serve as the root production -- it worries with the problem
of EOF as terminating the parse.)
(create FPRODUCTION
LEFTHAND _ (QUOTE START)
ALTERNATIVES _ (LIST (create ALTERNATIVE
RULE _ (LIST (fetch StartSymbol of G)
(QUOTE EOF))
AUGMENT _ NIL])
(PG.AFSM.ADDPROD
[LAMBDA (PROD S FIN) (* hts: " 3-Apr-86 16:08")
(if (NEQ (fetch NAME of S)
(fetch LHS of PROD))
then (\ILLEGAL.ARG S))
(if (NOT (type? STATE FIN))
then (\ILLEGAL.ARG FIN))
(* * Adds the given production to S, creating new states if necessary, finally linking it in to the FINAL state.
Builds new states in such a way as to ensure determinism.)
(BLOCK)
(for TOKEN in (fetch RHS of PROD) bind S2 L S3 first (SETQ S2 S)
do (SETQ L (for L2 in (fetch OUT of S2) thereis (EQ (fetch SYM of L2)
TOKEN)))
(SETQ S2 (if (type? LINK L)
then (CAR (fetch ST of L))
else (SETQ S3 (create STATE))
(PG.CONNECT S2 S3 TOKEN)
S3))
finally (PG.CONNECT S2 FIN PROD))
S])
(PG.CONNECT
[LAMBDA (FROM.STATE TO.STATE TRANSITION.SYMBOL) (* hts: " 3-Apr-86 16:08")
(* * Connects FROM.STATE to TO.STATE with a transition labelled TRANSITION.SYMBOL)
(BLOCK)
(replace OUT of FROM.STATE with (PG.ADD.ARC (fetch OUT of FROM.STATE)
TO.STATE TRANSITION.SYMBOL])
(PG.ADD.ARC
[LAMBDA (LINKS TO.STATE TRANSITION.SYMBOL) (* hts: "28-Feb-86 14:48")
(* * Augments the set of transitions LINKS by adding a transition via TRANSITION.SYMBOL to TO.STATE %.
Returns the augmented transition sets. Note: tries to keep them in order of acquisition)
(LET [(TRANSITION (for LINK in LINKS thereis (EQ (fetch SYM of LINK)
TRANSITION.SYMBOL]
(if (type? LINK TRANSITION)
then (if (FMEMB TO.STATE (fetch ST of TRANSITION))
then (fetch ST of TRANSITION)
else (push (fetch ST of TRANSITION)
TO.STATE))
LINKS
else (CONS (create LINK
SYM _ TRANSITION.SYMBOL
ST _ (LIST TO.STATE))
LINKS))])
(PG.DETERMINISTIC
[LAMBDA (S) (* hts: " 3-Apr-86 16:08")
(if (type? FSM S)
then (SETQ S (fetch START of S)))
(* * Returns T if S is a deterministic FSM, NIL otherwise. For a state to have deterministic transitions to the
next state, (a) there may not be more than one arc out with the same transition symbol, and
(b) there may not be more than one state reachable by any transition symbol. (* * Details: HASHARRAY glop is to
ensure you don't get into infinite loops because of cycles in the FSM. Could have implemented this by calling
STATES, but that would CONS a lot more and require essentially two traversals instead of one.
Recursion shouldn't hurt because these FSMs are more bushy than they are deep.))
(BLOCK)
(PG.DETERMINISTIC1 S (HASHARRAY 33])
(PG.DETERMINISTIC1
[LAMBDA (S DONE) (* hts: " 3-Apr-86 16:08")
(* * Tells whether the submachine beginning at S is deterministic. See comments in DETERMINISTIC.)
(OR (GETHASH S DONE)
(PROGN (PUTHASH S T DONE)
(for ARC in (fetch OUT of S) bind (SYMBOLS _ NIL)
always (AND (PROG1 (NOT (FMEMB (fetch SYM of ARC)
SYMBOLS))
(push SYMBOLS (fetch SYM of ARC)))
(PG.SINGLETON (fetch ST of ARC))
(PG.DETERMINISTIC1 (CAR (fetch ST of ARC))
DONE])
(PG.CONNECT.AFSMS
[LAMBDA (NTSL SYMBOL.TABLE) (* hts: " 3-Apr-86 17:46")
(* * Interconnects all the "[A]-FSMs" This is the dotted linking and unlinking phase. NTSL is a list of start
states of the various "[A]-FSMs", with the overall start state first. SYMBOL.TABLE maps symbols onto {TERMINAL,
NONTERMINAL}.)
(LET [(START (HASHARRAY (LENGTH NTSL)))
(DONE (HASHARRAY (TIMES 5 (LENGTH NTSL]
(* * Build a hasharray which implements a fast mapping: name of the nonterminal to which an "[A]-FSM" reduces ->
the start state of that "[A]-FSM")
(for S in NTSL do (PUTHASH (fetch NAME of S)
S START))
(* * Absorb to each state all the links of any machine which reduces to a nonterminal which the state is supposed
to read)
(for S in (PG.STATES NTSL) do (PG.CONNECT.AFSMS1 S (LIST S)
SYMBOL.TABLE START DONE))
(* * Check to make sure everything got connected up properly.)
(PG.CONNECTED NTSL SYMBOL.TABLE START))
(* * Start state better be first in the list)
(if (NEQ (QUOTE START)
(fetch NAME of (CAR NTSL)))
then (SHOULDNT "Start state in wrong place"))
(* * Return the fully-connected state machine)
(create FSM
START _ (CAR NTSL)
SymbolTable _ SYMBOL.TABLE])
(PG.CONNECT.AFSMS1
[LAMBDA (S PATH SYMBOL.TABLE START DONE) (* hts: " 4-Apr-86 16:29")
(* * For each transition out of S, if it is a transition on a nonterminal (say A) and is not immediately reentrant
(ie, the first state of the "[A]-FSM" for A -> Ax for some x), then S inherits all the out-links of the "[A]-FSM"
(for A). The inheritance is done depth-first, since the "[A]-FSM" may have a first transition on a nonterminal, and
so may need to inherit some transitions itself. When a state has been completed, it is entered in the hashtable
DONE; if a state is already in this table, it can have links copied from it directly, and does not have its
inheritance checked again. Of course, if there are cyclically left-recursive rules (eg, A -> Bx, B -> Ay) then this
depth first search could result in a cycle. To avoid this, the routine remembers and checks its current search
chain. If a cycle is detected, it is returned and not dealt with. If the caller's state is not involved in the
cycle, he deals with it (see CONNECT.CYCLES); else he returns it to his caller.)
[if (NOT (GETHASH S DONE))
then (for TRANSITION in (fetch OUT of S) bind SYMBOL AFSM CYCLE NONTERMINAL
(COMPLETED _ T)
do (SETQ SYMBOL (fetch SYM of TRANSITION))
(SETQ NONTERMINAL (PG.NONTERMINALP SYMBOL SYMBOL.TABLE))
(SETQ CYCLE (FMEMB SYMBOL PATH))
(SETQ AFSM (GETHASH SYMBOL START))
(if (AND NONTERMINAL (NOT CYCLE)
(NEQ S AFSM))
then (PG.CONNECT.AFSMS1 AFSM (CONS SYMBOL PATH)
SYMBOL.TABLE START DONE)
(if (NOT (GETHASH AFSM DONE))
then (SETQ COMPLETED NIL))
[for TRANSITION2 in (fetch OUT of AFSM)
do (for S2 in (fetch ST of TRANSITION2)
do (PG.CONNECT S S2 (fetch SYM of TRANSITION2]
elseif (AND NONTERMINAL CYCLE (NEQ S AFSM))
then (SETQ COMPLETED NIL))
finally (if COMPLETED
then (PUTHASH S T DONE]])
(PG.SINGLETON
[LAMBDA (LST) (* hts: "16-Feb-86 13:48")
(* * Returns T if LST is a singleton list, NIL otherwise.)
(AND (LISTP LST)
(NOT (CDR LST])
(PG.CONNECTED
[LAMBDA (NTSL SYMBOL.TABLE START) (* hts: " 3-Apr-86 16:09")
(* * Checks to see if CONNECT.AFSMS has done its work correctly. calls SHOULDNT iff it hasnt.)
(for S in (PG.STATES NTSL) do (for L in (fetch OUT of S)
do (if (PG.NONTERMINALP (fetch SYM of L)
SYMBOL.TABLE)
then (OR (PG.OWNS.LINKS
S
(GETHASH (fetch SYM
of L)
START))
(SHOULDNT "Ownership fault")))])
(PG.OWNS.LINKS
[LAMBDA (S1 S2) (* hts: "21-Feb-86 17:10")
(* * Returns non-NIL iff S1s transitions are a superset of S2s.)
(BLOCK)
(for L2 in (fetch OUT of S2) always (for NEXT2 in (fetch ST of L2)
always (for L1 in (fetch OUT
of S1)
thereis
(AND (EQ (fetch SYM
of L2)
(fetch SYM
of L1))
(for NEXT1
in (fetch ST
of L1)
thereis (EQ NEXT1
NEXT2])
(PG.RABIN.SCOTT
[LAMBDA (M) (* hts: " 3-Apr-86 16:08")
(* * Rabin-Scott algorithm for making deterministic a FSM. Returns the determinized FSM.)
(PG.RABIN.SCOTT1 (fetch START of M)
(HASHARRAY 33)
(HASHARRAY 33))
(if (NOT (PG.DETERMINISTIC M))
then (SHOULDNT "Nondeterminism"))
M])
(PG.RABIN.SCOTT1
[LAMBDA (S STATES DONE) (* hts: " 3-Apr-86 16:08")
(* * Does the actual work of the Rabin-Scott determinizing algorithm. Tail recursively enumerates the states of the
machine (including replacement states). Recursion should be ok here because FSMs should be more bushy than deep.
Basically, if S has transitions to two or more states S1, ..., Sn on the same symbol, replaces those transitions
with a single transition to a union state (which has all the out transitions of S1 thru Sn). DONE hashtable has an
entry for each state visited, so that you can detect cycles and not go catatonic over them.
STATES hashtable maps replacement state names onto union states; this is used to avoid unnecessary duplication of
union states)
(LET ((S.NAME (fetch NAME of S)))
(if (GETHASH S.NAME DONE)
then S
else (PUTHASH S.NAME T DONE)
(for SYM in (PG.TRANSITION.SYMBOLS S) bind NEXT PG.REPLACEMENT
do (SETQ NEXT (PG.NEXT.STATES S SYM))
(SETQ PG.REPLACEMENT (PG.REPLACEMENT NEXT STATES))
(if PG.REPLACEMENT
then (for N inside NEXT do (PG.DISCONNECT S N SYM))
(PG.CONNECT S PG.REPLACEMENT SYM)))
(for L in (fetch OUT of S)
do (OR (PG.SINGLETON (fetch ST of L))
(SHOULDNT "Nondeterministic state"))
(PG.RABIN.SCOTT1 (CAR (fetch ST of L))
STATES DONE))
S)])
(PG.REPLACEMENT
[LAMBDA (S STATES) (* hts: " 3-Apr-86 16:08")
(* * Finds or generates the union state (if any) for the list of states S.)
(if (AND (LISTP S)
(CDR S))
then (LET* ((NAME (PG.COMPOSITE.STATE.NAME S))
(PG.REPLACEMENT (GETHASH NAME STATES)))
(if (NOT (type? STATE PG.REPLACEMENT))
then (SETQ PG.REPLACEMENT (create STATE
NAME _ NAME))
(for SYM in (PG.TRANSITION.SYMBOLS S)
do (for NEXT inside (PG.NEXT.STATES S SYM)
do (PG.CONNECT PG.REPLACEMENT NEXT SYM)))
(PUTHASH NAME PG.REPLACEMENT STATES))
PG.REPLACEMENT)
else NIL)])
(PG.TRANSITION.SYMBOLS
[LAMBDA (STATESET) (* hts: "25-Feb-86 23:19")
(* * Finds all the transition symbols that come from any of the states in STATESET)
(LET ((SYMS (CONS)))
[for S inside STATESET do (for L in (fetch OUT of S)
do (if (NOT (FMEMB (fetch SYM of L)
(CAR SYMS)))
then (TCONC SYMS (fetch SYM of L)))]
(CAR SYMS])
(PG.NEXT.STATES
[LAMBDA (STATESET SYMBOL) (* hts: " 3-Apr-86 16:08")
(LET [(NEXT (for S inside STATESET bind TRANSITION
when [type? LINK (SETQ TRANSITION (for L in (fetch OUT of S)
thereis (EQ SYMBOL
(fetch SYM
of L]
join (COPY (fetch ST of TRANSITION]
(if (PG.SINGLETON NEXT)
then (CAR NEXT)
else NEXT)])
(PG.COMPOSITE.STATE.NAME
[LAMBDA (STATESET) (* hts: "25-Feb-86 23:15")
(* * Gives a unique name to the set of states STATESET)
[if (NLISTP STATESET)
then (fetch NAME of STATESET)
else (PACK (SORT (for S in STATESET collect (fetch NAME of S))
(FUNCTION ALPHORDER]])
(PG.DISCONNECT
[LAMBDA (A B TRANSITION.SYMBOL) (* hts: " 3-Apr-86 16:08")
(* * Breaks the link between states A and B along TRANSITION.SYMBOL)
(BLOCK)
(LET [(TRANSITION (for LINK in (fetch OUT of A) thereis (EQ TRANSITION.SYMBOL
(fetch SYM
of LINK]
[if (type? LINK TRANSITION)
then (if (PG.SINGLETON (fetch ST of TRANSITION))
then (* this is the only transition that is keeping the
link alive, so kill the link)
(replace OUT of A with (DREMOVE TRANSITION
(fetch OUT of A)))
else (* chop B out of the link)
(replace ST of TRANSITION with (DREMOVE B (fetch ST
of TRANSITION]
(QUOTE DISCONNECTED])
(PG.NONTERMINALP
[LAMBDA (SYM SYMBOL.TABLE) (* hts: " 1-Mar-86 17:06")
(* * Returns T if SYM is a nonterminal symbol according to SYMBOL.TABLE , which was built from the current grammar)
(EQ (GETHASH SYM SYMBOL.TABLE)
(QUOTE NONTERMINAL])
(PG.TERMINALP
[LAMBDA (SYM SYMBOL.TABLE) (* hts: "24-Feb-86 16:28")
(* * Determines if SYM is a terminal for the current grammar. NIL is a terminal for all grammars.)
(OR (EQ (GETHASH SYM SYMBOL.TABLE)
(QUOTE TERMINAL))
(NULL SYM])
)
(DEFINEQ
(PG.LALRKFSM
[LAMBDA (FSM) (* hts: " 3-Apr-86 16:09")
(* Transforms an "LR(0)-FSM" into a "LALR(K)-FSM" by adding lookahead states to resolve inadequate states of the
"LR(0)-FSM". Also builds inverse transition links; the backlinks, in addition to helping determine the lookahead
sets, help in building the actual parser (because you have to backtrack states on reduction))
(LET ((PG.STATES (PG.STATES FSM))
(SYMBOL.TABLE (fetch (FSM SymbolTable) of FSM)))
(* * Build inverse transition links.)
(PG.BUILD.BackLinks PG.STATES)
(* * Resolve all inadequate states by adding lookahead states.)
(for S in PG.STATES do (SELECTQ (PG.STATETYPE S SYMBOL.TABLE)
(INADEQUATE (PG.PRINT.INADEQUATE S SYMBOL.TABLE)
(PG.RESOLVE S SYMBOL.TABLE))
((READ REDUCE LOOKAHEAD)
NIL)
(NIL (if (NEQ (fetch NAME of S)
(QUOTE FINAL))
then (SHOULDNT "Null state type")))
(SHOULDNT "Queer state type")))
(* * Make sure there are no remaining inadequate states.)
(if (for S in (PG.STATES FSM) thereis (EQ (PG.STATETYPE S SYMBOL.TABLE)
(QUOTE INADEQUATE)))
then (SHOULDNT "Inadequate states remaining")))
FSM])
(PG.BUILD.BackLinks
[LAMBDA (STATES) (* hts: " 3-Apr-86 16:08")
(* * Puts backward links on each of the state S in FSM, showing which from which states you could have arrived at S
and by what token transition.)
[for S1 in STATES do (for L in (fetch OUT of S1)
do (for S2 in (fetch ST of L)
do (PG.CONNECT.BACK S2 S1 (fetch SYM of L]
(QUOTE BACKLINKED])
(PG.CONNECT.BACK
[LAMBDA (FROM.STATE TO.STATE TRANSITION.SYMBOL) (* hts: " 3-Apr-86 16:08")
(* * Connects FROM.STATE to TO.STATE with a backwards transition labelled TRANSITION.SYMBOL)
(BLOCK)
(replace BackLinks of FROM.STATE with (PG.ADD.ARC (fetch BackLinks of FROM.STATE)
TO.STATE TRANSITION.SYMBOL))
(QUOTE CONNECTED-BACKWARDS])
(PG.STATETYPE
[LAMBDA (S SYMBOL.TABLE) (* hts: " 3-Apr-86 16:09" posted: "20-MAY-77 23:03")
(* * Examines a state and determines its type for the parser.)
(for L in (fetch OUT of S) bind (TYPE _ NIL) do [COND
((PG.LOOKAHEADP L)
(SELECTQ TYPE
((NIL LOOKAHEAD)
(SETQQ TYPE
LOOKAHEAD))
(SHOULDNT)))
((type? PRODUCTION
(fetch SYM
of L))
(SELECTQ TYPE
((READ REDUCE
INADEQUATE)
(SETQQ TYPE
INADEQUATE))
(NIL (SETQQ TYPE
REDUCE))
(SHOULDNT)))
((PG.TERMINALP (fetch SYM
of L)
SYMBOL.TABLE)
(SELECTQ TYPE
((READ NIL)
(SETQQ TYPE READ))
((REDUCE INADEQUATE)
(SETQQ TYPE
INADEQUATE))
(SHOULDNT]
finally (RETURN TYPE])
(PG.RESOLVE
[LAMBDA (S SYMBOL.TABLE) (* hts: "16-Apr-86 15:26")
(* * Attempts to resolve inadequacy in a state. Note that if the language is not "LALR(k)" for any k, this routine
will go into an infinite loop; but it will announce its progress to the user so he can (a) stop it if it looks
runaway, and (b) know what the conflict is.)
(LET ((LOOKAHEAD (PG.BUILD.LOOKAHEAD.SETS S SYMBOL.TABLE)))
(for LOOK in LOOKAHEAD bind EXTRA.STATE LINK SYMBOL DEST LLA
do (SETQ LINK (CAR LOOK))
(SETQ SYMBOL (fetch SYM of LINK))
(SETQ DEST (CAR (fetch ST of LINK)))
(SETQ LLA (CDR LOOK))
(SETQ EXTRA.STATE (create STATE))
(PG.CONNECT EXTRA.STATE DEST SYMBOL)
(for SYMS in LLA do (PG.CONNECT S EXTRA.STATE SYMS))
(replace BackLinks of EXTRA.STATE with S)
(PG.DISCONNECT S DEST SYMBOL)))
(QUOTE RESOLVED])
(PG.PRINT.INADEQUATE
[LAMBDA (S SYMBOL.TABLE) (* hts: " 3-Apr-86 16:08")
(* * Prints out information about the inadequate state about to be resolved. Tells what transitions are possible
from this state, thus showing the conflict. Should the parser generator enter an infinite loop trying to resolve
this state (ie, if the language is not "LALR(k)" for any k), this will help the user find the rules in his grammar
which are responsible for the problem.)
(PRINTOUT NIL "Adding lookahead to resolve conflict in state " (fetch NAME of S)
":" T)
(for L in (fetch OUT of S) do (LET ((SYM (fetch SYM of L)))
(if (PG.TERMINALP SYM SYMBOL.TABLE)
then (PRINTOUT NIL " Read: " SYM T)
elseif (type? PRODUCTION SYM)
then (PRINTOUT NIL " Reduce: "
(fetch LHS of SYM)
" -> ")
(for THING
in (fetch RHS of SYM)
do (PRINTOUT NIL THING " "))
(PRINTOUT NIL T))])
(PG.BUILD.LOOKAHEAD.SETS
[LAMBDA (S SYMBOL.TABLE) (* hts: "16-Apr-86 15:26")
(* * Attempts to resolve inadequacy in a state. Note that if the language is not "LALR(k)" for any k, this routine
will go into an infinite loop; but it will announce its progress to the user so he can (a) stop it if it looks
runaway, and (b) know what the conflict is. If possible, returns a list of things of the form
(link %. set of lookahead strings) such that each link from S is given a set of lookahead strings disjoint from any
belonging to another of S's links.)
(for K from 1 bind LLAS
do
(* * Tell user how deep you're going, so he can detect runaway (in case the grammar he gave is more complex than he
expected))
(PRINTOUT NIL " " K "-level lookahead from state " (fetch NAME of S)
T)
[SETQ LLAS (for L in (fetch OUT of S)
when (NOT (PG.NONTERMINALP (fetch SYM of L)
SYMBOL.TABLE))
collect (CONS L (PG.LLA K (LIST S)
L SYMBOL.TABLE NIL]
(if (PG.DISJOINT LLAS)
then (RETURN LLAS))])
(PG.DISJOINT
[LAMBDA (LLASET) (* hts: " 1-Mar-86 21:59")
(* * Tells whether the lookahead sets in LLASET are all disjoint. Note that entries on LLASET are of the form
(link . set-of-lookahead-chars))
(for L on LLASET bind L1
never (SETQ L1 (CAR L))
(for L2 in (CDR L) thereis (INTERSECTION (CDR L1)
(CDR L2])
(PG.LLA
[LAMBDA (K PATH LINK SYMBOL.TABLE ALREADY) (* hts: " 3-Apr-86 16:09")
(* * Finds the level-K set of lookahead symbols generated by looking along the state path PATH
(which is backwards). This local lookahead set is used to build the next level of lookahead states to resolve an
inadequate state. ALREADY is a set of states already visited, and it is intended to prevent infinite recursion
because of cycles.)
(COND
((LEQ K 0)
(* * The lookahead set for lookahead strings of length 0 is the set containing the empty string.
This stops the recursion.)
(LIST NIL))
((LISTP (fetch SYM of LINK))
(PG.LLA.LOOKAHEAD K PATH LINK SYMBOL.TABLE ALREADY))
((PG.TERMINALP (fetch SYM of LINK)
SYMBOL.TABLE)
(PG.LLA.READ K PATH LINK SYMBOL.TABLE ALREADY))
((type? PRODUCTION (fetch SYM of LINK))
(PG.LLA.REDUCE K PATH LINK SYMBOL.TABLE ALREADY))
(T (\ILLEGAL.ARG LINK])
(PG.LLA.LOOKAHEAD
[LAMBDA (K PATH LINK SYMBOL.TABLE ALREADY) (* hts: " 3-Apr-86 16:08")
(* * Lookahead transition. Skip over it.)
(LET* [(NEXT.STATE (CAR (fetch ST of LINK)))
(NEXT.LOOKAHEAD (for L in (fetch OUT of NEXT.STATE)
when (NOT (PG.NONTERMINALP (fetch SYM of L)
SYMBOL.TABLE))
join (PG.LLA K PATH L SYMBOL.TABLE ALREADY]
(INTERSECTION NEXT.LOOKAHEAD NEXT.LOOKAHEAD])
(PG.LLA.READ
[LAMBDA (K PATH LINK SYMBOL.TABLE ALREADY) (* hts: " 3-Apr-86 16:08")
(* * Read transition. Current lookahead symbol, obviously, is the transition symbol of the current link.
Recurse to find deeper lookahead. (Except if current symbol is EOF, in which case all deeper lookahead must be EOF
also, by definition of LLA.))
(if (EQ (QUOTE EOF)
(fetch SYM of LINK))
then (LIST (to K collect (QUOTE EOF)))
else (LET* [(NEXT.STATE (CAR (fetch ST of LINK)))
(NEXT.LOOKAHEAD (for L in (fetch OUT of NEXT.STATE)
when (NOT (PG.NONTERMINALP (fetch SYM of L)
SYMBOL.TABLE))
join (for LOOK in (PG.LLA (SUB1 K)
(CONS NEXT.STATE PATH)
L SYMBOL.TABLE ALREADY)
collect (CONS (fetch SYM of LINK)
(COPY LOOK]
(INTERSECTION NEXT.LOOKAHEAD NEXT.LOOKAHEAD)))])
(PG.LLA.REDUCE
[LAMBDA (K PATH LINK SYMBOL.TABLE ALREADY) (* hts: " 3-Apr-86 16:09")
(* * Reduce transition: Back up along symbols of right-hand side of reduction rule %. Then follow the symbol of the
left hand side to get to another state. Now we've essentially performed the reduction and got back in the FSM to
where we would have been if we had just read the nonterminal symbol (if such were possible). Lookahead symbol
search can proceed (recursively) from there.)
(LET* [(PRODUCTION (fetch SYM of LINK))
(LHS (fetch LHS of PRODUCTION))
(RHS (fetch RHS of PRODUCTION))
(PG.BACKUP (PG.BACKUP PATH (REVERSE RHS)))
(SHORTENED.PATH (FNTH PATH (PLUS 2 (LENGTH RHS]
(LET [(LOOK (for S in PG.BACKUP
join (LET ((TARGET (PG.REDUCE.TARGET S LHS)))
[if (FMEMB TARGET ALREADY)
then NIL
else (for L in (fetch OUT of TARGET)
when (NOT (PG.NONTERMINALP (fetch SYM
of L)
SYMBOL.TABLE))
join (PG.LLA K (CONS TARGET (CONS S
SHORTENED.PATH))
L SYMBOL.TABLE (CONS TARGET
ALREADY]]
(INTERSECTION LOOK LOOK])
(PG.BACKUP
[LAMBDA (STATE.PATH SYMBOL.PATH) (* hts: "17-Apr-86 16:25" posted: "23-JUN-77 13:45")
(* * Designates the set of states along PATH from which you could have reached (CAR PATH) by reading the symbols in
SYMBOL.PATH. STATE.PATH is a (perhaps incomplete) path of states along which to back up; (CAR STATE.PATH) is the
state from which to start backing. SYMBOL.PATH is the list of symbols along which to back up
(empty in the case of rules like A -> e).)
(SETQ STATE.PATH (for S in STATE.PATH collect (PG.LOOKAHEAD.SOURCE S)))
(* * Back up as far as you are fully constrained by the symbol path provided.)
(while (AND SYMBOL.PATH (CDR STATE.PATH))
do (if [NOT (for LNK in (fetch BackLinks of (CAR STATE.PATH))
thereis (AND (EQ (fetch SYM of LNK)
(CAR SYMBOL.PATH))
(FMEMB (CADR STATE.PATH)
(fetch ST of LNK]
then (SHOULDNT))
(SETQ STATE.PATH (CDR STATE.PATH))
(SETQ SYMBOL.PATH (CDR SYMBOL.PATH)))
(* * Back up the rest of the way (if any). This may produce some fanning out.)
(bind (STATE.SET _ (LIST (CAR STATE.PATH))) while SYMBOL.PATH
do (for S in STATE.SET bind (STATES _ NIL)
do (for PREV in [fetch ST of (for L in (fetch BackLinks of S)
thereis (EQ (fetch SYM of L)
(CAR SYMBOL.PATH]
do (if (NOT (FMEMB PREV STATES))
then (SETQ STATES (NCONC1 STATES PREV))) finally (SETQ
STATE.SET
STATES)))
(SETQ SYMBOL.PATH (CDR SYMBOL.PATH))
finally (RETURN STATE.SET])
(PG.LOOKAHEAD.SOURCE
[LAMBDA (S) (* hts: "20-Feb-86 11:53")
(* * If S was generated to do lookahead, you really want to start backign up from the state that S came from.
States generated to do lookahead always have their source (the state from which they do lookahead) in their
BackLinks field; other states have lists of states in their BackLinks field.)
(if (type? STATE (fetch BackLinks of S))
then (fetch BackLinks of S)
else S)])
(PG.REDUCE.TARGET
[LAMBDA (STATE NONTERMINAL) (* hts: "24-Feb-86 18:59")
(* * Returns the state connected by a transition from STATE along the nonterminal symbol NONTERMINAL)
(CAR (fetch ST of (for LNK in (fetch OUT of STATE)
thereis (EQ NONTERMINAL (fetch SYM of LNK])
(PG.LOOKAHEADP
[LAMBDA (L) (* hts: "24-Feb-86 15:53")
(* * Tells whether the given link is a lookahead link. Ordinary backlinks fields contain lists of links;
states generated by lookahead have just a state as their backlink: the state from which the lookahead was
generated.)
(type? STATE (fetch BackLinks of (CAR (fetch ST of L])
)
(DEFINEQ
(PG.OLD.LLA
[LAMBDA (K PATH LINK FIRST.LOOKAHEAD.SYMBOLS SYMBOL.TABLE ALREADY)
(* hts: " 3-Apr-86 16:09")