summaryrefslogtreecommitdiffstats
path: root/generic/Lgrammar.y
blob: 2b6ee8ffd38e5edd0f68bd91cb2741355a668727 (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
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
%{
/*
 * Copyright (c) 2006-2008 BitMover, Inc.
 */
#include <stdio.h>
#include "Lcompile.h"

/* L_lex is generated by flex. */
extern int	L_lex (void);

#define YYERROR_VERBOSE
#define L_error L_synerr
%}

/*
 * We need a GLR parser because of a shift/reduce conflict introduced
 * by hash-element types.  This production is the culprit:
 *
 * array_or_hash_type: "{" scalar_type_specifier "}"
 *
 * This introduced a shift/reduce conflict on "{" due to "{" being in
 * the FOLLOW set of scalar_type_specifier because "{" can follow
 * type_specifier in function_decl.  For example, after you
 * have seen
 *
 *    struct s
 *
 * and "{" is the next token, the parser can't tell whether to shift
 * and proceed to parse a struct_specifier that declares a struct:
 *
 *    struct s { int x,y; }
 *
 * or whether to reduce and proceed in to a array_or_hash_type:
 *
 *    struct s { int } f() {}
 *
 * To make this grammar LALR(1) seemed difficult.  The grammar seems
 * to want to be LALR(3) perhaps(?).  The best we could do was to extend
 * the language by pushing the array_or_hash_type syntax down into
 * scalar_type_specifier and struct_specifier.  This would allow
 * inputs that should be syntax errors, so extra checking would have
 * been needed to detect these cases.
 *
 * The GLR parser has no problem with this type of conflict and keeps
 * the grammar nice.
 *
 * Note that the %expect 1 below is for this conflict.  Although the
 * GLR parser handles it, it is still reported as a conflict.
 */
%glr-parser
%expect 1

%union {
	long	i;
	char	*s;
	Tcl_Obj	*obj;
	Type	*Type;
	Expr	*Expr;
	Block	*Block;
	ForEach	*ForEach;
	Switch	*Switch;
	Case	*Case;
	FnDecl	*FnDecl;
	Cond	*Cond;
	Loop	*Loop;
	Stmt	*Stmt;
	TopLev	*TopLev;
	VarDecl	*VarDecl;
	ClsDecl	*ClsDecl;
	struct {
		Type	*t;
		char	*s;
	} Typename;
}

%token T_ANDAND "&&"
%token T_ARROW "=>"
%token T_ATTRIBUTE "_attribute"
%token T_BANG "!"
%token T_BANGTWID "!~"
%token T_BITAND "&"
%token T_BITOR "|"
%token T_BITNOT "~"
%token T_BITXOR "^"
%token T_BREAK "break"
%token T_CLASS "class"
%token T_COLON ":"
%token T_COMMA ","
%token T_CONSTRUCTOR "constructor"
%token T_CONTINUE "continue"
%token T_DEFINED "defined"
%token T_DESTRUCTOR "destructor"
%token T_DO "do"
%token T_DOT "."
%token T_DOTDOT ".."
%token T_ELLIPSIS "..."
%token T_ELSE "else"
%token T_EQ "eq"
%token T_EQBITAND "&="
%token T_EQBITOR "|="
%token T_EQBITXOR "^="
%token T_EQDOT ".="
%token T_EQLSHIFT "<<="
%token T_EQMINUS "-="
%token T_EQPERC "%="
%token T_EQPLUS "+="
%token T_EQRSHIFT ">>="
%token T_EQSTAR "*="
%token T_EQSLASH "/="
%token T_EQTWID "=~"
%token T_EQUALS "="
%token T_EQUALEQUAL "=="
%token T_EXPAND "(expand)"
%token T_EXTERN "extern"
%token T_FLOAT "float"
%token <s> T_FLOAT_LITERAL "float constant"
%token T_FOR "for"
%token T_FOREACH "foreach"
%token T_GOTO "goto"
%token T_GE "ge"
%token T_GREATER ">"
%token T_GREATEREQ ">="
%token T_GT "gt"
%token <s> T_HTML
%token <s> T_ID "id"
%token T_IF "if"
%token T_INSTANCE "instance"
%token T_INT "int"
%token <s> T_INT_LITERAL "integer constant"
%token <s> T_LHTML_EXPR_START "<?="
%token <s> T_LHTML_EXPR_END "?>"
%token T_LBRACE "{"
%token T_LBRACKET "["
%token T_LE "le"
%token <s> T_LEFT_INTERPOL "${"
%token <s> T_LEFT_INTERPOL_RE "${ (in re)"
%token T_LESSTHAN "<"
%token T_LESSTHANEQ "<="
%token T_LPAREN "("
%token T_LSHIFT "<<"
%token T_LT "lt"
%token T_MINUS "-"
%token T_MINUSMINUS "--"
%token T_NE "ne"
%token T_NOTEQUAL "!="
%token T_OROR "||"
%token <s> T_PATTERN "pattern function"
%token T_PERC "%"
%token T_PLUS "+"
%token T_PLUSPLUS "++"
%token T_POINTS "->"
%token T_POLY "poly"
%token T_PRIVATE "private"
%token T_PUBLIC "public"
%token T_QUESTION "?"
%token T_RBRACE "}"
%token T_RBRACKET "]"
%token <s> T_RE "regular expression"
%token <s> T_RE_MODIFIER "regexp modifier"
%token T_RETURN "return"
%token T_RIGHT_INTERPOL "} (end of interpolation)"
%token T_RIGHT_INTERPOL_RE "} (end of interpolation in re)"
%token T_RPAREN ")"
%token T_RSHIFT ">>"
%token T_TRY "try"
%token T_SEMI ";"
%token T_SLASH "/"
%token T_SPLIT "split"
%token T_STAR "*"
%token <s> T_START_BACKTICK "backtick"
%token <s> T_STR_BACKTICK "`"
%token <s> T_STR_LITERAL "string constant"
%token T_STRCAT " . "
%token T_STRING "string"
%token T_STRUCT "struct"
%token <s> T_SUBST "=~ s/a/b/"
%token <Typename> T_TYPE "type name"
%token T_TYPEDEF "typedef"
%token T_UNLESS "unless"
%token T_ARGUSED "_argused"
%token T_OPTIONAL "_optional"
%token T_MUSTBETYPE "_mustbetype"
%token T_VOID "void"
%token T_WIDGET "widget"
%token T_WHILE "while"
%token T_PRAGMA "#pragma"
%token T_SWITCH "switch"
%token T_CASE "case"
%token T_DEFAULT "default"
%token END 0 "end of file"

/*
 * This follows the C operator-precedence rules, from lowest to
 * highest precedence.
 */
%left LOWEST
// The following %nonassoc lines are defined to resolve a conflict with
// labeled statements (see the stmt nonterm).
%nonassoc T_IF T_UNLESS T_RETURN T_ID T_STR_LITERAL T_LEFT_INTERPOL
%nonassoc T_STR_BACKTICK T_INT_LITERAL T_FLOAT_LITERAL T_TYPE T_WHILE
%nonassoc T_FOR T_DO T_DEFINED T_STRING T_FOREACH T_BREAK T_CONTINUE
%nonassoc T_SPLIT T_GOTO T_WIDGET T_PRAGMA T_SWITCH T_START_BACKTICK T_TRY
%nonassoc T_HTML T_LHTML_EXPR_START
%left T_COMMA
%nonassoc T_ELSE T_SEMI
%right T_EQUALS T_EQPLUS T_EQMINUS T_EQSTAR T_EQSLASH T_EQPERC
       T_EQBITAND T_EQBITOR T_EQBITXOR T_EQLSHIFT T_EQRSHIFT T_EQDOT
%right T_QUESTION
%left T_OROR
%left T_ANDAND
%left T_BITOR
%left T_BITXOR
%left T_BITAND
%left T_EQ T_NE T_EQUALEQUAL T_NOTEQUAL T_EQTWID T_BANGTWID
%left T_GT T_GE T_LT T_LE T_GREATER T_GREATEREQ T_LESSTHAN T_LESSTHANEQ
%left T_LSHIFT T_RSHIFT
%left T_PLUS T_MINUS T_STRCAT
%left T_STAR T_SLASH T_PERC
%right PREFIX_INCDEC UPLUS UMINUS T_BANG T_BITNOT ADDRESS
%left T_LBRACKET T_LBRACE T_RBRACE T_DOT T_POINTS T_PLUSPLUS T_MINUSMINUS
%left HIGHEST

%type <TopLev> toplevel_code
%type <ClsDecl> class_decl class_decl_tail
%type <FnDecl> function_decl fundecl_tail fundecl_tail1
%type <Stmt> stmt single_stmt compound_stmt stmt_list opt_stmt_list
%type <Stmt> unlabeled_stmt optional_else
%type <Cond> selection_stmt
%type <Loop> iteration_stmt
%type <ForEach> foreach_stmt
%type <Switch> switch_stmt
%type <Case> switch_cases switch_case
%type <Expr> expr expression_stmt argument_expr_list pragma_expr_list
%type <Expr> id id_list string_literal cmdsubst_literal dotted_id
%type <Expr> regexp_literal regexp_literal_mod subst_literal interpolated_expr
%type <Expr> interpolated_expr_re list list_element case_expr option_arg
%type <Expr> here_doc_backtick opt_attribute pragma
%type <VarDecl> parameter_list parameter_decl_list parameter_decl
%type <VarDecl> declaration_list declaration declaration2
%type <VarDecl> init_declarator_list declarator_list init_declarator
%type <VarDecl> declarator opt_declarator struct_decl_list struct_decl
%type <VarDecl> struct_declarator_list
%type <Type> array_or_hash_type type_specifier scalar_type_specifier
%type <Type> struct_specifier
%type <obj> dotted_id_1
%type <i> decl_qualifier parameter_attrs

%%

start:	  toplevel_code
	{
		REVERSE(TopLev, next, $1);
		L->ast = $1;
	}
	;

toplevel_code:
	  toplevel_code class_decl
	{
		if ($2) {
			$$ = ast_mkTopLevel(L_TOPLEVEL_CLASS, $1, @2, @2);
			$$->u.class = $2;
		} else {
			// Don't create a node for a forward class declaration.
			$$ = $1;
		}
	}
	| toplevel_code function_decl
	{
		$$ = ast_mkTopLevel(L_TOPLEVEL_FUN, $1, @2, @2);
		$2->decl->flags |= DECL_FN;
		if ($2->decl->flags & DECL_PRIVATE) {
			$2->decl->flags |= SCOPE_SCRIPT;
		} else {
			$2->decl->flags |= SCOPE_GLOBAL;
		}
		$$->u.fun = $2;
	}
	| toplevel_code struct_specifier ";"
	{
		$$ = $1;  // nothing more to do
	}
	| toplevel_code T_TYPEDEF type_specifier declarator ";"
	{
		L_set_declBaseType($4, $3);
		L_typedef_store($4);
		$$ = $1;  // nothing more to do
	}
	| toplevel_code declaration
	{
		// Global variable declaration.
		VarDecl *v;
		$$ = ast_mkTopLevel(L_TOPLEVEL_GLOBAL, $1, @2, @2);
		for (v = $2; v; v = v->next) {
			v->flags |= DECL_GLOBAL_VAR;
			if ($2->flags & DECL_PRIVATE) {
				v->flags |= SCOPE_SCRIPT;
			} else {
				v->flags |= SCOPE_GLOBAL;
			}
		}
		$$->u.global = $2;
	}
	| toplevel_code stmt
	{
		// Top-level statement.
		$$ = ast_mkTopLevel(L_TOPLEVEL_STMT, $1, @2, @2);
		$$->u.stmt = $2;
	}
	| /* epsilon */		{ $$ = NULL; }
	;

class_decl:
	  T_CLASS id "{"
	{
		/*
		 * This is a new class declaration.
		 * Alloc the VarDecl now and associate it with
		 * the class name so that it is available while
		 * parsing the class body.
		 */
		Type	*t = type_mkClass();
		VarDecl	*d = ast_mkVarDecl(t, $2, @1, @1);
		ClsDecl	*c = ast_mkClsDecl(d, @1, @1);
		t->u.class.clsdecl = c;
		ASSERT(!L_typedef_lookup($2->str));
		L_typedef_store(d);
		$<ClsDecl>$ = c;
	} class_decl_tail
	{
		$$ = $5;
		/* silence unused warning */
		(void)$<ClsDecl>4;
	}
	| T_CLASS T_TYPE "{"
	{
		/*
		 * This is a class declaration where the type name was
		 * previously declared.  Use the ClsDecl from the
		 * prior decl.
		 */
		ClsDecl	*c = $2.t->u.class.clsdecl;
		unless (c->decl->flags & DECL_FORWARD) {
			L_err("redeclaration of %s", $2.s);
		}
		ASSERT(isclasstype(c->decl->type));
		c->decl->flags &= ~DECL_FORWARD;
		$<ClsDecl>$ = c;
	} class_decl_tail
	{
		$$ = $5;
		/* silence unused warning */
		(void)$<ClsDecl>4;
	}
	| T_CLASS id ";"
	{
		/* This is a forward class declaration. */
		Type	*t = type_mkClass();
		VarDecl	*d = ast_mkVarDecl(t, $2, @1, @3);
		ClsDecl	*c = ast_mkClsDecl(d, @1, @3);
		ASSERT(!L_typedef_lookup($2->str));
		t->u.class.clsdecl = c;
		d->flags |= DECL_FORWARD;
		L_typedef_store(d);
		$<ClsDecl>$ = NULL;
	}
	| T_CLASS T_TYPE ";"
	{
		/* Empty declaration of an already declared type. */
		unless (isclasstype($2.t)) {
			L_err("%s not a class type", $2.s);
		}
		$<ClsDecl>$ = NULL;
	}
	;

class_decl_tail:
	class_code "}"
	{
		$$ = $<ClsDecl>0;
		$$->node.loc.end       = @2.end;
		$$->decl->node.loc.end = @2.end;
		/* If constructor or destructor were omitted, make defaults. */
		unless ($$->constructors) {
			$$->constructors = ast_mkConstructor($$);
		}
		unless ($$->destructors) {
			$$->destructors = ast_mkDestructor($$);
		}
	}
	;

class_code:
	  class_code T_INSTANCE "{" declaration_list "}" opt_semi
	{
		VarDecl	*v;
		ClsDecl	*clsdecl = $<ClsDecl>0;
		REVERSE(VarDecl, next, $4);
		for (v = $4; v; v = v->next) {
			v->clsdecl = clsdecl;
			v->flags  |= SCOPE_CLASS | DECL_CLASS_INST_VAR;
			unless (v->flags & (DECL_PUBLIC | DECL_PRIVATE)) {
				L_errf(v, "class instance variable %s not "
				       "declared public or private",
				       v->id->str);
				v->flags |= DECL_PUBLIC;
			}
		}
		APPEND_OR_SET(VarDecl, next, clsdecl->instvars, $4);
	}
	| class_code T_INSTANCE "{" "}" opt_semi
	| class_code declaration
	{
		VarDecl	*v;
		ClsDecl	*clsdecl = $<ClsDecl>0;
		REVERSE(VarDecl, next, $2);
		for (v = $2; v; v = v->next) {
			v->clsdecl = clsdecl;
			v->flags  |= SCOPE_CLASS | DECL_CLASS_VAR;
			unless (v->flags & (DECL_PUBLIC | DECL_PRIVATE)) {
				L_errf(v, "class variable %s not "
				       "declared public or private",
				       v->id->str);
				v->flags |= DECL_PUBLIC;
			}
		}
		APPEND_OR_SET(VarDecl, next, clsdecl->clsvars, $2);
	}
	| class_code struct_specifier ";"
	| class_code T_TYPEDEF type_specifier declarator ";"
	{
		L_set_declBaseType($4, $3);
		L_typedef_store($4);
	}
	| class_code function_decl
	{
		ClsDecl	*clsdecl = $<ClsDecl>0;
		$2->decl->clsdecl = clsdecl;
		$2->decl->flags  |= DECL_CLASS_FN;
		unless ($2->decl->flags & DECL_PRIVATE) {
			$2->decl->flags |= SCOPE_GLOBAL | DECL_PUBLIC;
		} else {
			$2->decl->flags |= SCOPE_CLASS;
			$2->decl->tclprefix = cksprintf("_L_class_%s_",
						clsdecl->decl->id->str);
		}
		APPEND_OR_SET(FnDecl, next, clsdecl->fns, $2);
	}
	| class_code T_CONSTRUCTOR fundecl_tail
	{
		ClsDecl	*clsdecl = $<ClsDecl>0;
		$3->decl->type->base_type = clsdecl->decl->type;
		$3->decl->clsdecl = clsdecl;
		$3->decl->flags  |= SCOPE_GLOBAL | DECL_CLASS_FN | DECL_PUBLIC |
			DECL_CLASS_CONST;
		APPEND_OR_SET(FnDecl, next, clsdecl->constructors, $3);
	}
	| class_code T_DESTRUCTOR fundecl_tail
	{
		ClsDecl	*clsdecl = $<ClsDecl>0;
		$3->decl->type->base_type = L_void;
		$3->decl->clsdecl = clsdecl;
		$3->decl->flags  |= SCOPE_GLOBAL | DECL_CLASS_FN | DECL_PUBLIC |
			DECL_CLASS_DESTR;
		APPEND_OR_SET(FnDecl, next, clsdecl->destructors, $3);
	}
	| class_code pragma
	{
		/*
		 * We don't store the things that make up class_code
		 * in order, so there's no place in which to
		 * interleave #pragmas.  So don't create an AST node,
		 * just update L->options now; it gets used when other
		 * AST nodes are created.
		 */
		L_compile_attributes(L->options, $2, L_attrs_pragma);
	}
	| /* epsilon */
	;

opt_semi:
	  ";"
	| /* epsilon */
	;

function_decl:
	  type_specifier fundecl_tail
	{
		$2->decl->type->base_type = $1;
		$$ = $2;
		$$->node.loc = @1;
	}
	| decl_qualifier type_specifier fundecl_tail
	{
		$3->decl->type->base_type = $2;
		$3->decl->flags |= $1;
		$$ = $3;
		$$->node.loc = @1;
	}
	;

fundecl_tail:
	  id fundecl_tail1
	{
		$$ = $2;
		$$->decl->id = $1;
		$$->node.loc = @1;
	}
	| T_PATTERN fundecl_tail1
	{
		VarDecl	*new_param;
		Expr	*dollar1 = ast_mkId("$1", @2, @2);

		$$ = $2;
		$$->decl->id = ast_mkId($1, @1, @1);
		ckfree($1);
		$$->node.loc = @1;
		/* Prepend a new arg "$1" as the first formal. */
		new_param = ast_mkVarDecl(L_string, dollar1, @1, @2);
		new_param->flags = SCOPE_LOCAL | DECL_LOCAL_VAR;
		new_param->next = $2->decl->type->u.func.formals;
		$$->decl->type->u.func.formals = new_param;
	}
	;

fundecl_tail1:
	  "(" parameter_list ")" opt_attribute compound_stmt
	{
		Type	*type = type_mkFunc(NULL, $2);
		VarDecl	*decl = ast_mkVarDecl(type, NULL, @1, @3);
		decl->attrs = $4;
		$$ = ast_mkFnDecl(decl, $5->u.block, @1, @5);
	}
	| "(" parameter_list ")" opt_attribute ";"
	{
		Type	*type = type_mkFunc(NULL, $2);
		VarDecl	*decl = ast_mkVarDecl(type, NULL, @1, @3);
		decl->attrs = $4;
		$$ = ast_mkFnDecl(decl, NULL, @1, @5);
	}
	;

stmt:
	  T_ID ":" stmt
	{
		$$ = ast_mkStmt(L_STMT_LABEL, NULL, @1, @3);
		$$->u.label = $1;
		$$->next = $3;
	}
	| T_ID ":" %prec LOWEST
	{
		$$ = ast_mkStmt(L_STMT_LABEL, NULL, @1, @2);
		$$->u.label = $1;
	}
	| unlabeled_stmt
	| pragma
	{
		L_compile_attributes(L->options, $1, L_attrs_pragma);
		$$ = NULL;
	}
	| T_HTML
	{
		// Wrap the html in a puts(-nonewline) call.
		Expr	*fn = ast_mkId("puts", @1, @1);
		Expr	*arg = ast_mkConst(L_string, "-nonewline", @1, @1);
		arg->next = ast_mkConst(L_string, $1, @1, @1);
		$$ = ast_mkStmt(L_STMT_EXPR, NULL, @1, @1);
		$$->u.expr = ast_mkFnCall(fn, arg, @1, @1);
	}
	| T_LHTML_EXPR_START expr T_LHTML_EXPR_END
	{
		// Wrap expr in a puts(-nonewline) call.
		Expr	*fn = ast_mkId("puts", @2, @2);
		Expr	*arg = ast_mkConst(L_string, "-nonewline", @2, @2);
		arg->next = $2;
		$$ = ast_mkStmt(L_STMT_EXPR, NULL, @1, @3);
		$$->u.expr = ast_mkFnCall(fn, arg, @1, @3);
	}
	;

pragma_expr_list:
	  id
	| id "=" id
	{
		$$ = ast_mkBinOp(L_OP_EQUALS, $1, $3, @1, @3);
	}
	| id "=" T_INT_LITERAL
	{
		Expr	*lit = ast_mkConst(L_int, $3, @3, @3);
		$$ = ast_mkBinOp(L_OP_EQUALS, $1, lit, @1, @3);
	}
	| pragma_expr_list "," id
	{
		$3->next = $1;
		$$ = $3;
		$$->node.loc.beg = @1.beg;
	}
	| pragma_expr_list "," id "=" id
	{
		$$ = ast_mkBinOp(L_OP_EQUALS, $3, $5, @3, @5);
		$$->next = $1;
		$$->node.loc.beg = @1.beg;
	}
	| pragma_expr_list "," id "=" T_INT_LITERAL
	{
		Expr	*lit = ast_mkConst(L_int, $5, @5, @5);
		$$ = ast_mkBinOp(L_OP_EQUALS, $3, lit, @3, @5);
		$$->next = $1;
		$$->node.loc.beg = @1.beg;
	}
	;

pragma:
	  T_PRAGMA pragma_expr_list
	{
		REVERSE(Expr, next, $2);
		$$ = $2;
		$$->node.loc.beg = @1.beg;
	}
	;

opt_attribute:
	  T_ATTRIBUTE "(" argument_expr_list ")"
	{
		REVERSE(Expr, next, $3);
		$$ = $3;
		$$->node.loc.beg = @1.beg;
		$$->node.loc.end = @4.end;
	}
	|	{ $$ = NULL; }
	;

unlabeled_stmt:
	  single_stmt
	| compound_stmt
	;

single_stmt:
	  selection_stmt
	{
		$$ = ast_mkStmt(L_STMT_COND, NULL, @1, @1);
		$$->u.cond = $1;
	}
	| iteration_stmt
	{
		$$ = ast_mkStmt(L_STMT_LOOP, NULL, @1, @1);
		$$->u.loop = $1;
	}
	| switch_stmt
	{
		$$ = ast_mkStmt(L_STMT_SWITCH, NULL, @1, @1);
		$$->u.swich = $1;
	}
	| foreach_stmt
	{
		$$ = ast_mkStmt(L_STMT_FOREACH, NULL, @1, @1);
		$$->u.foreach = $1;
	}
	| expr ";"
	{
		$$ = ast_mkStmt(L_STMT_EXPR, NULL, @1, @1);
		$$->u.expr = $1;
	}
	| T_BREAK ";"
	{
		$$ = ast_mkStmt(L_STMT_BREAK, NULL, @1, @1);
	}
	| T_CONTINUE ";"
	{
		$$ = ast_mkStmt(L_STMT_CONTINUE, NULL, @1, @1);
	}
	| T_RETURN ";"
	{
		$$ = ast_mkStmt(L_STMT_RETURN, NULL, @1, @1);
	}
	| T_RETURN expr ";"
	{
		$$ = ast_mkStmt(L_STMT_RETURN, NULL, @1, @2);
		$$->u.expr = $2;
	}
	| T_GOTO T_ID ";"
	{
		$$ = ast_mkStmt(L_STMT_GOTO, NULL, @1, @3);
		$$->u.label = $2;
	}
	| "try" compound_stmt T_ID "(" expr ")" compound_stmt
	{
		/*
		 * We don't want to make "catch" a keyword since it's a Tcl
		 * function name, so allow any ID here but check it.
		 */
		unless (!strcmp($3, "catch")) {
			L_synerr2("syntax error -- expected 'catch'", @3.beg);
		}
		$$ = ast_mkStmt(L_STMT_TRY, NULL, @1, @7);
		$$->u.try = ast_mkTry($2, $5, $7);
	}
	| "try" compound_stmt T_ID compound_stmt
	{
		$$ = ast_mkStmt(L_STMT_TRY, NULL, @1, @4);
		$$->u.try = ast_mkTry($2, NULL, $4);
	}
	| ";"	{ $$ = NULL; }
	;

selection_stmt:
	  T_IF "(" expr ")" compound_stmt optional_else
	{
		$$ = ast_mkIfUnless($3, $5, $6, @1, @6);
	}
	/* If you have no curly braces, you get no else. */
	| T_IF "(" expr ")" single_stmt
	{
		$$ = ast_mkIfUnless($3, $5, NULL, @1, @5);
	}
	| T_UNLESS "(" expr ")" compound_stmt optional_else
	{
		$$ = ast_mkIfUnless($3, $6, $5, @1, @6);
	}
	| T_UNLESS "(" expr ")" single_stmt
	{
		$$ = ast_mkIfUnless($3, NULL, $5, @1, @5);
	}
	;

switch_stmt:
	  T_SWITCH "(" expr ")" "{" switch_cases "}"
	{
		Case	*c, *def;

		for (c = $6, def = NULL; c; c = c->next) {
			if (c->expr) continue;
			if (def) {
				L_errf(c,
				"multiple default cases in switch statement");
			}
			def = c;
		}
		$$ = ast_mkSwitch($3, $6, @1, @7);
	}
	;

switch_cases:
	  switch_cases switch_case
	{
		if ($1) {
			APPEND(Case, next, $1, $2);
			$$ = $1;
		} else {
			$$ = $2;
		}
	}
	| /* epsilon */		{ $$ = NULL; }
	;

switch_case:
	  "case" re_start_case case_expr ":" opt_stmt_list
	{
		REVERSE(Stmt, next, $5);
		$$ = ast_mkCase($3, $5, @1, @5);
	}
	| "default" ":" opt_stmt_list
	{
		/* The default case is distinguished by a NULL expr. */
		REVERSE(Stmt, next, $3);
		$$ = ast_mkCase(NULL, $3, @1, @2);
	}
	;

case_expr:
	  regexp_literal_mod
	{
		if ($1->flags & L_EXPR_RE_G) {
			L_errf($1, "illegal regular expression modifier");
		}
	}
	| expr
	;

optional_else:
	/* Else clause must either have curly braces or be another if/unless. */
	  T_ELSE compound_stmt
	{
		$$ = $2;
		$$->node.loc = @1;
	}
	| T_ELSE selection_stmt
	{
		$$ = ast_mkStmt(L_STMT_COND, NULL, @1, @2);
		$$->u.cond = $2;
	}
	| /* epsilon */		{ $$ = NULL; }
	;

iteration_stmt:
	  T_WHILE "(" expr ")" stmt
	{
		$$ = ast_mkLoop(L_LOOP_WHILE, NULL, $3, NULL, $5, @1, @5);
	}
	| T_DO stmt T_WHILE "(" expr ")" ";"
	{
		$$ = ast_mkLoop(L_LOOP_DO, NULL, $5, NULL, $2, @1, @6);
	}
	| T_FOR "(" expression_stmt expression_stmt ")" stmt
	{
		$$ = ast_mkLoop(L_LOOP_FOR, $3, $4, NULL, $6, @1, @6);
	}
	| T_FOR "(" expression_stmt expression_stmt expr ")" stmt
	{
		$$ = ast_mkLoop(L_LOOP_FOR, $3, $4, $5, $7, @1, @7);
	}
	;

foreach_stmt:
	  T_FOREACH "(" id "=>" id id expr ")" stmt
	{
		$$ = ast_mkForeach($7, $3, $5, $9, @1, @9);
		unless (isid($6, "in")) {
			L_synerr2("syntax error -- expected 'in' in foreach",
				  @6.beg);
		}
	}
	| T_FOREACH "(" id_list id expr ")" stmt
	{
		$$ = ast_mkForeach($5, $3, NULL, $7, @1, @7);
		unless (isid($4, "in")) {
			L_synerr2("syntax error -- expected 'in' in foreach",
				  @4.beg);
		}
	}
	;

expression_stmt:
	  ";"		{ $$ = NULL; }
	| expr ";"
	;

opt_stmt_list:
	  stmt_list
	| { $$ = NULL; }
	;

stmt_list:
	  stmt
	{
		REVERSE(Stmt, next, $1);
		$$ = $1;
	}
	| stmt_list stmt
	{
		if ($2) {
			REVERSE(Stmt, next, $2);
			APPEND(Stmt, next, $2, $1);
			$$ = $2;
		} else {
			// Empty stmt.
			$$ = $1;
		}
	}
	;

parameter_list:
	  parameter_decl_list
	{
		VarDecl *v;
		REVERSE(VarDecl, next, $1);
		for (v = $1; v; v = v->next) {
			v->flags |= SCOPE_LOCAL | DECL_LOCAL_VAR;
		}
		$$ = $1;
		/*
		 * Special case a parameter list of "void" -- a single
		 * formal of type void with no arg name.  This really
		 * means there are no args.
		 */
		if ($1 && !$1->next && !$1->id && ($1->type == L_void)) {
			$$ = NULL;
		}
	}
	| /* epsilon */	{ $$ = NULL; }
	;

parameter_decl_list:
	  parameter_decl
	| parameter_decl_list "," parameter_decl
	{
		$3->next = $1;
		$$ = $3;
		$$->node.loc = @1;
	}
	;

parameter_decl:
	  parameter_attrs type_specifier opt_declarator
	{
		if ($3) {
			L_set_declBaseType($3, $2);
			$$ = $3;
		} else {
			$$ = ast_mkVarDecl($2, NULL, @2, @2);
			if (isnameoftype($2)) $$->flags |= DECL_REF;
		}
		$$->flags |= $1;
		$$->node.loc = @1;
	}
	| parameter_attrs T_ELLIPSIS id
	{
		Type *t = type_mkArray(NULL, L_poly);
		$$ = ast_mkVarDecl(t, $3, @1, @3);
		$$->flags |= $1 | DECL_REST_ARG;
	}
	;

parameter_attrs:
	  parameter_attrs T_ARGUSED	{ $$ = $1 | DECL_ARGUSED; }
	| parameter_attrs T_OPTIONAL	{ $$ = $1 | DECL_OPTIONAL; }
	| parameter_attrs T_MUSTBETYPE	{ $$ = $1 | DECL_NAME_EQUIV; }
	| /* epsilon */	{ $$ = 0; }
	;

argument_expr_list:
	  expr %prec T_COMMA
	| option_arg
	| option_arg expr %prec T_COMMA
	{
		$2->next = $1;
		$$ = $2;
		$$->node.loc = @1;
	}
	| argument_expr_list "," expr
	{
		$3->next = $1;
		$$ = $3;
		$$->node.loc.end = @3.end;
	}
	| argument_expr_list "," option_arg
	{
		$3->next = $1;
		$$ = $3;
		$$->node.loc.end = @3.end;
	}
	| argument_expr_list "," option_arg expr %prec T_COMMA
	{
		$4->next = $3;
		$3->next = $1;
		$$ = $4;
		$$->node.loc.end = @4.end;
	}
	;

/*
 * option_arg is an actual parameter "arg:" that becomes "-arg".
 * Allow both "T_ID:" and "default:" to overcome a nasty grammar
 * conflict with statement labels that otherwise results.  The scanner
 * returns T_DEFAULT T_COLON when it sees "default:" but returns T_ID
 * T_COLON for the other cases even if they include a reserved word.
 */
option_arg:
	  T_ID ":"
	{
		char	*s = cksprintf("-%s", $1);
		$$ = ast_mkConst(L_string, s, @1, @2);
		ckfree($1);
	}
	| "default" ":"
	{
		char	*s = cksprintf("-default");
		$$ = ast_mkConst(L_string, s, @1, @2);
	}
	;

expr:
	  "(" expr ")"
	{
		$$ = $2;
		$$->node.loc = @1;
		$$->node.loc.end = @3.end;
	}
	| "(" type_specifier ")" expr %prec PREFIX_INCDEC
	{
		// This is a binop where an arg is a Type*.
		$$ = ast_mkBinOp(L_OP_CAST, (Expr *)$2, $4, @1, @4);
	}
	| "(" T_EXPAND ")" expr %prec PREFIX_INCDEC
	{
		$$ = ast_mkUnOp(L_OP_EXPAND, $4, @1, @4);
	}
	| T_BANG expr
	{
		$$ = ast_mkUnOp(L_OP_BANG, $2, @1, @2);
	}
	| T_BITNOT expr
	{
		$$ = ast_mkUnOp(L_OP_BITNOT, $2, @1, @2);
	}
	| T_BITAND expr %prec ADDRESS
	{
		$$ = ast_mkUnOp(L_OP_ADDROF, $2, @1, @2);
	}
	| T_MINUS expr %prec UMINUS
	{
		$$ = ast_mkUnOp(L_OP_UMINUS, $2, @1, @2);
	}
	| T_PLUS expr %prec UPLUS
	{
		$$ = ast_mkUnOp(L_OP_UPLUS, $2, @1, @2);
	}
	| T_PLUSPLUS expr %prec PREFIX_INCDEC
	{
		$$ = ast_mkUnOp(L_OP_PLUSPLUS_PRE, $2, @1, @2);
	}
	| T_MINUSMINUS expr %prec PREFIX_INCDEC
	{
		$$ = ast_mkUnOp(L_OP_MINUSMINUS_PRE, $2, @1, @2);
	}
	| expr T_PLUSPLUS
	{
		$$ = ast_mkUnOp(L_OP_PLUSPLUS_POST, $1, @1, @2);
	}
	| expr T_MINUSMINUS
	{
		$$ = ast_mkUnOp(L_OP_MINUSMINUS_POST, $1, @1, @2);
	}
	| expr T_EQTWID regexp_literal_mod
	{
		$$ = ast_mkBinOp(L_OP_EQTWID, $1, $3, @1, @3);
	}
	| expr T_BANGTWID regexp_literal_mod
	{
		$$ = ast_mkBinOp(L_OP_BANGTWID, $1, $3, @1, @3);
	}
	| expr T_EQTWID regexp_literal subst_literal T_RE_MODIFIER
	{
		if (strchr($5, 'i')) $3->flags |= L_EXPR_RE_I;
		if (strchr($5, 'g')) $3->flags |= L_EXPR_RE_G;
		$$ = ast_mkTrinOp(L_OP_EQTWID, $1, $3, $4, @1, @5);
		ckfree($5);
	}
	| expr T_STAR expr
	{
		$$ = ast_mkBinOp(L_OP_STAR, $1, $3, @1, @3);
	}
	| expr T_SLASH expr
	{
		$$ = ast_mkBinOp(L_OP_SLASH, $1, $3, @1, @3);
	}
	| expr T_PERC expr
	{
		$$ = ast_mkBinOp(L_OP_PERC, $1, $3, @1, @3);
	}
	| expr T_PLUS expr
	{
		$$ = ast_mkBinOp(L_OP_PLUS, $1, $3, @1, @3);
	}
	| expr T_MINUS expr
	{
		$$ = ast_mkBinOp(L_OP_MINUS, $1, $3, @1, @3);
	}
	| expr T_EQ expr
	{
		$$ = ast_mkBinOp(L_OP_STR_EQ, $1, $3, @1, @3);
	}
	| expr T_NE expr
	{
		$$ = ast_mkBinOp(L_OP_STR_NE, $1, $3, @1, @3);
	}
	| expr T_LT expr
	{
		$$ = ast_mkBinOp(L_OP_STR_LT, $1, $3, @1, @3);
	}
	| expr T_LE expr
	{
		$$ = ast_mkBinOp(L_OP_STR_LE, $1, $3, @1, @3);
	}
	| expr T_GT expr
	{
		$$ = ast_mkBinOp(L_OP_STR_GT, $1, $3, @1, @3);
	}
	| expr T_GE expr
	{
		$$ = ast_mkBinOp(L_OP_STR_GE, $1, $3, @1, @3);
	}
	| expr T_EQUALEQUAL expr
	{
		$$ = ast_mkBinOp(L_OP_EQUALEQUAL, $1, $3, @1, @3);
	}
	| T_EQ "(" expr "," expr ")"
	{
		$$ = ast_mkBinOp(L_OP_EQUALEQUAL, $3, $5, @1, @6);
	}
	| expr T_NOTEQUAL expr
	{
		$$ = ast_mkBinOp(L_OP_NOTEQUAL, $1, $3, @1, @3);
	}
	| expr T_GREATER expr
	{
		$$ = ast_mkBinOp(L_OP_GREATER, $1, $3, @1, @3);
	}
	| expr T_GREATEREQ expr
	{
		$$ = ast_mkBinOp(L_OP_GREATEREQ, $1, $3, @1, @3);
	}
	| expr T_LESSTHAN expr
	{
		$$ = ast_mkBinOp(L_OP_LESSTHAN, $1, $3, @1, @3);
	}
	| expr T_LESSTHANEQ expr
	{
		$$ = ast_mkBinOp(L_OP_LESSTHANEQ, $1, $3, @1, @3);
	}
	| expr T_ANDAND expr
	{
		$$ = ast_mkBinOp(L_OP_ANDAND, $1, $3, @1, @3);
	}
	| expr T_OROR expr
	{
		$$ = ast_mkBinOp(L_OP_OROR, $1, $3, @1, @3);
	}
	| expr T_LSHIFT expr
	{
		$$ = ast_mkBinOp(L_OP_LSHIFT, $1, $3, @1, @3);
	}
	| expr T_RSHIFT expr
	{
		$$ = ast_mkBinOp(L_OP_RSHIFT, $1, $3, @1, @3);
	}
	| expr T_BITOR expr
	{
		$$ = ast_mkBinOp(L_OP_BITOR, $1, $3, @1, @3);
	}
	| expr T_BITAND expr
	{
		$$ = ast_mkBinOp(L_OP_BITAND, $1, $3, @1, @3);
	}
	| expr T_BITXOR expr
	{
		$$ = ast_mkBinOp(L_OP_BITXOR, $1, $3, @1, @3);
	}
	| id
	| string_literal
	| cmdsubst_literal
	| T_INT_LITERAL
	{
		$$ = ast_mkConst(L_int, $1, @1, @1);
	}
	| T_FLOAT_LITERAL
	{
		$$ = ast_mkConst(L_float, $1, @1, @1);
	}
	| id "(" argument_expr_list ")"
	{
		REVERSE(Expr, next, $3);
		$$ = ast_mkFnCall($1, $3, @1, @4);
	}
	| id "(" ")"
	{
		$$ = ast_mkFnCall($1, NULL, @1, @3);
	}
	| T_STRING "(" argument_expr_list ")"
	{
		Expr *id = ast_mkId("string", @1, @1);
		REVERSE(Expr, next, $3);
		$$ = ast_mkFnCall(id, $3, @1, @4);
	}
	| T_SPLIT "(" re_start_split regexp_literal_mod "," argument_expr_list ")"
	{
		Expr *id = ast_mkId("split", @1, @1);
		REVERSE(Expr, next, $6);
		$4->next = $6;
		$$ = ast_mkFnCall(id, $4, @1, @7);
	}
	/*
	 * Even though there is no regexp arg in this form, the
	 * re_start_split is necessary to be able to scan either a
	 * regexp or a non-regexp first argument.  The scanner makes
	 * that decision based on the first one or two characters and
	 * then returns appropriate tokens.
	 */
	| T_SPLIT "(" re_start_split argument_expr_list ")"
	{
		Expr *id = ast_mkId("split", @1, @1);
		REVERSE(Expr, next, $4);
		$$ = ast_mkFnCall(id, $4, @1, @5);
	}
	/* this is to allow calling Tk widget functions */
	| dotted_id "(" argument_expr_list ")"
	{
		REVERSE(Expr, next, $3);
		$$ = ast_mkFnCall($1, $3, @1, @4);
	}
	| dotted_id "(" ")"
	{
		$$ = ast_mkFnCall($1, NULL, @1, @3);
	}
	| expr T_EQUALS expr
	{
		$$ = ast_mkBinOp(L_OP_EQUALS, $1, $3, @1, @3);
	}
	| expr T_EQPLUS expr
	{
		$$ = ast_mkBinOp(L_OP_EQPLUS, $1, $3, @1, @3);
	}
	| expr T_EQMINUS expr
	{
		$$ = ast_mkBinOp(L_OP_EQMINUS, $1, $3, @1, @3);
	}
	| expr T_EQSTAR expr
	{
		$$ = ast_mkBinOp(L_OP_EQSTAR, $1, $3, @1, @3);
	}
	| expr T_EQSLASH expr
	{
		$$ = ast_mkBinOp(L_OP_EQSLASH, $1, $3, @1, @3);
	}
	| expr T_EQPERC expr
	{
		$$ = ast_mkBinOp(L_OP_EQPERC, $1, $3, @1, @3);
	}
	| expr T_EQBITAND expr
	{
		$$ = ast_mkBinOp(L_OP_EQBITAND, $1, $3, @1, @3);
	}
	| expr T_EQBITOR expr
	{
		$$ = ast_mkBinOp(L_OP_EQBITOR, $1, $3, @1, @3);
	}
	| expr T_EQBITXOR expr
	{
		$$ = ast_mkBinOp(L_OP_EQBITXOR, $1, $3, @1, @3);
	}
	| expr T_EQLSHIFT expr
	{
		$$ = ast_mkBinOp(L_OP_EQLSHIFT, $1, $3, @1, @3);
	}
	| expr T_EQRSHIFT expr
	{
		$$ = ast_mkBinOp(L_OP_EQRSHIFT, $1, $3, @1, @3);
	}
	| expr T_EQDOT expr
	{
		$$ = ast_mkBinOp(L_OP_EQDOT, $1, $3, @1, @3);
	}
	| T_DEFINED "(" expr ")"
	{
		$$ = ast_mkUnOp(L_OP_DEFINED, $3, @1, @4);
	}
	| expr "[" expr "]"
	{
		$$ = ast_mkBinOp(L_OP_ARRAY_INDEX, $1, $3, @1, @4);
	}
	| expr "{" expr "}"
	{
		$$ = ast_mkBinOp(L_OP_HASH_INDEX, $1, $3, @1, @4);
	}
	| expr T_STRCAT expr
	{
		$$ = ast_mkBinOp(L_OP_CONCAT, $1, $3, @1, @3);
	}
	| expr "." T_ID
	{
		$$ = ast_mkBinOp(L_OP_DOT, $1, NULL, @1, @3);
		$$->str = $3;
	}
	| expr "->" T_ID
	{
		$$ = ast_mkBinOp(L_OP_POINTS, $1, NULL, @1, @3);
		$$->str = $3;
	}
	| T_TYPE "." T_ID
	{
		// This is a binop where an arg is a Type*.
		$$ = ast_mkBinOp(L_OP_CLASS_INDEX, (Expr *)$1.t, NULL, @1, @3);
		$$->str = $3;
	}
	| T_TYPE "->" T_ID
	{
		// This is a binop where an arg is a Type*.
		$$ = ast_mkBinOp(L_OP_CLASS_INDEX, (Expr *)$1.t, NULL, @1, @3);
		$$->str = $3;
	}
	| expr "," expr
	{
		$$ = ast_mkBinOp(L_OP_COMMA, $1, $3, @1, @3);
	}
	| expr "[" expr T_DOTDOT expr "]"
	{
		$$ = ast_mkTrinOp(L_OP_ARRAY_SLICE, $1, $3, $5, @1, @3);
	}
	/*
	 * We don't really need to open a scope here, but it doesn't hurt, and
	 * it avoids a shift/reduce conflict with a compound_stmt production.
	 */
	| "{" enter_scope list "}"
	{
		$$ = $3;
		$$->node.loc = @1;
		$$->node.loc.end = @4.end;
		L_scope_leave();
	}
	| "{" "}"
	{
		$$ = ast_mkBinOp(L_OP_LIST, NULL, NULL, @1, @2);
	}
	| expr "?" expr ":" expr %prec T_QUESTION
	{
		$$ = ast_mkTrinOp(L_OP_TERNARY_COND, $1, $3, $5, @1, @5);
	}
	| "<" expr ">"
	{
		$$ = ast_mkUnOp(L_OP_FILE, $2, @1, @3);
	}
	| "<" ">"
	{
		$$ = ast_mkUnOp(L_OP_FILE, NULL, @1, @2);
	}
	;

re_start_split:
		{ L_lex_begReArg(0); }
	;

re_start_case:
		{ L_lex_begReArg(1); }
	;

id:
	  T_ID
	{
		$$ = ast_mkId($1, @1, @1);
		ckfree($1);
	}
	;

id_list:
	id
	| id "," id_list
	{
		$$ = $1;
		$$->next = $3;
		$$->node.loc.end = @3.end;
	}
	;

compound_stmt:
	  "{" enter_scope "}"
	{
		$$ = ast_mkStmt(L_STMT_BLOCK, NULL, @1, @3);
		$$->u.block = ast_mkBlock(NULL, NULL, @1, @3);
		L_scope_leave();
	}
	| "{" enter_scope stmt_list "}"
	{
		REVERSE(Stmt, next, $3);
		$$ = ast_mkStmt(L_STMT_BLOCK, NULL, @1, @4);
		$$->u.block = ast_mkBlock(NULL, $3, @1, @4);
		L_scope_leave();
	}
	| "{" enter_scope declaration_list "}"
	{
		VarDecl	*v;
		REVERSE(VarDecl, next, $3);
		for (v = $3; v; v = v->next) {
			v->flags |= SCOPE_LOCAL | DECL_LOCAL_VAR;
		}
		$$ = ast_mkStmt(L_STMT_BLOCK, NULL, @1, @4);
		$$->u.block = ast_mkBlock($3, NULL, @1, @4);
		L_scope_leave();
	}
	| "{" enter_scope declaration_list stmt_list "}"
	{
		VarDecl	*v;
		REVERSE(VarDecl, next, $3);
		for (v = $3; v; v = v->next) {
			v->flags |= SCOPE_LOCAL | DECL_LOCAL_VAR;
		}
		REVERSE(Stmt, next, $4);
		$$ = ast_mkStmt(L_STMT_BLOCK, NULL, @1, @5);
		$$->u.block = ast_mkBlock($3, $4, @1, @5);
		L_scope_leave();
	}
	;

enter_scope:
	   /* epsilon */ %prec HIGHEST { L_scope_enter(); }
	;

declaration_list:
	  declaration
	| declaration_list declaration
	{
		/*
		 * Each declaration is a list of declarators.  Here we
		 * append the lists.
		 */
		APPEND(VarDecl, next, $2, $1);
		$$ = $2;
	}
	;

declaration:
	  declaration2 ";"
	| decl_qualifier declaration2 ";"
	{
		VarDecl *v;
		for (v = $2; v; v = v->next) {
			v->flags |= $1;
		}
		$$ = $2;
		$$->node.loc = @1;
	}
	;

decl_qualifier:
	  T_PRIVATE	{ $$ = DECL_PRIVATE; }
	| T_PUBLIC	{ $$ = DECL_PUBLIC; }
	| T_EXTERN	{ $$ = DECL_EXTERN; }
	;

declaration2:
	  type_specifier init_declarator_list
	{
		/* Don't REVERSE $2; it's done as part of declaration_list. */
		VarDecl *v;
		for (v = $2; v; v = v->next) {
			L_set_declBaseType(v, $1);
		}
		$$ = $2;
	}
	;

init_declarator_list:
	  init_declarator
	| init_declarator_list "," init_declarator
	{
		$3->next = $1;
		$$ = $3;
	}
	;

declarator_list:
	  declarator
	| declarator_list "," declarator
	{
		$3->next = $1;
		$$ = $3;
	}
	;

init_declarator:
	  declarator
	| declarator T_EQUALS expr
	{
		$1->initializer = ast_mkBinOp(L_OP_EQUALS, $1->id, $3, @3, @3);
		$$ = $1;
		$$->node.loc.end = @3.end;
	}
	;

opt_declarator:
	  declarator
	| { $$ = NULL; }
	;

declarator:
	  id array_or_hash_type
	{
		$$ = ast_mkVarDecl($2, $1, @1, @2);
	}
	| T_TYPE array_or_hash_type
	{
		Expr *id = ast_mkId($1.s, @1, @1);
		$$ = ast_mkVarDecl($2, id, @1, @2);
		if (isnameoftype($1.t)) $$->flags |= DECL_REF;
		ckfree($1.s);
	}
	| T_BITAND id array_or_hash_type
	{
		Type *t = type_mkNameOf($3);
		$$ = ast_mkVarDecl(t, $2, @1, @3);
		$$->flags |= DECL_REF;
	}
	| T_BITAND id "(" parameter_list ")"
	{
		Type *tf = type_mkFunc(NULL, $4);
		Type *tn = type_mkNameOf(tf);
		$$ = ast_mkVarDecl(tn, $2, @1, @5);
		$$->flags |= DECL_REF;
	}
	;

/* Right recursion OK here since depth is typically low. */
array_or_hash_type:
	  /* epsilon */
	{
		$$ = NULL;
	}
	| "[" expr "]" array_or_hash_type
	{
		$$ = type_mkArray($2, $4);
	}
	| "[" "]" array_or_hash_type
	{
		$$ = type_mkArray(NULL, $3);
	}
	| "{" scalar_type_specifier "}" array_or_hash_type
	{
		$$ = type_mkHash($2, $4);
	}
	;

type_specifier:
	  scalar_type_specifier array_or_hash_type
	{
		if ($2) {
			L_set_baseType($2, $1);
			$$ = $2;
		} else {
			$$ = $1;
		}
	}
	| struct_specifier array_or_hash_type
	{
		if ($2) {
			L_set_baseType($2, $1);
			$$ = $2;
		} else {
			$$ = $1;
		}
	}
	;

scalar_type_specifier:
	  T_STRING	{ $$ = L_string; }
	| T_INT		{ $$ = L_int; }
	| T_FLOAT	{ $$ = L_float; }
	| T_POLY	{ $$ = L_poly; }
	| T_WIDGET	{ $$ = L_widget; }
	| T_VOID	{ $$ = L_void; }
	| T_TYPE	{ $$ = $1.t; ckfree($1.s); }
	;

struct_specifier:
	  T_STRUCT T_ID "{" struct_decl_list "}"
	{
		REVERSE(VarDecl, next, $4);
		$$ = L_struct_store($2, $4);
		ckfree($2);
	}
	| T_STRUCT "{" struct_decl_list "}"
	{
		REVERSE(VarDecl, next, $3);
		(void)L_struct_store(NULL, $3);  // to sanity check member types
		$$ = type_mkStruct(NULL, $3);
	}
	| T_STRUCT T_ID
	{
		$$ = L_struct_lookup($2, FALSE);
		ckfree($2);
	}
	;

struct_decl_list:
	  struct_decl
	| struct_decl_list struct_decl
	{
		APPEND(VarDecl, next, $2, $1);
		$$ = $2;
		$$->node.loc = @1;
	}
	;

struct_decl:
	  struct_declarator_list ";"	{ $$->node.loc.end = @2.end; }
	;

struct_declarator_list:
	  type_specifier declarator_list
	{
		VarDecl *v;
		for (v = $2; v; v = v->next) {
			L_set_declBaseType(v, $1);
		}
		$$ = $2;
		$$->node.loc = @1;
	}
	;

list:
	  list_element
	| list "," list_element
	{
		APPEND(Expr, b, $1, $3);
		$$ = $1;
	}
	| list ","
	;

list_element:
	  expr %prec HIGHEST
	{
		$$ = ast_mkBinOp(L_OP_LIST, $1, NULL, @1, @1);
	}
	| expr "=>" expr %prec HIGHEST
	{
		Expr *kv = ast_mkBinOp(L_OP_KV, $1, $3, @1, @3);
		$$ = ast_mkBinOp(L_OP_LIST, kv, NULL, @1, @3);
	}
	;

string_literal:
	  T_STR_LITERAL
	{
		$$ = ast_mkConst(L_string, $1, @1, @1);
	}
	| interpolated_expr T_STR_LITERAL
	{
		Expr *right = ast_mkConst(L_string, $2, @2, @2);
		$$ = ast_mkBinOp(L_OP_INTERP_STRING, $1, right, @1, @2);
	}
	| here_doc_backtick T_STR_LITERAL
	{
		Expr *right = ast_mkConst(L_string, $2, @2, @2);
		$$ = ast_mkBinOp(L_OP_INTERP_STRING, $1, right, @1, @2);
	}
	;

here_doc_backtick:
	  T_START_BACKTICK T_STR_BACKTICK
	{
		Expr *left  = ast_mkConst(L_string, $1, @1, @1);
		Expr *right = ast_mkUnOp(L_OP_CMDSUBST, NULL, @2, @2);
		right->str = $2;
		$$ = ast_mkBinOp(L_OP_INTERP_STRING, left, right, @1, @2);
	}
	| here_doc_backtick T_START_BACKTICK T_STR_BACKTICK
	{
		Expr *middle = ast_mkConst(L_string, $2, @2, @2);
		Expr *right  = ast_mkUnOp(L_OP_CMDSUBST, NULL, @3, @3);
		right->str = $3;
		$$ = ast_mkTrinOp(L_OP_INTERP_STRING, $1, middle, right,
				  @1, @3);
	}
	;

cmdsubst_literal:
	  T_STR_BACKTICK
	{
		$$ = ast_mkUnOp(L_OP_CMDSUBST, NULL, @1, @1);
		$$->str = $1;
	}
	| interpolated_expr T_STR_BACKTICK
	{
		$$ = ast_mkUnOp(L_OP_CMDSUBST, $1, @1, @2);
		$$->str = $2;
	}
	;

regexp_literal:
	  T_RE
	{
		$$ = ast_mkRegexp($1, @1, @1);
	}
	| interpolated_expr_re T_RE
	{
		Expr *right = ast_mkConst(L_string, $2, @2, @2);
		$$ = ast_mkBinOp(L_OP_INTERP_RE, $1, right, @1, @2);
	}
	;

regexp_literal_mod:
	  regexp_literal T_RE_MODIFIER
	{
		/* Note: the scanner catches illegal modifiers. */
		if (strchr($2, 'i')) $1->flags |= L_EXPR_RE_I;
		if (strchr($2, 'g')) $1->flags |= L_EXPR_RE_G;
		if (strchr($2, 'l')) $1->flags |= L_EXPR_RE_L;
		if (strchr($2, 't')) $1->flags |= L_EXPR_RE_T;
		ckfree($2);
		$$ = $1;
	}
	;

subst_literal:
	  T_SUBST
	{
		$$ = ast_mkConst(L_string, $1, @1, @1);
	}
	| interpolated_expr_re T_SUBST
	{
		Expr *right = ast_mkConst(L_string, $2, @2, @2);
		$$ = ast_mkBinOp(L_OP_INTERP_RE, $1, right, @1, @2);
	}
	;

interpolated_expr:
	  T_LEFT_INTERPOL expr T_RIGHT_INTERPOL
	{
		Expr *left = ast_mkConst(L_string, $1, @1, @1);
		$$ = ast_mkBinOp(L_OP_INTERP_STRING, left, $2, @1, @3);
	}
	| interpolated_expr T_LEFT_INTERPOL expr T_RIGHT_INTERPOL
	{
		Expr *middle = ast_mkConst(L_string, $2, @2, @2);
		$$ = ast_mkTrinOp(L_OP_INTERP_STRING, $1, middle, $3, @1, @4);
	}
	;

interpolated_expr_re:
	  T_LEFT_INTERPOL_RE expr T_RIGHT_INTERPOL_RE
	{
		Expr *left = ast_mkConst(L_string, $1, @1, @1);
		$$ = ast_mkBinOp(L_OP_INTERP_STRING, left, $2, @1, @3);
	}
	| interpolated_expr_re T_LEFT_INTERPOL_RE expr T_RIGHT_INTERPOL_RE
	{
		Expr *middle = ast_mkConst(L_string, $2, @2, @2);
		$$ = ast_mkTrinOp(L_OP_INTERP_STRING, $1, middle, $3, @1, @4);
	}
	;

dotted_id:
	  "."
	{
		$$ = ast_mkId(".", @1, @1);
	}
	| dotted_id_1
	{
		$$ = ast_mkId(Tcl_GetString($1), @1, @1);
		Tcl_DecrRefCount($1);
	}
	;

dotted_id_1:
	  "." T_ID
	{
		$$ = Tcl_NewObj();
		Tcl_IncrRefCount($$);
		Tcl_AppendToObj($$, ".", 1);
		Tcl_AppendToObj($$, $2, -1);
		ckfree($2);
	}
	| dotted_id_1 "." T_ID
	{
		Tcl_AppendToObj($1, ".", 1);
		Tcl_AppendToObj($1, $3, -1);
		$$ = $1;
		ckfree($3);
	}
	;
%%