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
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
|
%{
/* $OpenBSD: cgram.y,v 1.15 2005/12/07 01:55:12 cloder Exp $ */
/* $NetBSD: cgram.y,v 1.8 1995/10/02 17:31:35 jpo Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Jochen Pohl for
* The NetBSD Project.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef lint
static char rcsid[] = "$OpenBSD: cgram.y,v 1.15 2005/12/07 01:55:12 cloder Exp $";
#endif
#include <stdlib.h>
#include <limits.h>
#include "lint1.h"
/*
* Contains the level of current declaration. 0 is extern.
* Used for symbol table entries.
*/
int blklev;
/*
* level for memory allocation. Normaly the same as blklev.
* An exeption is the declaration of arguments in prototypes. Memory
* for these can't be freed after the declaration, but symbols must
* be removed from the symbol table after the declaration.
*/
int mblklev;
static int toicon(tnode_t *);
static void idecl(sym_t *, int);
static void ignuptorp(void);
%}
%union {
int y_int;
val_t *y_val;
sbuf_t *y_sb;
sym_t *y_sym;
op_t y_op;
scl_t y_scl;
tspec_t y_tspec;
tqual_t y_tqual;
attr_t y_attr;
attrnode_t *y_attrnode;
type_t *y_type;
tnode_t *y_tnode;
strg_t *y_strg;
pqinf_t *y_pqinf;
};
%token T_LBRACE T_RBRACE T_LBRACK T_RBRACK T_LPARN T_RPARN
%token <y_op> T_STROP
%token <y_op> T_UNOP
%token <y_op> T_INCDEC
%token T_SIZEOF
%token <y_op> T_MULT
%token <y_op> T_DIVOP
%token <y_op> T_ADDOP
%token <y_op> T_SHFTOP
%token <y_op> T_RELOP
%token <y_op> T_EQOP
%token <y_op> T_AND
%token <y_op> T_XOR
%token <y_op> T_OR
%token <y_op> T_LOGAND
%token <y_op> T_LOGOR
%token T_QUEST
%token T_COLON
%token <y_op> T_ASSIGN
%token <y_op> T_OPASS
%token T_COMMA
%token T_SEMI
%token T_ELLIPSE
/* storage classes (extern, static, auto, register and typedef) */
%token <y_scl> T_SCLASS
/* types (char, int, short, long, unsigned, signed, float, double, void) */
%token <y_tspec> T_TYPE
/* qualifiers (const, volatile, restrict) */
%token <y_tqual> T_QUAL
/* attributes (noreturn, format, etc.) */
%token <y_attr> T_ATTR
/* struct or union */
%token <y_tspec> T_SOU
/* enum */
%token T_ENUM
/* remaining keywords */
%token T_CASE
%token T_DEFAULT
%token T_IF
%token T_ELSE
%token T_SWITCH
%token T_DO
%token T_WHILE
%token T_FOR
%token T_GOTO
%token T_CONTINUE
%token T_BREAK
%token T_RETURN
%token T_ASM
%token T_LEQUAL
%token T_ATTRIBUTE
%left T_COMMA
%right T_ASSIGN T_OPASS
%right T_QUEST T_COLON
%left T_LOGOR
%left T_LOGAND
%left T_OR
%left T_XOR
%left T_AND
%left T_EQOP
%left T_RELOP
%left T_SHFTOP
%left T_ADDOP
%left T_MULT T_DIVOP
%right T_UNOP T_INCDEC T_SIZEOF
%left T_LPARN T_LBRACK T_STROP
%token <y_sb> T_NAME
%token <y_sb> T_TYPENAME
%token <y_val> T_CON
%token <y_strg> T_STRING
%type <y_sym> func_decl
%type <y_sym> notype_decl
%type <y_sym> type_decl
%type <y_type> typespec
%type <y_type> clrtyp_typespec
%type <y_type> notype_typespec
%type <y_type> struct_spec
%type <y_type> enum_spec
%type <y_sym> struct_tag
%type <y_sym> enum_tag
%type <y_tspec> struct
%type <y_sym> struct_declaration
%type <y_sb> identifier
%type <y_sym> member_declaration_list_with_rbrace
%type <y_sym> member_declaration_list
%type <y_sym> member_declaration
%type <y_sym> notype_member_decls
%type <y_sym> type_member_decls
%type <y_sym> notype_member_decl
%type <y_sym> type_member_decl
%type <y_tnode> constant
%type <y_sym> enum_declaration
%type <y_sym> enums_with_opt_comma
%type <y_sym> enums
%type <y_sym> enumerator
%type <y_sym> ename
%type <y_sym> notype_direct_decl
%type <y_sym> type_direct_decl
%type <y_pqinf> pointer
%type <y_pqinf> asterisk
%type <y_sym> param_decl
%type <y_sym> param_list
%type <y_sym> abs_decl_param_list
%type <y_sym> direct_param_decl
%type <y_sym> notype_param_decl
%type <y_sym> direct_notype_param_decl
%type <y_pqinf> type_qualifier_list
%type <y_pqinf> type_qualifier
%type <y_sym> identifier_list
%type <y_sym> abs_decl
%type <y_sym> direct_abs_decl
%type <y_sym> vararg_parameter_type_list
%type <y_sym> parameter_type_list
%type <y_sym> parameter_declaration
%type <y_tnode> expr
%type <y_tnode> term
%type <y_tnode> func_arg_list
%type <y_op> point_or_arrow
%type <y_type> type_name
%type <y_sym> abstract_declaration
%type <y_tnode> do_while_expr
%type <y_tnode> opt_expr
%type <y_strg> string
%type <y_strg> string2
%type <y_attr> attribute_name
%type <y_attr> attribute_spec
%type <y_attrnode> attribute_specs
%type <y_attrnode> opt_attribute_specs
%%
program:
/* empty */ {
if (sflag) {
/* empty translation unit */
error(272);
} else if (!tflag) {
/* empty translation unit */
warning(272);
}
}
| translation_unit
;
translation_unit:
ext_decl
| translation_unit ext_decl
;
ext_decl:
func_def {
glclup(0);
clrwflgs();
}
| data_def {
glclup(0);
clrwflgs();
}
| T_LEQUAL T_LPARN identifier T_COMMA identifier T_RPARN T_SEMI {
sym_t *new, *old;
if ($5->sb_sym && $3->sb_sym /*== NULL*/) {
new = getsym($5);
old = $3->sb_sym;
new->s_dpos = old->s_dpos;
new->s_spos = old->s_spos;
new->s_upos = old->s_upos;
new->s_kind = old->s_kind;
new->s_keyw = old->s_keyw;
new->s_field = old->s_field;
new->s_set = old->s_set;
new->s_used = old->s_used;
new->s_arg = old->s_arg;
new->s_reg = old->s_reg;
new->s_defarg = old->s_defarg;
new->s_rimpl = old->s_rimpl;
new->s_osdef = old->s_osdef;
new->s_inline = old->s_inline;
new->s_def = old->s_def;
new->s_scl = old->s_scl;
new->s_blklev = old->s_blklev;
new->s_type = old->s_type;
new->s_value = old->s_value;
new->u = old->u;
/* XXX missing 'r' because do not know return type
of parent... */
/* outsym(new, new->s_scl, DEF); */
outfdef(new, &csrc_pos,
new->s_rimpl || new->s_type->t_subt->t_tspec != VOID,
0, NULL);
}
}
;
data_def: T_SEMI
| clrtyp deftyp notype_init_decls T_SEMI {
if (sflag) {
/* old style declaration; add "int" */
error(1);
} else if (!tflag) {
/* old style declaration; add "int" */
warning(1);
}
}
| declmods deftyp T_SEMI {
if (dcs->d_scl == TYPEDEF) {
/* typedef declares no type name */
warning(72);
} else {
/* empty declaration */
warning(2);
}
}
| declmods deftyp notype_init_decls T_SEMI
| declspecs deftyp T_SEMI {
if (dcs->d_scl == TYPEDEF) {
/* typedef declares no type name */
warning(72);
} else if (!dcs->d_nedecl) {
/* empty declaration */
warning(2);
}
}
| declspecs deftyp type_init_decls T_SEMI
| error T_SEMI {
globclup();
}
| error T_RBRACE {
globclup();
}
;
func_def:
func_decl {
if ($1->s_type->t_tspec != FUNC) {
/* syntax error */
error(249);
YYERROR;
}
if ($1->s_type->t_typedef) {
/* ()-less function definition */
error(64);
YYERROR;
}
funcdef($1);
blklev++;
pushdecl(ARG);
} opt_arg_declaration_list {
popdecl();
blklev--;
cluparg();
pushctrl(0);
} comp_stmnt {
funcend();
popctrl(0);
}
;
func_decl:
clrtyp deftyp notype_decl {
$$ = $3;
}
| declmods deftyp notype_decl {
$$ = $3;
}
| declspecs deftyp type_decl {
$$ = $3;
}
;
opt_arg_declaration_list:
/* empty */
| arg_declaration_list
;
arg_declaration_list:
arg_declaration
| arg_declaration_list arg_declaration
/* XXX or better "arg_declaration error" ? */
| error
;
/*
* "arg_declaration" is separated from "declaration" because it
* needs other error handling.
*/
arg_declaration:
declmods deftyp T_SEMI {
/* empty declaration */
warning(2);
}
| declmods deftyp notype_init_decls T_SEMI
| declspecs deftyp T_SEMI {
if (!dcs->d_nedecl) {
/* empty declaration */
warning(2);
} else {
tspec_t ts = dcs->d_type->t_tspec;
/* %s declared in argument declaration list */
warning(3, ts == STRUCT ? "struct" :
(ts == UNION ? "union" : "enum"));
}
}
| declspecs deftyp type_init_decls T_SEMI {
if (dcs->d_nedecl) {
tspec_t ts = dcs->d_type->t_tspec;
/* %s declared in argument declaration list */
warning(3, ts == STRUCT ? "struct" :
(ts == UNION ? "union" : "enum"));
}
}
| declmods error
| declspecs error
;
declaration:
declmods deftyp T_SEMI {
if (dcs->d_scl == TYPEDEF) {
/* typedef declares no type name */
warning(72);
} else {
/* empty declaration */
warning(2);
}
}
| declmods deftyp notype_init_decls T_SEMI
| declspecs deftyp T_SEMI {
if (dcs->d_scl == TYPEDEF) {
/* typedef declares no type name */
warning(72);
} else if (!dcs->d_nedecl) {
/* empty declaration */
warning(2);
}
}
| declspecs deftyp type_init_decls T_SEMI
| error T_SEMI
;
clrtyp:
{
clrtyp();
}
;
deftyp:
/* empty */ {
deftyp();
}
;
declspecs:
clrtyp_typespec {
addtype($1);
}
| declmods typespec {
addtype($2);
}
| declspecs declmod
| declspecs notype_typespec {
addtype($2);
}
;
declmods:
clrtyp T_QUAL {
addqual($2);
}
| clrtyp T_SCLASS {
addscl($2);
}
| clrtyp attribute_spec
| declmods declmod
;
declmod:
T_QUAL {
addqual($1);
}
| T_SCLASS {
addscl($1);
}
| attribute_spec
;
attribute_name:
T_QUAL {
$$ = getkwattr(T_QUAL, $1);
}
| T_SCLASS {
$$ = getkwattr(T_SCLASS, $1);
}
| T_TYPENAME {
$$ = getattr($1->sb_name);
}
| T_NAME {
$$ = getattr($1->sb_name);
}
;
attribute_spec:
T_ATTRIBUTE T_LPARN T_LPARN attribute_name T_RPARN T_RPARN {
$$ = $4;
}
| T_ATTRIBUTE T_LPARN T_LPARN attribute_name T_COMMA read_until_rparn T_RPARN {
/* some other exotic syntax that we don't understand */
$$ = AT_UNKNOWN;
}
| T_ATTRIBUTE T_LPARN T_LPARN attribute_name T_LPARN read_until_rparn T_RPARN T_RPARN {
/* some other exotic syntax that we don't understand */
$$ = AT_UNKNOWN;
}
;
attribute_specs:
attribute_spec {
/*$$ = newattrnode($1);*/
$$ = NULL;
}
| attribute_specs attribute_spec {
/*appendattr($1, $2);*/
$$ = $1;
}
;
opt_attribute_specs:
/* EMPTY */ {
$$ = NULL;
}
| attribute_specs {
$$ = $1;
}
;
clrtyp_typespec:
clrtyp notype_typespec {
$$ = $2;
}
| T_TYPENAME clrtyp {
$$ = getsym($1)->s_type;
}
;
typespec:
notype_typespec {
$$ = $1;
}
| T_TYPENAME {
$$ = getsym($1)->s_type;
}
;
notype_typespec:
T_TYPE {
$$ = gettyp($1);
}
| struct_spec {
popdecl();
$$ = $1;
}
| enum_spec {
popdecl();
$$ = $1;
}
;
struct_spec:
struct struct_tag {
/*
* STDC requires that "struct a;" always introduces
* a new tag if "a" is not declared at current level
*
* yychar is valid because otherwise the parser would
* not been able to deceide if he must shift or reduce
*/
$$ = mktag($2, $1, 0, yychar == T_SEMI);
}
| struct struct_tag {
dcs->d_tagtyp = mktag($2, $1, 1, 0);
} struct_declaration {
$$ = compltag(dcs->d_tagtyp, $4);
}
| struct {
dcs->d_tagtyp = mktag(NULL, $1, 1, 0);
} struct_declaration {
$$ = compltag(dcs->d_tagtyp, $3);
}
| struct error {
symtyp = FVFT;
$$ = gettyp(INT);
}
;
struct:
T_SOU {
symtyp = FTAG;
pushdecl($1 == STRUCT ? MOS : MOU);
dcs->d_offset = 0;
dcs->d_stralign = CHAR_BIT;
$$ = $1;
}
;
struct_tag:
identifier {
$$ = getsym($1);
}
;
struct_declaration:
struct_decl_lbrace member_declaration_list_with_rbrace {
$$ = $2;
}
;
struct_decl_lbrace:
T_LBRACE {
symtyp = FVFT;
}
;
member_declaration_list_with_rbrace:
member_declaration_list T_SEMI T_RBRACE {
$$ = $1;
}
| member_declaration_list T_RBRACE {
if (sflag) {
/* syntax req. ";" after last struct/union member */
error(66);
} else {
/* syntax req. ";" after last struct/union member */
warning(66);
}
$$ = $1;
}
| T_RBRACE {
$$ = NULL;
}
;
member_declaration_list:
member_declaration opt_attribute_specs {
$$ = $1;
}
| member_declaration_list T_SEMI member_declaration opt_attribute_specs {
$$ = lnklst($1, $3);
}
;
member_declaration:
noclass_declmods deftyp {
/* too late, i know, but getsym() compensates it */
symtyp = FMOS;
} notype_member_decls {
symtyp = FVFT;
$$ = $4;
}
| noclass_declspecs deftyp {
symtyp = FMOS;
} type_member_decls {
symtyp = FVFT;
$$ = $4;
}
| noclass_declmods deftyp {
/* struct or union member must be named */
warning(49);
$$ = NULL;
}
| noclass_declspecs deftyp {
/* struct or union member must be named */
warning(49);
$$ = NULL;
}
| error {
symtyp = FVFT;
$$ = NULL;
}
;
noclass_declspecs:
clrtyp_typespec {
addtype($1);
}
| noclass_declmods typespec {
addtype($2);
}
| noclass_declspecs T_QUAL {
addqual($2);
}
| noclass_declspecs notype_typespec {
addtype($2);
}
;
noclass_declmods:
clrtyp T_QUAL {
addqual($2);
}
| noclass_declmods T_QUAL {
addqual($2);
}
;
notype_member_decls:
notype_member_decl {
$$ = decl1str($1);
}
| notype_member_decls {
symtyp = FMOS;
} T_COMMA type_member_decl {
$$ = lnklst($1, decl1str($4));
}
;
type_member_decls:
type_member_decl {
$$ = decl1str($1);
}
| type_member_decls {
symtyp = FMOS;
} T_COMMA type_member_decl {
$$ = lnklst($1, decl1str($4));
}
;
notype_member_decl:
notype_decl {
$$ = $1;
}
| notype_decl T_COLON constant {
$$ = bitfield($1, toicon($3));
}
| {
symtyp = FVFT;
} T_COLON constant {
$$ = bitfield(NULL, toicon($3));
}
;
type_member_decl:
type_decl {
$$ = $1;
}
| type_decl T_COLON constant {
$$ = bitfield($1, toicon($3));
}
| {
symtyp = FVFT;
} T_COLON constant {
$$ = bitfield(NULL, toicon($3));
}
;
enum_spec:
enum enum_tag {
$$ = mktag($2, ENUM, 0, 0);
}
| enum enum_tag {
dcs->d_tagtyp = mktag($2, ENUM, 1, 0);
} enum_declaration {
$$ = compltag(dcs->d_tagtyp, $4);
}
| enum {
dcs->d_tagtyp = mktag(NULL, ENUM, 1, 0);
} enum_declaration {
$$ = compltag(dcs->d_tagtyp, $3);
}
| enum error {
symtyp = FVFT;
$$ = gettyp(INT);
}
;
enum:
T_ENUM {
symtyp = FTAG;
pushdecl(ENUMCON);
}
;
enum_tag:
identifier {
$$ = getsym($1);
}
;
enum_declaration:
enum_decl_lbrace enums_with_opt_comma T_RBRACE {
$$ = $2;
}
;
enum_decl_lbrace:
T_LBRACE {
symtyp = FVFT;
enumval = 0;
}
;
enums_with_opt_comma:
enums {
$$ = $1;
}
| enums T_COMMA {
if (sflag) {
/* trailing "," prohibited in enum declaration */
error(54);
} else {
/* trailing "," prohibited in enum declaration */
warning(54);
}
$$ = $1;
}
;
enums:
enumerator {
$$ = $1;
}
| enums T_COMMA enumerator {
$$ = lnklst($1, $3);
}
| error {
$$ = NULL;
}
;
enumerator:
ename {
$$ = ename($1, enumval, 1);
}
| ename T_ASSIGN constant {
$$ = ename($1, toicon($3), 0);
}
;
ename:
identifier {
$$ = getsym($1);
}
;
notype_init_decls:
notype_init_decl
| notype_init_decls T_COMMA type_init_decl
;
type_init_decls:
type_init_decl
| type_init_decls T_COMMA type_init_decl
;
notype_init_decl:
notype_decl opt_asm_spec {
idecl($1, 0);
chksz($1);
}
| notype_decl opt_asm_spec {
idecl($1, 1);
} T_ASSIGN initializer {
chksz($1);
}
;
type_init_decl:
type_decl opt_asm_spec {
idecl($1, 0);
chksz($1);
}
| type_decl opt_asm_spec {
idecl($1, 1);
} T_ASSIGN initializer {
chksz($1);
}
;
notype_decl:
notype_direct_decl opt_attribute_specs {
$$ = $1;
}
| pointer notype_direct_decl opt_attribute_specs {
$$ = addptr($2, $1);
}
;
notype_direct_decl:
T_NAME {
$$ = dname(getsym($1));
}
| T_LPARN type_decl T_RPARN {
$$ = $2;
}
| notype_direct_decl T_LBRACK T_RBRACK {
$$ = addarray($1, 0, 0);
}
| notype_direct_decl T_LBRACK constant T_RBRACK {
$$ = addarray($1, 1, toicon($3));
}
| notype_direct_decl param_list {
$$ = addfunc($1, $2);
popdecl();
blklev--;
}
;
type_decl:
type_direct_decl opt_attribute_specs {
$$ = $1;
}
| pointer type_direct_decl opt_attribute_specs {
$$ = addptr($2, $1);
}
;
type_direct_decl:
identifier {
$$ = dname(getsym($1));
}
| T_LPARN type_decl T_RPARN {
$$ = $2;
}
| type_direct_decl T_LBRACK T_RBRACK {
$$ = addarray($1, 0, 0);
}
| type_direct_decl T_LBRACK constant T_RBRACK {
$$ = addarray($1, 1, toicon($3));
}
| type_direct_decl param_list {
$$ = addfunc($1, $2);
popdecl();
blklev--;
}
;
/*
* param_decl and notype_param_decl exist to avoid a conflict in
* argument lists. A typename enclosed in parens should always be
* treated as a typename, not an argument.
* "typedef int a; f(int (a));" is "typedef int a; f(int foo(a));"
* not "typedef int a; f(int a);"
*/
param_decl:
direct_param_decl {
$$ = $1;
}
| pointer direct_param_decl {
$$ = addptr($2, $1);
}
;
direct_param_decl:
identifier {
$$ = dname(getsym($1));
}
| T_LPARN notype_param_decl T_RPARN {
$$ = $2;
}
| direct_param_decl T_LBRACK T_RBRACK {
$$ = addarray($1, 0, 0);
}
| direct_param_decl T_LBRACK constant T_RBRACK {
$$ = addarray($1, 1, toicon($3));
}
| direct_param_decl param_list {
$$ = addfunc($1, $2);
popdecl();
blklev--;
}
;
notype_param_decl:
direct_notype_param_decl {
$$ = $1;
}
| pointer direct_notype_param_decl {
$$ = addptr($2, $1);
}
;
direct_notype_param_decl:
T_NAME {
$$ = dname(getsym($1));
}
| T_LPARN notype_param_decl T_RPARN {
$$ = $2;
}
| direct_notype_param_decl T_LBRACK T_RBRACK {
$$ = addarray($1, 0, 0);
}
| direct_notype_param_decl T_LBRACK constant T_RBRACK {
$$ = addarray($1, 1, toicon($3));
}
| direct_notype_param_decl param_list {
$$ = addfunc($1, $2);
popdecl();
blklev--;
}
;
pointer:
asterisk opt_attribute_specs {
$$ = $1;
}
| asterisk opt_attribute_specs type_qualifier_list {
$$ = mergepq($1, $3);
}
| asterisk opt_attribute_specs pointer {
$$ = mergepq($1, $3);
}
| asterisk opt_attribute_specs type_qualifier_list pointer {
$$ = mergepq(mergepq($1, $3), $4);
}
;
asterisk:
T_MULT {
$$ = xcalloc(1, sizeof (pqinf_t));
$$->p_pcnt = 1;
}
;
type_qualifier_list:
type_qualifier {
$$ = $1;
}
| type_qualifier_list type_qualifier {
$$ = mergepq($1, $2);
}
;
type_qualifier:
T_QUAL {
$$ = xcalloc(1, sizeof (pqinf_t));
if ($1 == CONST) {
$$->p_const = 1;
} else {
$$->p_volatile = 1;
}
}
;
param_list:
id_list_lparn identifier_list T_RPARN {
$$ = $2;
}
| abs_decl_param_list {
$$ = $1;
}
;
id_list_lparn:
T_LPARN {
blklev++;
pushdecl(PARG);
}
;
identifier_list:
T_NAME {
$$ = iname(getsym($1));
}
| identifier_list T_COMMA T_NAME {
$$ = lnklst($1, iname(getsym($3)));
}
| identifier_list error {
$$ = $1;
}
;
abs_decl_param_list:
abs_decl_lparn T_RPARN {
$$ = NULL;
}
| abs_decl_lparn vararg_parameter_type_list T_RPARN {
dcs->d_proto = 1;
$$ = $2;
}
| abs_decl_lparn error T_RPARN {
$$ = NULL;
}
;
abs_decl_lparn:
T_LPARN {
blklev++;
pushdecl(PARG);
}
;
vararg_parameter_type_list:
parameter_type_list {
$$ = $1;
}
| parameter_type_list T_COMMA T_ELLIPSE {
dcs->d_vararg = 1;
$$ = $1;
}
| T_ELLIPSE {
if (sflag) {
/* ANSI C requires formal parameter before "..." */
error(84);
} else if (!tflag) {
/* ANSI C requires formal parameter before "..." */
warning(84);
}
dcs->d_vararg = 1;
$$ = NULL;
}
;
parameter_type_list:
parameter_declaration opt_attribute_specs opt_asm_spec {
$$ = $1;
}
| parameter_type_list T_COMMA parameter_declaration opt_attribute_specs opt_asm_spec {
$$ = lnklst($1, $3);
}
;
parameter_declaration:
declmods deftyp {
$$ = decl1arg(aname(), 0);
}
| declspecs deftyp {
$$ = decl1arg(aname(), 0);
}
| declmods deftyp notype_param_decl {
$$ = decl1arg($3, 0);
}
/*
* param_decl is needed because of following conflict:
* "typedef int a; f(int (a));" could be parsed as
* "function with argument a of type int", or
* "function with an abstract argument of type function".
* This grammar realizes the second case.
*/
| declspecs deftyp param_decl {
$$ = decl1arg($3, 0);
}
| declmods deftyp abs_decl {
$$ = decl1arg($3, 0);
}
| declspecs deftyp abs_decl {
$$ = decl1arg($3, 0);
}
;
opt_asm_spec:
/* empty */
| T_ASM T_LPARN read_until_rparn
;
initializer:
init_expr
;
init_expr:
expr %prec T_COMMA {
mkinit($1);
}
| init_lbrace init_expr_list init_rbrace
| init_lbrace init_expr_list T_COMMA init_rbrace
| init_lbrace init_rbrace
| error
;
init_expr_list:
init_expr %prec T_COMMA
| init_expr_list T_COMMA init_expr
;
init_lbrace:
T_LBRACE {
initlbr();
}
;
init_rbrace:
T_RBRACE {
initrbr();
}
;
type_name:
{
pushdecl(ABSTRACT);
} abstract_declaration {
popdecl();
$$ = $2->s_type;
}
;
abstract_declaration:
noclass_declmods deftyp {
$$ = decl1abs(aname());
}
| noclass_declspecs deftyp {
$$ = decl1abs(aname());
}
| noclass_declmods deftyp abs_decl {
$$ = decl1abs($3);
}
| noclass_declspecs deftyp abs_decl {
$$ = decl1abs($3);
}
;
abs_decl:
pointer {
$$ = addptr(aname(), $1);
}
| direct_abs_decl {
$$ = $1;
}
| pointer direct_abs_decl {
$$ = addptr($2, $1);
}
;
direct_abs_decl:
T_LPARN abs_decl T_RPARN {
$$ = $2;
}
| T_LBRACK T_RBRACK {
$$ = addarray(aname(), 0, 0);
}
| T_LBRACK constant T_RBRACK {
$$ = addarray(aname(), 1, toicon($2));
}
| direct_abs_decl T_LBRACK T_RBRACK {
$$ = addarray($1, 0, 0);
}
| direct_abs_decl T_LBRACK constant T_RBRACK {
$$ = addarray($1, 1, toicon($3));
}
| abs_decl_param_list {
$$ = addfunc(aname(), $1);
popdecl();
blklev--;
}
| direct_abs_decl abs_decl_param_list {
$$ = addfunc($1, $2);
popdecl();
blklev--;
}
;
stmnt:
labeled_stmnt
| expr_stmnt
| comp_stmnt
| selection_stmnt
| iteration_stmnt
| jump_stmnt {
ftflg = 0;
}
| asm_stmnt
;
labeled_stmnt:
label stmnt
;
label:
identifier T_COLON {
symtyp = FLAB;
label(T_NAME, getsym($1), NULL);
}
| T_CASE constant T_COLON {
label(T_CASE, NULL, $2);
ftflg = 1;
}
| T_CASE constant T_ELLIPSE constant T_COLON {
/* XXX: only using the first value of this gcc-ism */
label(T_CASE, NULL, $2);
ftflg = 1;
if (!gflag)
gnuism(311);
}
| T_DEFAULT T_COLON {
label(T_DEFAULT, NULL, NULL);
ftflg = 1;
}
;
comp_stmnt:
compstmnt_lbrace declaration_list opt_stmnt_list compstmnt_rbrace
| compstmnt_lbrace opt_stmnt_list compstmnt_rbrace
;
compstmnt_lbrace:
T_LBRACE {
blklev++;
mblklev++;
pushdecl(AUTO);
}
;
compstmnt_rbrace:
T_RBRACE {
popdecl();
freeblk();
mblklev--;
blklev--;
ftflg = 0;
}
;
opt_stmnt_list:
/* empty */
| stmnt_list
;
stmnt_list:
stmnt {
clrwflgs();
}
| stmnt_list stmnt {
clrwflgs();
}
| stmnt_list error T_SEMI {
clrwflgs();
}
;
expr_stmnt:
expr T_SEMI {
expr($1, 0, 0);
ftflg = 0;
}
| T_SEMI {
ftflg = 0;
}
;
selection_stmnt:
if_without_else {
if2();
if3(0);
}
| if_without_else T_ELSE {
if2();
} stmnt {
if3(1);
}
| if_without_else T_ELSE error {
if3(0);
}
| switch_expr stmnt {
switch2();
}
| switch_expr error {
switch2();
}
;
if_without_else:
if_expr stmnt
| if_expr error
;
if_expr:
T_IF T_LPARN expr T_RPARN {
if1($3);
clrwflgs();
}
;
switch_expr:
T_SWITCH T_LPARN expr T_RPARN {
switch1($3);
clrwflgs();
}
;
iteration_stmnt:
while_expr stmnt {
while2();
}
| while_expr error {
while2();
}
| do stmnt do_while_expr {
do2($3);
ftflg = 0;
}
| do error {
do2(NULL);
}
| for_exprs stmnt {
for2();
}
| for_exprs error {
for2();
}
;
while_expr:
T_WHILE T_LPARN expr T_RPARN {
while1($3);
clrwflgs();
}
;
do:
T_DO {
do1();
}
;
do_while_expr:
T_WHILE T_LPARN expr T_RPARN T_SEMI {
$$ = $3;
}
;
for_exprs:
T_FOR T_LPARN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPARN {
for1($3, $5, $7);
clrwflgs();
}
;
opt_expr:
/* empty */ {
$$ = NULL;
}
| expr {
$$ = $1;
}
;
jump_stmnt:
goto identifier T_SEMI {
dogoto(getsym($2));
}
| goto error T_SEMI {
symtyp = FVFT;
}
| T_CONTINUE T_SEMI {
docont();
}
| T_BREAK T_SEMI {
dobreak();
}
| T_RETURN T_SEMI {
doreturn(NULL);
}
| T_RETURN expr T_SEMI {
doreturn($2);
}
;
goto:
T_GOTO {
symtyp = FLAB;
}
;
asm_stmnt:
T_ASM T_LPARN read_until_rparn T_SEMI {
setasm();
}
| T_ASM T_QUAL T_LPARN read_until_rparn T_SEMI {
setasm();
}
| T_ASM error
;
read_until_rparn:
/* empty */ {
ignuptorp();
}
;
declaration_list:
declaration {
clrwflgs();
}
| declaration_list declaration {
clrwflgs();
}
;
constant:
expr %prec T_COMMA {
$$ = $1;
}
;
expr:
expr T_MULT expr {
$$ = build(MULT, $1, $3);
}
| expr T_DIVOP expr {
$$ = build($2, $1, $3);
}
| expr T_ADDOP expr {
$$ = build($2, $1, $3);
}
| expr T_SHFTOP expr {
$$ = build($2, $1, $3);
}
| expr T_RELOP expr {
$$ = build($2, $1, $3);
}
| expr T_EQOP expr {
$$ = build($2, $1, $3);
}
| expr T_AND expr {
$$ = build(AND, $1, $3);
}
| expr T_XOR expr {
$$ = build(XOR, $1, $3);
}
| expr T_OR expr {
$$ = build(OR, $1, $3);
}
| expr T_LOGAND expr {
$$ = build(LOGAND, $1, $3);
}
| expr T_LOGOR expr {
$$ = build(LOGOR, $1, $3);
}
| expr T_QUEST expr T_COLON expr {
$$ = build(QUEST, $1, build(COLON, $3, $5));
}
| expr T_ASSIGN expr {
$$ = build(ASSIGN, $1, $3);
}
| expr T_OPASS expr {
$$ = build($2, $1, $3);
}
| expr T_COMMA expr {
$$ = build(COMMA, $1, $3);
}
| term {
$$ = $1;
}
;
term:
T_NAME {
/* XXX really necessary? */
if (yychar < 0)
yychar = yylex();
$$ = getnnode(getsym($1), yychar);
}
| string {
$$ = getsnode($1);
}
| T_CON {
$$ = getcnode(gettyp($1->v_tspec), $1);
}
| T_LPARN expr T_RPARN {
if ($2 != NULL)
$2->tn_parn = 1;
$$ = $2;
}
| term T_INCDEC {
$$ = build($2 == INC ? INCAFT : DECAFT, $1, NULL);
}
| T_INCDEC term {
$$ = build($1 == INC ? INCBEF : DECBEF, $2, NULL);
}
| T_MULT term {
$$ = build(STAR, $2, NULL);
}
| T_AND term {
$$ = build(AMPER, $2, NULL);
}
| T_UNOP term {
$$ = build($1, $2, NULL);
}
| T_ADDOP term {
if (tflag && $1 == PLUS) {
/* unary + is illegal in traditional C */
warning(100);
}
$$ = build($1 == PLUS ? UPLUS : UMINUS, $2, NULL);
}
| term T_LBRACK expr T_RBRACK {
$$ = build(STAR, build(PLUS, $1, $3), NULL);
}
| term T_LPARN T_RPARN {
$$ = funccall($1, NULL);
}
| term T_LPARN func_arg_list T_RPARN {
$$ = funccall($1, $3);
}
| term point_or_arrow T_NAME {
if ($1 != NULL) {
sym_t *msym;
/* XXX strmemb should be integrated in build() */
if ($2 == ARROW) {
/* must to this before strmemb is called */
$1 = cconv($1);
}
msym = strmemb($1, $2, getsym($3));
$$ = build($2, $1, getnnode(msym, 0));
} else {
$$ = NULL;
}
}
| T_SIZEOF term %prec T_SIZEOF {
if (($$ = $2 == NULL ? NULL : bldszof($2->tn_type)) != NULL)
chkmisc($2, 0, 0, 0, 0, 0, 1);
}
| T_SIZEOF T_LPARN type_name T_RPARN %prec T_SIZEOF {
$$ = bldszof($3);
}
| T_LPARN type_name T_RPARN term %prec T_UNOP {
$$ = cast($4, $2);
}
;
string:
T_STRING {
$$ = $1;
}
| T_STRING string2 {
$$ = catstrg($1, $2);
}
;
string2:
T_STRING {
if (tflag) {
/* concatenated strings are illegal in traditional C */
warning(219);
}
$$ = $1;
}
| string2 T_STRING {
$$ = catstrg($1, $2);
}
;
func_arg_list:
expr %prec T_COMMA {
$$ = funcarg(NULL, $1);
}
| func_arg_list T_COMMA expr {
$$ = funcarg($1, $3);
}
;
point_or_arrow:
T_STROP {
symtyp = FMOS;
$$ = $1;
}
;
identifier:
T_NAME {
$$ = $1;
}
| T_TYPENAME {
$$ = $1;
}
;
%%
/* ARGSUSED */
int
yyerror(char *msg)
{
error(249);
if (++sytxerr >= 5)
norecover();
return (0);
}
/*
* Gets a node for a constant and returns the value of this constant
* as integer.
* Is the node not constant or too large for int or of type float,
* a warning will be printed.
*
* toicon() should be used only inside declarations. If it is used in
* expressions, it frees the memory used for the expression.
*/
static int
toicon(tnode_t *tn)
{
int i;
tspec_t t;
val_t *v;
v = constant(tn);
/*
* Abstract declarations are used inside expression. To free
* the memory would be a fatal error.
*/
if (dcs->d_ctx != ABSTRACT)
tfreeblk();
if ((t = v->v_tspec) == FLOAT || t == DOUBLE || t == LDOUBLE) {
i = (int)v->v_ldbl;
/* integral constant expression expected */
error(55);
} else {
i = (int)v->v_quad;
if (isutyp(t)) {
if ((u_quad_t)v->v_quad > UINT_MAX) {
/* integral constant too large */
warning(56);
}
} else {
if (v->v_quad > INT_MAX || v->v_quad < INT_MIN) {
/* integral constant too large */
warning(56);
}
}
}
free(v);
return (i);
}
static void
idecl(sym_t *decl, int initflg)
{
initerr = 0;
initsym = decl;
switch (dcs->d_ctx) {
case EXTERN:
decl1ext(decl, initflg);
break;
case ARG:
(void)decl1arg(decl, initflg);
break;
case AUTO:
decl1loc(decl, initflg);
break;
default:
lerror("idecl()");
}
if (initflg && !initerr)
prepinit();
}
/*
* Discard all input tokens up to and including the next
* unmatched right paren
*/
void
ignuptorp(void)
{
int level;
if (yychar < 0)
yychar = yylex();
freeyyv(&yylval, yychar);
level = 1;
while (yychar != T_RPARN || --level > 0) {
if (yychar == T_LPARN) {
level++;
} else if (yychar <= 0) {
break;
}
freeyyv(&yylval, yychar = yylex());
}
yyclearin;
}
|