summaryrefslogtreecommitdiffstats
path: root/taccle/taccle.tcl
blob: 8ed3639809eb818b0126d30bb34d3228f9c2f1eb (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
#!/usr/bin/tclsh

# $Id: taccle.tcl,v 1.6 2005/03/17 20:42:21 tang Exp $

set TACCLE_VERSION 1.2

# no yydebug
# no YYDEBUG
# no yyerrok
# no YYERROR
# no YYRECOVERING

#//#
# Taccle is another compiler compiler written in pure Tcl.  reads a
# <em>taccle specification file</em> to generate pure Tcl code that
# implements an LALR(1) parser.  See the {@link README} file for
# complete instructions.  Additional information may be found at
# {@link http://mini.net/tcl/taccle}.
#
# @author Jason Tang (tang@jtang.org)
#//#

# Process a definition on a single line, either a literal block or a
# <code>%</code> directive.
#
# @param line text of a definition
proc handle_defs {line} {
    # trim whitespace and remove any comments
    set line [strip_comments [string trim $line]]
    if {$line == ""} {
        return
    }
    if {$line == "%\{"} {
        handle_literal_block
    } else {
        # extract the keyword to the left of the first space and the
        # arguments (if any) to the right
        if {[regexp -line {^(\S+)\s+(.*)} $line foo keyword args] == 0} {
            set keyword $line
            set args ""
        }
        switch -- $keyword {
            "%token" {
                foreach token_name [split $args] {
                    if {$token_name != ""} {
                        # add the terminal token to the table
                        add_token $token_name $::TERMINAL 0 0 nonassoc
                    }
                }
            }
            "%left" -
            "%right" -
            "%nonassoc" {
                handle_precedence $::next_precedence [string range $keyword 1 end] $args
                incr ::next_precedence
            }
            "%start" {
                if {$args == ""} {
                    taccle_error "Must supply a token with %start" $::PARAM_ERROR
                }
                set ::start_symbol $args
            }
            default {
                taccle_error "Unknown declaration \"$keyword\"" $::SYNTAX_ERROR
            }
        }
    }
}

# Start reading from the source file and copy everything between ^%\{$
# to ^%\}$ to the destination file.
proc handle_literal_block {} {
    set end_defs 0
    set lines_in_block 0
    while {$end_defs == 0} {
        if {[gets $::src line] < 0} {
            taccle_error "No terminator to verbatim section found " $::SYNTAX_ERROR
        } elseif {[string trim $line] == "%\}"} {
            set end_defs 1
        } else {
            puts $::dest $line
        }
        incr lines_in_block
    }
    incr ::line_count $lines_in_block
}

# Assigns operator precedence to each token in $tokens.  Adds the
# token as a TERMINAL to the token table.
#
# @param level integer value for token precedence
# @param direction direction of precedence, either <var>left</var>,
# <var>right</var>, or <var>nonassoc</var>
# @param tokens list of terminals to which assign precedence
proc handle_precedence {level direction tokens} {
    foreach token $tokens {
        if {[regexp -- {\A\'(.)\'\Z} $token foo c]} {
            add_token $c $::TERMINAL 1 $level $direction
        } else {
            add_token $token $::TERMINAL 0 $level $direction
        }
    }
}

# The nine steps to actually building a parser, given a string buffer
# containing all of the rules.
#
# @param rules_buf a very large string consisting of all of the
# grammar's rules
proc build_parser {rules_buf} {
    # setp 0: parse the entire rules buffer into separate productions
    handle_rules_buf $rules_buf

    # step 1: rewrite the grammar, then augment it
    rewrite_grammar

    # step 2: determine which non-terminals are nullable
    generate_nullable_table
    
    # step 3: generate FIRST table for each element in the token table
    generate_first_table

    # step 4: now generate FOLLOW table for each element
    generate_follow_table

    # step 5: build canonical LR(1) table
    generate_lr1

    # step 6: combine cores into LALR(1) table
    generate_lalr1

    # step 7: wherever there exists a shift/reduce conflict, choose to
    # reduce wherever the precedence table dictates such
    resolve_precedences

    # step 8: check for infinite recursions
    check_recursions
    
    # step 9: finally take LALR(1) table and generate a state
    # transition matrix
    generate_lalr1_parse_table
}

# Parses the rules buffer, extracting each rule and adding
# pseudo-rules wherever embedded actions exist.
#
# @param rules_buf remaining rules to handle
proc handle_rules_buf {rules_buf} {
    # counts number of rules in the grammar
    # rule number 0 is reserved for the special augmentation S' -> S
    set ::rule_count 1
    set prev_lhs ""

    # keep track of pseudo-rules (used for embedded actions)
    set pseudo_count 1

    # add the special end marker
    set ::token_table("\$",t) $::TERMINAL
    set ::token_table("\$") 0
    set ::token_id_table(0) "\$"
    set ::token_id_table(0,t) $::TERMINAL
    set ::prec_table(0) 0
    set ::prec_table(0,dir) nonassoc

    # add the special error token
    add_token error $::TERMINAL 1 0 nonassoc

    while {[string length $rules_buf] > 0} {
        # consume blank lines
        if {[regexp -line -- {\A([[:blank:]]*\n)} $rules_buf foo blanks]} {
            set rules_buf [string range $rules_buf [string length $blanks] end]
            incr ::line_count
            continue
        }
        # extract left hand side
        if {[regexp -line -- {\A\s*(\w+)\s*:} $rules_buf foo lhs]} {
            add_token $lhs $::NONTERMINAL 0 0 nonassoc
            set prev_lhs $lhs
        } elseif {[regexp -line -- {\A\s*\|} $rules_buf foo]} {
            if {$prev_lhs == ""} {
                taccle_error "No previously declared left hand side" $::SYNTAX_ERROR
            }
            set lhs $prev_lhs
        } elseif {[regexp -line -- {\A\s*\Z} $rules_buf]} {
            # only whitespace left
            break
        } else {
            taccle_error "No left hand side found" $::SYNTAX_ERROR
        }
        set rules_buf [string range $rules_buf [string length $foo] end]
        
        # read the rule derivation, which is everything up to a bar or
        # semicolon
        set rhs ""
        set action ""
        set done_deriv 0
        set num_lines 0
        while {$rules_buf != "" && $done_deriv != 1} {
            switch -- [string index $rules_buf 0] {
                |    { set done_deriv 1 }
                ;    {
                    set done_deriv 1
                    set prev_lhs ""
                    set rules_buf [string range $rules_buf 1 end]
                }
                "\n" {
                    incr num_lines
                    append rhs " "
                    set rules_buf [string range $rules_buf 1 end]
                }
                '  {
                    append rhs [string range $rules_buf 0 2]
                    set rules_buf [string range $rules_buf 3 end]
                }
                \{   {
                    # keep scanning until end of action found
                    set a ""
                    set rp 1
                    set found_end 0
                    while {!$found_end && $rp < [string length $rules_buf]} {
                        set c [string index $rules_buf $rp]
                        if {$c == "\}"} {
                            if {[info complete $a]} {
                                set found_end 1
                            } else {
                                append a "\}"
                            }
                        } elseif {$c == "\n"} {
                            append a $c
                            incr num_lines
                        } else {
                            append a $c
                        }
                        incr rp
                    }
                    if {!$found_end} {
                        taccle_error "Unmatched `\{'" $::SYNTAX_ERROR
                    }
                    set action $a
                    set rules_buf [string range $rules_buf $rp end]
                }
                default {
                    set c [string index $rules_buf 0]
                    if {$action != "" && ![string is space $c]} {
                        # embedded action found; add a special rule for it
                        set pseudo_name "@PSEUDO$pseudo_count"
                        add_token $pseudo_name $::NONTERMINAL 0 0 nonassoc
                        set ::rule_table($::rule_count,l) $pseudo_name
                        set ::rule_table($::rule_count,d) ""
                        set ::rule_table($::rule_count,dc) 0
                        set ::rule_table($::rule_count,a) $action
                        set ::rule_table($::rule_count,e) 0
                        set ::rule_table($::rule_count,line) $::line_count
                        append rhs "$pseudo_name "
                        set action ""
                        incr pseudo_count
                        incr ::rule_count
                    } else {
                        append rhs $c
                        set rules_buf [string range $rules_buf 1 end]
                    }
                }
            }
        }
        if {$rules_buf == "" && $done_deriv == 0} {
            taccle_error "Rule does not terminate" $::SYNTAX_ERROR
        }
        set derivation [string trim $rhs]
        set deriv_list ""
        set deriv_count 0
        set prec_next 0
        foreach token [split $derivation] {
            if {$prec_next} {
                # check that argument to %prec is a terminal symbol
                if {![info exists ::token_table($token)] || \
                        $::token_table($token,t) != $::TERMINAL} {
                    taccle_error "Argument to %prec is not a terminal symbol" $::GRAMMAR_ERROR
                }
                set ::rule_table($::rule_count,prec) $::token_table($token)
                set prec_next 0
                continue
            }
            if {$token == "%prec"} {
                set prec_next 1
                continue
            }
            if {[regexp -- {\A\'(.)\'\Z} $token foo c]} {
                add_token $c $::TERMINAL 1 0 nonassoc
                set token $c
            }
            if {$token != ""} {
                if {[string range $token 0 6] == "@PSEUDO"} {
                    set ::rule_table([expr {$::rule_count - 1}],e) $deriv_count
                }
                
                lappend deriv_list $token
                incr deriv_count
            }
        }
        if {$prec_next} {
            taccle_error "%prec modifier has no associated terminal symbol" $::PARAM_ERROR
        }
        incr ::line_count $num_lines
        set ::rule_table($::rule_count,l) $lhs
        set ::rule_table($::rule_count,d) $deriv_list
        set ::rule_table($::rule_count,dc) [llength $deriv_list]
        set ::rule_table($::rule_count,a) $action
        set ::rule_table($::rule_count,line) $::line_count
        incr ::rule_count
    }
}

# Post-process the grammar by augmenting it and and replacing all
# tokens with their id values.
proc rewrite_grammar {} {
    set ::rule_table(0,l) "start'"    
    if {[info exists ::start_symbol]} {
        if {![info exists ::token_table($::start_symbol)]} {
            taccle_error "Token given by %start does not exist" $::PARAM_ERROR
        }
        if {$::token_table($::start_symbol,t) == $::TERMINAL} {
            taccle_error "Token given by %start is a terminal." $::PARAM_ERROR
        }
        set ::rule_table(0,d) $::start_symbol
    } else {
        set ::rule_table(0,d) $::rule_table(1,l)
    }
    set ::rule_table(0,dc) 1
    set ::rule_table(0,prec) 0
    set ::start_token_id [add_token "start'" $::NONTERMINAL 0 0 nonassoc]
    set ::token_list [lsort -command tokid_compare $::token_list]

    # now go through grammar and replace all token names with their id
    # number
    for {set i 0} {$i < $::rule_count} {incr i} {
        set ::rule_table($i,l) $::token_table($::rule_table($i,l))
        set new_deriv_list ""
        foreach deriv $::rule_table($i,d) {
            if {![info exists ::token_table($deriv)]} {
                taccle_error "Symbol $deriv used, but is not defined as a token and has no rules." $::GRAMMAR_ERROR
            }
            lappend new_deriv_list $::token_table($deriv)
        }
        set ::rule_table($i,d) $new_deriv_list
        # set the rule's precedence only if it was not already specified
        if {![info exist ::rule_table($i,prec)]} {
            set ::rule_table($i,prec) [get_prec $new_deriv_list]
        }
    }
    
    # check for unused tokens
    set used_list [concat "error" [recurse_dfs $::start_token_id ""]]
    foreach tok_id $::token_list {
        if {[lsearch -exact $used_list $tok_id] == -1} {
            taccle_warn "Token $::token_id_table($tok_id) unused."
        } else {
            lappend ::used_token_list $tok_id
        }
    }
    # add to the used token list {$} but /not/ start'
    set ::used_token_list [concat [lrange $::used_token_list 0 end-1] \
                               $::token_table("\$")]
}

# Determine which non-terminals are nullable.  Any terminal which can
# be simplified to just an epsilon transition is nullable.
proc generate_nullable_table {} {
    set nullable_found 1
    while {$nullable_found} {
        set nullable_found 0
        foreach tok_id $::token_list {
            if {[info exist ::nullable_table($tok_id)]} {
                continue
            }
            if {$::token_id_table($tok_id,t) == $::TERMINAL} {
                set ::nullable_table($tok_id) 0
                continue
            }            
            for {set i 0} {$i < $::rule_count} {incr i} {
                set lhs $::rule_table($i,l)
                if {$lhs != $tok_id} {
                    continue
                }
                set rhs [lindex $::rule_table($i,d) 0]
                if {$rhs == ""} {
                    set ::nullable_table($lhs) 1
                    set nullable_found 1
                } else {
                    set nullable 0
                    foreach r $rhs {
                        if {[info exists ::nullable_table($r)]} {
                            set nullable $::nullable_table($r)
                            break
                        }
                    }
                    if {$nullable} {
                        set ::nullable_table($lhs) 1
                        set nullable_found 1
                    }
                }
            }
        }
    }
    foreach tok_id $::token_list {
        if {![info exist ::nullable_table($tok_id)]} {
            set ::nullable_table($tok_id) 0
        }
    }
}

# Generate the table of FIRST symbols for the grammar.
proc generate_first_table {} {
    foreach tok_id $::token_list {
        generate_first_recurse $tok_id ""
    }
}

# Recursively calculates the FIRST set for a given token, handling
# nullable terminals as well.
#
# @param tok_id id of token to generate FIRST set
# @param history list of tokens already examined
# @return list of tokens (including -1 for epsilon) in tok_id's FIRST set
proc generate_first_recurse {tok_id history} {
    if {[lsearch -exact $history $tok_id] >= 0} {
        return ""
    }
    if {[info exists ::first_table($tok_id)]} {
        return $::first_table($tok_id)
    }
    if {$::token_id_table($tok_id,t) == $::TERMINAL} {
        set ::first_table($tok_id) $tok_id
        return $tok_id
    }
    # FIRST = union of all first non-terminals on rhs.  if a
    # non-terminal is nullable, then add FIRST of the following
    # terminal to the FIRST set.  keep repeating while nullable.
    set first_union ""
    for {set i 0} {$i < $::rule_count} {incr i} {
        set lhs $::rule_table($i,l)
        if {$lhs != $tok_id} {
            continue
        }
        if {$::rule_table($i,dc) == 0} {
            # empty rule, so add the special epsilon marker -1 to the FIRST set
            lappend first_union -1
        } else {
            foreach r $::rule_table($i,d) {
                lconcat first_union [generate_first_recurse $r [concat $history $tok_id]]
                if {$::nullable_table($r) == 0} {
                    break
                }
            }
        }
    }
    set ::first_table($tok_id) [lsort -increasing -unique $first_union]
    return $first_union
}

# Generate the table of FOLLOW symbols for the grammar.
proc generate_follow_table {} {
    set ::follow_table($::token_table(start')) $::token_table("\$")
    foreach tok_id $::token_list {
        generate_follow_recurse $tok_id ""
    }
}

# Recursively calculates the FOLLOW set for a given token, handling
# nullable terminals as well.
#
# @param tok_id id of token to generate FOLLOW set
# @param history list of tokens already examined
# @return list of tokens in tok_id's FOLLOW set
proc generate_follow_recurse {tok_id history} {
    if {[lsearch -exact $history $tok_id] >= 0} {
        return ""
    }
    if {[info exists ::follow_table($tok_id)]} {
        return $::follow_table($tok_id)
    }
    set follow_union ""
    for {set i 0} {$i < $::rule_count} {incr i} {
        # if the token is on the rhs of the rule then FOLLOW includes
        # the FIRST of the token following it; if at end of rule (or
        # can be derived to end of rule) then FOLLOW includes the
        # FOLLOW of the lhs
        set rhs $::rule_table($i,d)
        for {set j [expr {$::rule_table($i,dc) - 1}]} {$j >= 0} {incr j -1} {
            set r [lindex $rhs $j]
            if {$r != $tok_id} {
                continue
            }
            set k [expr {$j + 1}]
            set gamma [lindex $rhs $k]
            if {$gamma != ""} {
                lconcat follow_union [all_but_eps $::first_table($gamma)]
            }
            set at_end_of_list 1
            while {$k < $::rule_table($i,dc)} {
                if {![has_eps $::first_table([lindex $rhs $k])]} {
                    set at_end_of_list 0
                    break
                }
                incr k
            }
            if {$at_end_of_list} {
                set lhs $::rule_table($i,l)
                lconcat follow_union [generate_follow_recurse $lhs [concat $history $tok_id]]
            }
        }
    }
    set ::follow_table($tok_id) [lsort -increasing -unique $follow_union]
    return $follow_union
}

# Construct a canonical LR(1) by taking the start rule (rule 0) and
# successively adding closures/states until no more new states.
proc generate_lr1 {} {
    # first add start rule to the closure list
    set first_item [list [list 0 $::token_table("\$") 0]]
    set first_closure [add_closure $first_item 0 1]
    set ::lr1_table(0) [concat $first_item $first_closure]
    
    # used to keep count of total number of states produced by LR(1)
    set ::next_lr1_state 1
    
    # keep generating items until none remain
    for {set state_pointer 0} {$state_pointer < $::next_lr1_state} {incr state_pointer} {        
        # iterate through each token, adding transitions to new state(s)
        set trans_list ""
        set oldclosure_list $::lr1_table($state_pointer)
        foreach tok_id $::token_list {
            set todo_list ""
            set working_list ""
            foreach item $oldclosure_list {
                foreach {rule lookahead position} $item {}
                if {$position >= $::rule_table($rule,dc)} {
                    # at end of rule; don't expand (and remove it
                    # from the list)
                    continue
                }
                set nexttoken [lindex $::rule_table($rule,d) $position]
                if {$nexttoken == $tok_id} {
                    # item's next token matches the one currently
                    # saught; add it to the working list
                    lappend working_list $item
                } else {
                    # item was not used yet -- add it back to the
                    # todo list
                    lappend todo_list $item
                }
            }
            set oldclosure_list $todo_list
            if {$working_list != ""} {
                set new_closure ""
                foreach item $working_list {
                    # move pointer ahead to the next position
                    foreach {rule lookahead position} $item {}
                    incr position
                    set newitem [list $rule $lookahead $position]
                    lappend new_closure $newitem
                }
                set new_closure [concat $new_closure \
                                     [add_closure $new_closure 0 [llength $working_list]]]
                # add a transition out of this state -- to a
                # previously examined state if possible, or else
                # create a new state with my new closure
                set next_state -1
                for {set i 0} {$i < $::next_lr1_state} {incr i} {
                    if {[lsort $::lr1_table($i)] == [lsort $new_closure]} {
                        set next_state $i
                        break
                    }
                }
                if {$next_state == -1} {
                    # create a new state
                    set ::lr1_table($::next_lr1_state) $new_closure
                    lappend trans_list [list $tok_id $::next_lr1_state]
                    incr ::next_lr1_state
                } else {
                    # reuse existing state
                    lappend trans_list [list $tok_id $next_state]
                }

            }
        }
        set ::lr1_table($state_pointer,trans) [lsort -command tokid_compare -index 0 $trans_list]
    }
}

# Successively add closures from LR(1) table to LALR(1) table merging
# kernels with similar cores.
proc generate_lalr1 {} {
    for {set i 0} {$i < $::next_lr1_state} {incr i} {
        # as matching closures are found change their mapping here
        set state_mapping_table($i) $i
    }
    # go through all elements of LR(1) table and generate their cores.
    # this will make future comparisons easier.
    for {set i 0} {$i < $::next_lr1_state} {incr i} {
        set core ""
        foreach item $::lr1_table($i) {
            lappend core [list [lindex $item 0] [lindex $item 2]]
        }
        set core_table($i) [lsort $core]
    }
    lappend new_lalr_states(0) 0
    for {set i 1} {$i < $::next_lr1_state} {incr i} {
        set found_matching 0
        for {set j 0} {$j < $i} {incr j} {
            if {$core_table($i) == $core_table($j)} {
                # found a matching core -- change its mapping
                set state_mapping_table($i) $state_mapping_table($j)
                # because this state is being eliminated, shuffle all
                # future states down one
                for {set k [expr {$i + 1}]} {$k < $::next_lr1_state} {incr k} {
                    incr state_mapping_table($k) -1
                }
                # merge state $i into state $j
                lappend new_lalr_states($j) $i
                set found_matching 1
                break
            }
        }
        if {!$found_matching} {
            lappend new_lalr_states($i) $i
        }
    }
    # now copy items from LR(1) table to LALR(1) table
    set ::next_lalr1_state 0
    for {set i 0} {$i < $::next_lr1_state} {incr i} {
        if {![info exists new_lalr_states($i)]} {
            # state no longer exists (it got merged into another one)
            continue
        }
        # first merge together all lookaheads
        set ::lalr1_table($::next_lalr1_state) $::lr1_table([lindex $new_lalr_states($i) 0])
        foreach state [lrange $new_lalr_states($i) 1 end] {
          set ::lalr1_table($::next_lalr1_state) \
              [merge_closures $::lalr1_table($::next_lalr1_state) $::lr1_table($state)]
        }
        # now rewrite the transition table
        foreach trans $::lr1_table($i,trans) {
            foreach {symbol new_state} $trans {}
            lappend ::lalr1_table($::next_lalr1_state,trans) \
                [list $symbol $state_mapping_table($new_state)]
        }
        incr ::next_lalr1_state
    }
}

# Takes the LALR(1) table and resolves precedence issues by removing
# transitions whenever the precedence values indicate a reduce instead
# of a shift.
proc resolve_precedences {} {
    for {set i 0} {$i < $::next_lalr1_state} {incr i} {
        # scan through all kernel items that are at the end of their
        # rule.  for those, use the precedence table to decide to keep
        # a transition (a shift) or not (a reduce)
        foreach item $::lalr1_table($i) {
            foreach {rule lookahead position} $item {}
            if {$position < $::rule_table($rule,dc) || \
                    ![info exist ::lalr1_table($i,trans)]} {
                continue
            }
            set rule_prec_tok $::rule_table($rule,prec)
            set rule_prec_level $::prec_table($rule_prec_tok)
            set rule_prec_dir $::prec_table($rule_prec_tok,dir)
            set new_trans ""
            foreach trans $::lalr1_table($i,trans) {
                set trans_tok [lindex $trans 0]
                if {[lsearch $lookahead $trans_tok] == -1} {
                    lappend new_trans $trans
                    continue
                }
                set trans_tok_level $::prec_table($trans_tok)
                set trans_tok_dir $::prec_table($trans_tok,dir)
                if {$rule_prec_dir == "nonassoc" || \
                        $trans_tok_dir == "nonassoc" || \
                        $rule_prec_level < $trans_tok_level || \
                        ($rule_prec_level == $trans_tok_level && $rule_prec_dir == "right")} {
                    # precedence says to shift, so keep this transition
                    lappend new_trans $trans
                } else {
                    taccle_warn "Conflict in state $i between rule $rule and token \"$trans_tok\", resolved as reduce."
                }
            }
            set ::lalr1_table($i,trans) $new_trans
        }
    }
}

# Check if the grammar contains any infinite recursions.
proc check_recursions {} {
    set cleared ""
    for {set i 0} {$i < $::next_lalr1_state} {incr i} {
        if {[lsearch -exact $cleared $i] >= 0} {
            continue
        }
        set cleared [get_cleared $i {} $cleared]
    }
}

# Recursively performs a DFS search through the LALR(1) table to check
# for cycles.  In each node check if the position is at the end of any
# rule; this marks the node is "reducible" and it is added to the
# 'cleared' list.  Otherwise recurse on each terminal transitioning
# out of this state.  If a state and all of its transitions are not
# reducible then abort with an error.
#
# @param state which state within the LALR(1) table to examine
# @param history a list of states so far examined on this pass
# @param cleared a list of states which have already been verified as reducible
#
# @return a new cleared list, or an empty list of this state is not reducible
proc get_cleared {state history cleared} {
    if {[lsearch -exact $cleared $state] >= 0} {
        return $cleared
    }
    if {[lsearch -exact $history $state] >= 0} {
        return {}
    }
    # check if any items in this closure are reducible; if so then
    # this state passes
    set token -1
    foreach item $::lalr1_table($state) {
        foreach {rule lookahead position} $item {}
        if {$position == $::rule_table($rule,dc)} {
            return [concat $cleared $state]
        }
        if {$position == 0} {
            set token $::rule_table($rule,l)
        }
    }
    # recursively check all terminals transitioning out of this state;
    # if none of the new states eventually reduce then report this as
    # a cycle
    foreach trans $::lalr1_table($state,trans) {
        foreach {tok_id nextstate} $trans {}
        if {$::token_id_table($tok_id,t) == $::TERMINAL} {
            set retval [get_cleared $nextstate [concat $history $state] $cleared]
            if {[llength $retval] > 0} {
                return [concat $retval $state]
            }
        }
    }
    if {$token == -1} {
        puts stderr "OOPS: should not have gotten here!"
        exit -1
    }
    set ::line_count $::rule_table($rule,line)
    taccle_error "Token $::token_id_table($token) appears to recurse infinitely" $::GRAMMAR_ERROR
}

# Takes the LALR(1) table and generates the LALR(1) transition table.
# For terminals do a shift to the new state.  For non-terminals reduce
# when the next token is a lookahead.  Detect shift/reduce conflicts;
# resolve by giving precedence to shifting.  Detect reduce/reduce
# conflicts and resolve by reducing to the first rule found.
proc generate_lalr1_parse_table {} {
    for {set i 0} {$i < $::next_lalr1_state} {incr i} {        
        foreach item $::lalr1_table($i) {
            foreach {rule lookahead position} $item {}
            if {$position >= $::rule_table($rule,dc)} {
                if {$rule == 0} {
                    set command "accept"
                } else {
                    set command "reduce"
                }
                set token_list $lookahead
                # target for a reduce/accept is which rule to use
                # while accepting
                set target $rule
            } else {
                set token [lindex $::rule_table($rule,d) $position]
                if {$::token_id_table($token,t) == $::TERMINAL} {
                    set command "shift"
                } else {
                    set command "goto"
                }
                set token_list [list $token]
                # target for a shift/goto is the new state to move to
                set target ""
                foreach trans $::lalr1_table($i,trans) {
                    foreach {tok_id nextstate} $trans {}
                    if {$tok_id == $token} {
                        set target $nextstate
                        break
                    }
                }
                # this token must have been consumed by shift/reduce
                # conflict resolution through the precedence table
                # (above)
                if {$target == ""} {
                    continue
                }
            }

            foreach token $token_list {
                # check for shift/reduce conflicts
                if {[info exists ::lalr1_parse($i:$token)] && \
                        $::lalr1_parse($i:$token) != $command} {
                    # shifting takes precedence, so overwrite table
                    # entry if needed
                    if {$::lalr1_parse($i:$token) == "shift"} {
                        taccle_warn "Shift/Reduce error in state $i, token \"$::token_id_table($token)\", resolved by keeping shift."
                        break
                    }
                    taccle_warn "Shift/Reduce error in state $i between rule $::lalr1_parse($i:$token,target) and token \"$::token_id_table($token)\", resolved as shift."
                    unset ::lalr1_parse($i:$token,target)
                }
                set ::lalr1_parse($i:$token) $command
                # check for reduce/reduce conflicts
                # (theoretically it is impossible to have a shift/shift error)
                if {[info exists ::lalr1_parse($i:$token,target)] && \
                        $::lalr1_parse($i:$token,target) != $target} {
                    taccle_warn "Reduce/Reduce error in state $i, token \"$::token_id_table($token)\", resolved by reduce to rule $::lalr1_parse($i:$token,target)."
                    break
                }
                set ::lalr1_parse($i:$token,target) $target
            }
            
        }
    }
}

######################################################################
# utility routines that actually handle writing parser to output files

# Writes to the destination file utility functions called by yyparse
# as well as by user-supplied actions.
proc write_parser_utils {} {
    puts $::dest "
######
# Begin autogenerated taccle (version $::TACCLE_VERSION) routines.
# Although taccle itself is protected by the GNU Public License (GPL)
# all user-supplied functions are protected by their respective
# author's license.  See http://mini.net/tcl/taccle for other details.
######

namespace eval ${::p} \{
    variable yylval {}
    variable table
    variable rules
    variable token {}
    variable yycnt 0

    namespace export yylex
\}

proc ${::p}::YYABORT \{\} \{
    return -code return 1
\}

proc ${::p}::YYACCEPT \{\} \{
    return -code return 0
\}

proc ${::p}::yyclearin \{\} \{
    variable token
    variable yycnt
    set token {}
    incr yycnt -1
\}

proc ${::p}::yyerror \{s\} \{
    puts stderr \$s
\}

proc ${::p}::setupvalues \{stack pointer numsyms\} \{
    upvar 1 1 y
    set y \{\}
    for \{set i 1\} \{\$i <= \$numsyms\} \{incr i\} \{
        upvar 1 \$i y
        set y \[lindex \$stack \$pointer\]
        incr pointer
    \}
\}

proc ${::p}::unsetupvalues \{numsyms\} \{
    for \{set i 1\} \{\$i <= \$numsyms\} \{incr i\} \{
        upvar 1 \$i y
        unset y
    \}
\}"
}

# Writes to the destination file the actual parser including LALR(1)
# table.
proc write_parser {} {
    write_array $::dest ${::p}::table [array get ::lalr1_parse]
    write_array $::dest ${::p}::rules [array get ::rule_table *l]
    write_array $::dest ${::p}::rules [array get ::rule_table *dc]
    write_array $::dest ${::p}::rules [array get ::rule_table *e]
    
    puts $::dest "\nproc ${::p}::yyparse {} {
    variable yylval
    variable table
    variable rules
    variable token
    variable yycnt

    set yycnt 0
    set state_stack {0}
    set value_stack {{}}
    set token \"\"
    set accepted 0

    while {\$accepted == 0} {
        set state \[lindex \$state_stack end\]
        if {\$token == \"\"} {
            set yylval \"\"
            set token \[yylex\]
            set buflval \$yylval
	    if {\$token>0} {
	        incr yycnt
            }
        }
        if {!\[info exists table(\$state:\$token)\]} {
            \# pop off states until error token accepted
            while {\[llength \$state_stack\] > 0 && \\
                       !\[info exists table(\$state:error)]} {
                set state_stack \[lrange \$state_stack 0 end-1\]
                set value_stack \[lrange \$value_stack 0 \\
                                       \[expr {\[llength \$state_stack\] - 1}\]\]
                set state \[lindex \$state_stack end\]
            }
            if {\[llength \$state_stack\] == 0} {
                yyerror \"parse error\"
                return 1
            }
            lappend state_stack \[set state \$table(\$state:error,target)\]
            lappend value_stack {}
            \# consume tokens until it finds an acceptable one
            while {!\[info exists table(\$state:\$token)]} {
                if {\$token == 0} {
                    yyerror \"end of file while recovering from error\"
                    return 1
                }
                set yylval {}
                set token \[yylex\]
                set buflval \$yylval
            }
            continue
        }
        switch -- \$table(\$state:\$token) {
            shift {
                lappend state_stack \$table(\$state:\$token,target)
                lappend value_stack \$buflval
                set token \"\"
            }
            reduce {
                set rule \$table(\$state:\$token,target)
                set ll \$rules(\$rule,l)
                if \{\[info exists rules(\$rule,e)\]\} \{
                    set dc \$rules(\$rule,e)
                \} else \{
                    set dc \$rules(\$rule,dc)
                \}
                set stackpointer \[expr {\[llength \$state_stack\]-\$dc}\]
                setupvalues \$value_stack \$stackpointer \$dc
                set _ \$1
                set yylval \[lindex \$value_stack end\]
                switch -- \$rule {"
    for {set i 0} {$i < $::rule_count} {incr i} {
        if {[info exists ::rule_table($i,a)] && [string trim $::rule_table($i,a)] != ""} {
            puts $::dest "                    $i { $::rule_table($i,a) }"
        }
    }

    puts $::dest "                }
                unsetupvalues \$dc
                # pop off tokens from the stack if normal rule
                if \{!\[info exists rules(\$rule,e)\]\} \{
                    incr stackpointer -1
                    set state_stack \[lrange \$state_stack 0 \$stackpointer\]
                    set value_stack \[lrange \$value_stack 0 \$stackpointer\]
                \}
                # now do the goto transition
                lappend state_stack \$table(\[lindex \$state_stack end\]:\$ll,target)
                lappend value_stack \$_
            }
            accept {
                set accepted 1
            }
            goto -
            default {
                puts stderr \"Internal parser error: illegal command \$table(\$state:\$token)\"
                return 2
            }
        }
    }
    return 0
}

######
# end autogenerated taccle functions
######
"
}

# Pretty-prints an array to a file descriptor.  Code contributed by
# jcw.
#
# @param fd file descriptor to which write the array
# @param name name of array to declare within the file
# @param values list of 2-ple values
proc write_array {fd name values} {
    puts $fd "\narray set $name {"
    foreach {x y} $values {
        puts $fd "  [list $x $y]"
    }
    puts $fd "}"
}

# Writes a header file that should be [source]d by the lexer.
proc write_header_file {} {
    # scan through token_table and write out all non-implicit terminals
    puts $::header "namespace eval ${::p} \{"
    foreach tok_id $::token_list {
        if {$::token_id_table($tok_id,t) == $::TERMINAL && \
                [string is integer $tok_id] && $tok_id >= 256} {
            set token $::token_id_table($tok_id)
            puts $::header "set ${token} $tok_id"
        }
    }
    puts $::header "set yylval \{\}"
    puts $::header "\}"
}

######################################################################
# utility functions

# Adds a token to the token table, checking that it does not already
# exist.  Returns the ID for the token (either old one if token
# already exists or the newly assigned id value).
#
# @param token_name name of token to add
# @param type type of token, either $::TERMINAL or $::NON_TERMINAL
# @param implicit for $::TERMINAL tokens, 1 if the token is implicitly
# declared
# @param prec_level precedence level for token
# @param prec_dir direction of precedence, either <var>left</var>,
# <var>right</var>, or <var>nonassoc</var>
# @return id value for this token
proc add_token {token_name type implicit prec_level prec_dir} {
    if {$token_name == "\$"} {
        taccle_error "The token '$' is reserved and may not be used in productions." $::SYNTAX_ERROR
    }
    if {$token_name == "\{" || $token_name == 0} {
        taccle_error "Literal value $token_name not allowed; define a %token instead" $::SYNTAX_ERROR
    }
    if [info exists ::token_table($token_name)] {
        set id $::token_table($token_name)
        if {$::token_table($token_name,t) == $type} {
            # token already exists; modify its precedence level if necessary
            if {$::prec_table($id) < $prec_level} {
                taccle_warn "Redefining precedence of $token_name"
                set ::prec_table($id) $prec_level
                set ::prec_table($id,dir) $prec_dir
            }
            set ::token_id_table($id,line) $::line_count
            return $id
        }
        set old_type [expr {$::token_table($token_name,t) == 1 ? "non-" : ""}]terminal
        taccle_error "Token $token_name already declared as a $old_type" $::GRAMMAR_ERROR
    }
    if $implicit {
        set ::token_table($token_name) $token_name
        set id $token_name
    } else {
        set ::token_table($token_name) $::next_token_id
        set id $::next_token_id
        incr ::next_token_id
    }
    set ::token_table($token_name,t) $type
    set ::token_id_table($id) $token_name
    set ::token_id_table($id,t) $type
    set ::token_id_table($id,line) $::line_count
    lappend ::token_list $id
    set ::prec_table($id) $prec_level
    set ::prec_table($id,dir) $prec_dir
    return $id
}

# Adds closures to each item on $closure_list, starting from the index
# $closure_pointer.  Keeps adding closures until no more are added.
#
# @param closure_list list of closures to process
# @param closure_pointer index into $closure_list to which start
# @param original_length original size of $closure_list
# @return list of closures added
proc add_closure {closure_list closure_pointer original_length} {
    set orig_closure_pointer [expr {$closure_pointer + $original_length}]
    # keep adding items to the closure list until no more
    while {$closure_pointer < [llength $closure_list]} {
        set item [lindex $closure_list $closure_pointer]
        incr closure_pointer
        foreach {rule lookahead position} $item {}
        set mylength $::rule_table($rule,dc)
        if {$position < $mylength} {
            set nexttoken [lindex $::rule_table($rule,d) $position]
            if {$::token_id_table($nexttoken,t) == $::TERMINAL} {
                continue
            }
            # the lookahead is the FIRST of the rule /after/
            # nexttoken, or the current lookahead if at the end of
            # rule.  if the next token is NULLABLE then the lookahead
            # includes that which FOLLOWS it
            set beta_pos [expr {$position + 1}]
            if {$beta_pos >= $mylength} {
                set nextfirst $lookahead
            } else {
                set n [lindex $::rule_table($rule,d) $beta_pos]
                set nextfirst [all_but_eps $::first_table($n)]
                if {$::nullable_table($n)} {
                    set nextfirst [lsort -unique [concat $nextfirst $::follow_table($n)]]
                }
            }
            for {set rule_num 0} {$rule_num < $::rule_count} {incr rule_num} {
                if {$::rule_table($rule_num,l) != $nexttoken} {
                    continue
                }
                set newitem [list $rule_num $nextfirst 0]
                set closure_list [merge_closures $closure_list [list $newitem]]
            }
        }
    }
    return [lrange $closure_list $orig_closure_pointer end]
}

# Recurses through all productions, recording which tokens are
# actually used by the grammar.  Tokens used to indicate a rule's
# precedence are also added.  Returns a list of tokens used; note that
# this list can (and probably will) include duplicates.
#
# @param tok_id id of token to start
# @param history list of tok_id's already examined
# @return list of tokens used
proc recurse_dfs {tok_id history} {
    if {[lsearch -exact $history $tok_id] >= 0} {
        return $history
    }
    if {$::token_id_table($tok_id,t) == $::TERMINAL} {
        return [concat $history $tok_id]
    }
    lappend history $tok_id
    for {set i 0} {$i < $::rule_count} {incr i} {
        set lhs $::rule_table($i,l)
        if {$lhs == $tok_id} {
            foreach deriv $::rule_table($i,d) {
                set history [recurse_dfs $deriv $history]
            }
            lconcat history $::rule_table($i,prec)
        }
    }
    return $history
}

# Given a line, returns a new line with any comments removed.
#
# @param line string with a possible comment
# @return line with any commens removed
proc strip_comments {line} {
    regexp -- {\A([^\#]*)} $line foo line
    return $line
}

# Combines unique elements of the two closures, also merging lookahead
# symbols, and returns the new closure.
#
# @param closure1 first closure to merge
# @param closure2 second closure to merge
# @return $closure1 and $closure2 merged together, with duplicated removed
proc merge_closures {closure1 closure2} {
    foreach item2 $closure2 {
        foreach {rule2 lookahead2 pos2} $item2 {}
        set found_match 0
        for {set i 0} {$i < [llength $closure1]} {incr i} {
            foreach {rule1 lookahead1 pos1} [lindex $closure1 $i] {}
            if {$rule2 == $rule1 && $pos2 == $pos1} {
                set lookahead1 [lsort -uniq [concat $lookahead1 $lookahead2]]
                lset closure1 $i [list $rule1 $lookahead1 $pos1]
                set found_match 1
                break
            }
        }
        if {!$found_match} {
            lappend closure1 $item2
        }
    }
    return $closure1
}

# Compares two token id values.  If the two are integers then uses
# their values for comparison; otherwise performs a string comparison.
# Integer values are always "greater than" strings.
#
# @param a first token id
# @param b second token id
# @return -1 if <var>a</var> is less than <var>b</var>, 1 if
# <var>a</var> is greater, otherwise 0
proc tokid_compare {a b} {
    if {[string is integer $a] && [string is integer $b]} {
        if {$a < $b} {
            return -1
        } else {
            return 1
        }
    }
    if [string is integer $a] {
        return 1
    }
    if [string is integer $b] {
        return -1
    }
    return [string compare $a $b]
}

# Given a list, returns all everything in it except for any elements
# of value "-1", which corresponds with the epsilon symbol.
#
# @param first_list list of tokens (presumably a FIRST set)
# @return new list with all -1 values removed
proc all_but_eps {first_list} {
    set new_list ""
    foreach tok $first_list {
        if {$tok != -1} {
            lappend new_list $tok
        }
    }
    return $new_list
}

# Returns truth if the element value "-1", corresponding with the
# epsilon symbol, resides within the first list $first_list.
#
# @param first_list list of tokens (presumably a FIRST set)
# @return 1 if $first_list has the element -1, 0 otherwise
proc has_eps {first_list} {
    foreach tok $first_list {
        if {$tok == -1} {
            return 1
        }
    }
    return 0
}

# Given a list of tokens, returns the token with highest precedence
# level.
#
# @param tok_list list of token ids
# @return token with highest precedence; in case of tie returns first
# one found
proc get_prec {tok_list} {
    set prec_token 0
    foreach tok $tok_list {
        if {$::prec_table($tok) > $::prec_table($prec_token)} {
            set prec_token $tok
        }
    }
    return $prec_token
}

# Appends the first list a flattened version of the second, but only
# if the second is non-empty.
#
# @param list first list
# @param lists list of lists to append
# @return new list
proc lconcat {list lists} {
    upvar $list l
    if {$lists != ""} {
        set l [concat $l $lists]
    } else {
        return $l
    }
}

# Retrives a parameter from the options list.  If no parameter exists
# then abort with an error very reminisicent of C's
# <code>getopt</code> function; otherwise increment
# <code>param_num</code> by one.
#
# @param param_list list of parameters from the command line
# @param param_num index into <code>param_list</code> to retrieve
# @param param_name name of the parameter, used when reporting an error
# @return the <code>$param_num</code>'th element into <code>$param_list</code>
proc get_param {param_list param_num param_name} {
    upvar $param_num pn
    incr pn
    if {$pn >= [llength $param_list]} {
        puts stderr "taccle: option requires an argument -- $param_name"
        exit $::PARAM_ERROR
    }
    return [lindex $param_list $pn]
}

# Display to standard error a message, then abort the program.
proc taccle_error {message returnvalue} {
    if {$::verbose != ""} {
        puts $::verbose "$message (line $::line_count)"
    } 
    puts stderr "$message (line $::line_count)"
    exit $returnvalue
}

# Display a message to standard error if warnings enabled.  Write to
# the verbose output file if verbose is enabled.
proc taccle_warn {message} {
    if {$::show_warnings} {
        puts stderr $message
    }
    if {$::verbose != ""} {
        puts $::verbose "$message"
    }
}

# Print to a particular channel a brief summary of taccle command line
# options.
proc print_taccle_help {chan} {
    puts $chan "taccle: a Tcl compiler compiler
Usage: taccle \[options\] file
  file     a taccle grammar specification file

Options:
  -h          print this help message and quit
  -d          write extra output file containing Tcl code to be
              \[source\]d by yylex
  -o FILE     specify name to write parser
  -v          write extra output file containing descriptions of all
              parser states and extended information about conflicts
  -w          display all warnings to standard error
  -p PREFIX   change default yy prefix to PREFIX
  --version   print taccle version and quit

For more information see http://mini.net/tcl/taccle"
}

# Displays to standard out the taccle version, then exits program.
proc print_taccle_version {} {
    puts "taccle version $::TACCLE_VERSION"
    exit 0
}

######################################################################
# internal debugging routines

proc print_symbol_table {} {
    puts $::verbose "token table:"
    puts $::verbose [format "%-5s %-10s %s" "id" "token" "type"]
    foreach tok_id $::token_list {
        set token $::token_id_table($tok_id)
        if {$::token_id_table($tok_id,t) == $::TERMINAL} {
            set type "terminal"
        } else {
            set type "non-terminal"
        }
        puts $::verbose [format "%-5s %-10s %s" $tok_id $token $type]
    }
}

proc print_rule_table {} {
    puts $::verbose "rule table:"
    for {set i 0} {$i < $::rule_count} {incr i} {
        set lhs $::token_id_table($::rule_table($i,l))
        set deriv_list ""
        foreach deriv $::rule_table($i,d) {
            lappend deriv_list $::token_id_table($deriv)
        }
        if {$deriv_list == ""} {
            set deriv_list "\#\# empty \#\#"
        }
        puts $::verbose [format "%3d:  %-10s -> %s" $i $lhs $deriv_list]
    }
}

proc print_first_table {} {
    puts $::verbose "first table:"
    foreach tok_id $::token_list {
        if {$tok_id == -1} {
            continue
        }
        set token $::token_id_table($tok_id)
        set first_list ""
        foreach first $::first_table($tok_id) {
            if {$first >= 0} {
                lappend first_list $::token_id_table($first)
            }
        }
        puts $::verbose [format "%-10s => %s" $token $first_list]
    }
}

proc print_closure {closure_list indent dest} {
    foreach item $closure_list {
        foreach {rule lookahead position} $item {}
        set lhs $::token_id_table($::rule_table($rule,l))
        set deriv_list ""
        set i 0
        foreach deriv $::rule_table($rule,d) {
            if {$i == $position} {
                lappend deriv_list "."
            }
            lappend deriv_list $::token_id_table($deriv)
            incr i
        }
        if {$position == $::rule_table($rule,dc)} {
            lappend deriv_list "."
        }
        set lookahead_list ""
        foreach la $lookahead {
            lappend lookahead_list $::token_id_table($la)
        }
        puts $dest \
            [format "%*s %-10s -> %s, %s" $indent "" $lhs $deriv_list $lookahead_list]
    }
}

proc print_lr_table {table_name num_entries} {
    upvar $table_name table
    for {set i 0} {$i < $num_entries} {incr i} {
        puts $::verbose "state $i:"
        print_closure $table($i) 2 $::verbose
        if {[info exists table($i,trans)] && [llength $table($i,trans)] >= 1} {
            puts -nonewline $::verbose [format "%*s transitions:" 2 ""]
            foreach trans $table($i,trans) {
                foreach {tok_id nextstate} $trans {}
                puts -nonewline $::verbose "  $::token_id_table($tok_id) => s$nextstate"
            }
            puts $::verbose ""
        }
        puts $::verbose ""
    }
}

proc print_lr1_table {} {
    puts $::verbose "lr(1) table:"
    print_lr_table ::lr1_table $::next_lr1_state
}

proc print_lalr1_table {} {
    puts $::verbose "lalr(1) table:"
    print_lr_table ::lalr1_table $::next_lalr1_state
}

proc print_lalr1_parse {} {
    puts $::verbose "generated lalr(1) parse table:"
    puts -nonewline $::verbose "state "
    foreach tok_id $::used_token_list {
        set token [string range $::token_id_table($tok_id) 0 4]
        puts -nonewline $::verbose [format " %-5s" $token]
    }
    puts $::verbose ""
    for {set i 0} {$i < $::next_lalr1_state} {incr i} {
        puts -nonewline $::verbose [format "%4s  " $i]
        foreach tok_id $::used_token_list {
            if [info exists ::lalr1_parse($i:$tok_id)] {
                switch -- $::lalr1_parse($i:$tok_id) {
                    shift  { set s "sh" }
                    goto   { set s "go" }
                    reduce { set s "re" }
                    accept { set s "accept" }
                }
                if {$s != "accept"} {
                    append s $::lalr1_parse($i:$tok_id,target)
                }
                puts -nonewline $::verbose [format " %-5s" $s]
            } else {
                puts -nonewline $::verbose "      "
            }
        }
        puts $::verbose ""
    }
}

######################################################################
# other taccle functions

# Parse the taccle command line.
proc taccle_args {argv} {
    set argvp 0
    set write_defs_file 0
    set write_verbose_file 0
    set out_filename ""
    set ::p "yy"
    set ::show_warnings 0
    while {$argvp < [llength $argv]} {
        set arg [lindex $argv $argvp]
        switch -- $arg {
            "-d" { set write_defs_file 1 }
            "-h" -
            "--help" { print_taccle_help stdout; exit 0 }
            "-o" { set out_filename [get_param $argv argvp "o"] }
            "-v" - "--verbose" { set write_verbose_file 1 }
            "-w" { set ::show_warnings 1 }
            "-p" {
                set prefix [get_param $argv argvp "p"]
                set ::p [string tolower $prefix]
            }
            "--version" { print_taccle_version }
            default {
                if {[string index $arg 0] != "-"} {
                    break
                } else {
                    puts stderr "taccle: unknown option $arg"
                    print_taccle_help stderr
                    exit $::PARAM_ERROR
                }
            }
        }
        incr argvp
    }
    if {$argvp >= [llength $argv]} {
        puts stderr "taccle: no grammar file given"
        print_taccle_help stderr
        exit $::IO_ERROR
    }
    set in_filename [lindex $argv $argvp]
    if {$out_filename == ""} {
        set out_filename [file rootname $in_filename]
        append out_filename ".tcl"
    }
    if [catch {open $in_filename r} ::src] {
        puts stderr "Could not open grammar file '$in_filename'."
        exit $::IO_ERROR
    }
    if [catch {open $out_filename w} ::dest] {
        puts stderr "Could not open output file '$out_filename'."
        exit $::IO_ERROR
    }
    if $write_defs_file {
        set header_filename "[file rootname $out_filename].tab.tcl"
        if [catch {open $header_filename w} ::header] {
            puts stderr "Could not open header file '$header_filename'."
            exit $::IO_ERROR
        }
    } else {
        set ::header ""
    }
    if $write_verbose_file {
        set verbose_filename "[file rootname $out_filename].output"
        if [catch {open $verbose_filename w} ::verbose] {
            puts stderr "Could not open verbose file '$verbose_filename'."
            exit $::IO_ERROR
        }
    } else {
        set ::verbose ""
    }
}

# Actually do the parser generation.
proc taccle_main {} {
    set ::line_count 0
    
    # counts number of rules in the grammar
    # rule number 0 is reserved for the special augmentation S' -> S
    set ::rule_count 1

    # used to keep track of token IDs:
    # 0 is reserved for the special token '$'
    # 256 for the error token
    set ::next_token_id 257

    # used to keep track of operator precedence level
    # level 0 is reserved for terminals without any precedence
    set ::next_precedence 1

    # keep track of where within the file I am:
    # definitions, rules, or subroutines
    set file_state definitions

    while {[gets $::src line] >= 0} {
        incr ::line_count
    
        if {$line == "%%"} {
            if {$file_state == "definitions"} {
                set file_state "rules"
            } elseif {$file_state == "rules"} {
                set file_state "subroutines"
            } else {
                taccle_error "Syntax error." $::SYNTAX_ERROR
            }
        } else {
            if {$file_state == "definitions"} {
                handle_defs $line
            } elseif {$file_state == "rules"} {
                # keep reading the rest of the file until EOF or
                # another '%%' appears
                set rules_buf [strip_comments $line]
                while {[gets $::src line] >= 0 && $file_state == "rules"} {
                    if {$line == "%%"} {
                        set file_state "subroutines"
                    } else {
                        append rules_buf "\n" [strip_comments $line]
                    }
                }
                build_parser $rules_buf
                set file_state "subroutines"
                write_parser_utils
                write_parser
            } else {
                # file_state is subroutines -- copy verbatim to output file
                puts $::dest $line
            }
        }
    }
    if {$::header != ""} {
        write_header_file
    }
    if {$::verbose != ""} {
        print_symbol_table
        puts $::verbose ""
        print_rule_table
        puts $::verbose ""
        #print_first_table
        #puts $::verbose ""
        #print_lr1_table
        print_lalr1_table
        print_lalr1_parse
    }
}

######################################################################
# start of actual script

set IO_ERROR 1
set SYNTAX_ERROR 2
set PARAM_ERROR 3
set GRAMMAR_ERROR 4

set TERMINAL 0
set NONTERMINAL 1

taccle_args $argv
taccle_main