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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html40/loose.dtd">
<HTML>
<!-- Created on January, 28 2005 by texi2html 1.66 -->
<!--
Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author)
Karl Berry <karl@freefriends.org>
Olaf Bachmann <obachman@mathematik.uni-kl.de>
and many others.
Maintained by: Many creative people <dev@texi2html.cvshome.org>
Send bugs and suggestions to <users@texi2html.cvshome.org>
-->
<HEAD>
<TITLE>Bison 2.21.5: Grammar File</TITLE>
<META NAME="description" CONTENT="Bison 2.21.5: Grammar File">
<META NAME="keywords" CONTENT="Bison 2.21.5: Grammar File">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META NAME="Generator" CONTENT="texi2html 1.66">
</HEAD>
<BODY LANG="en" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080" ALINK="#FF0000">
<A NAME="SEC33"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_5.html#SEC32"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC34"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_5.html#SEC14"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H1> 3. Bison Grammar Files </H1>
<!--docid::SEC33::-->
<P>
Bison takes as input a context-free grammar specification and produces a
C-language function that recognizes correct instances of the grammar.
</P>
<P>
The Bison grammar input file conventionally has a name ending in `<SAMP>.y</SAMP>'.
</P>
<P>
<TABLE BORDER="0" CELLSPACING="0">
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC34">3.1 Outline of a Bison Grammar</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Overall layout of the grammar file.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC39">3.2 Symbols, Terminal and Nonterminal</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Terminal and nonterminal symbols.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC40">3.3 Syntax of Grammar Rules</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">How to write grammar rules.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC41">3.4 Recursive Rules</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Writing recursive rules.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC42">3.5 Defining Language Semantics</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Semantic values and actions.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC48">3.6 Bison Declarations</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">All kinds of Bison declarations are described here.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC57">3.7 Multiple Parsers in the Same Program</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Putting more than one Bison parser in one program.</TD></TR>
</TABLE>
<P>
<A NAME="Grammar Outline"></A>
<HR SIZE="6">
<A NAME="SEC34"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC35"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H2> 3.1 Outline of a Bison Grammar </H2>
<!--docid::SEC34::-->
<P>
A Bison grammar file has four main sections, shown here with the
appropriate delimiters:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>%{
<VAR>C declarations</VAR>
%}
<VAR>Bison declarations</VAR>
%%
<VAR>Grammar rules</VAR>
%%
<VAR>Additional C code</VAR>
</pre></td></tr></table><P>
Comments enclosed in `<SAMP>/* <small>...</small> */</SAMP>' may appear in any of the sections.
</P>
<P>
<TABLE BORDER="0" CELLSPACING="0">
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC35">3.1.1 The C Declarations Section</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Syntax and usage of the C declarations section.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC36">3.1.2 The Bison Declarations Section</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Syntax and usage of the Bison declarations section.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC37">3.1.3 The Grammar Rules Section</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Syntax and usage of the grammar rules section.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC38">3.1.4 The Additional C Code Section</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Syntax and usage of the additional C code section.</TD></TR>
</TABLE>
<P>
<A NAME="C Declarations"></A>
<HR SIZE="6">
<A NAME="SEC35"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC34"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC36"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC34"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.1.1 The C Declarations Section </H3>
<!--docid::SEC35::-->
<P>
The <VAR>C declarations</VAR> section contains macro definitions and
declarations of functions and variables that are used in the actions in the
grammar rules. These are copied to the beginning of the parser file so
that they precede the definition of <CODE>yyparse</CODE>. You can use
`<SAMP>#include</SAMP>' to get the declarations from a header file. If you don't
need any C declarations, you may omit the `<SAMP>%{</SAMP>' and `<SAMP>%}</SAMP>'
delimiters that bracket this section.
</P>
<P>
<A NAME="Bison Declarations"></A>
<HR SIZE="6">
<A NAME="SEC36"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC35"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC37"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC34"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.1.2 The Bison Declarations Section </H3>
<!--docid::SEC36::-->
<P>
The <VAR>Bison declarations</VAR> section contains declarations that define
terminal and nonterminal symbols, specify precedence, and so on.
In some simple grammars you may not need any declarations.
See section <A HREF="bison_6.html#SEC48">Bison Declarations</A>.
</P>
<P>
<A NAME="Grammar Rules"></A>
<HR SIZE="6">
<A NAME="SEC37"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC36"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC38"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC34"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.1.3 The Grammar Rules Section </H3>
<!--docid::SEC37::-->
<P>
The <EM>grammar rules</EM> section contains one or more Bison grammar
rules, and nothing else. See section <A HREF="bison_6.html#SEC40">Syntax of Grammar Rules</A>.
</P>
<P>
There must always be at least one grammar rule, and the first
`<SAMP>%%</SAMP>' (which precedes the grammar rules) may never be omitted even
if it is the first thing in the file.
</P>
<P>
<A NAME="C Code"></A>
<HR SIZE="6">
<A NAME="SEC38"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC37"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC39"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC34"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.1.4 The Additional C Code Section </H3>
<!--docid::SEC38::-->
<P>
The <VAR>additional C code</VAR> section is copied verbatim to the end of
the parser file, just as the <VAR>C declarations</VAR> section is copied to
the beginning. This is the most convenient place to put anything
that you want to have in the parser file but which need not come before
the definition of <CODE>yyparse</CODE>. For example, the definitions of
<CODE>yylex</CODE> and <CODE>yyerror</CODE> often go here. See section <A HREF="bison_7.html#SEC58">Parser C-Language Interface</A>.
</P>
<P>
If the last section is empty, you may omit the `<SAMP>%%</SAMP>' that separates it
from the grammar rules.
</P>
<P>
The Bison parser itself contains many static variables whose names start
with `<SAMP>yy</SAMP>' and many macros whose names start with `<SAMP>YY</SAMP>'. It is a
good idea to avoid using any such names (except those documented in this
manual) in the additional C code section of the grammar file.
</P>
<P>
<A NAME="Symbols"></A>
<HR SIZE="6">
<A NAME="SEC39"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC38"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC40"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H2> 3.2 Symbols, Terminal and Nonterminal </H2>
<!--docid::SEC39::-->
<P>
<EM>Symbols</EM> in Bison grammars represent the grammatical classifications
of the language.
</P>
<P>
A <EM>terminal symbol</EM> (also known as a <EM>token type</EM>) represents a
class of syntactically equivalent tokens. You use the symbol in grammar
rules to mean that a token in that class is allowed. The symbol is
represented in the Bison parser by a numeric code, and the <CODE>yylex</CODE>
function returns a token type code to indicate what kind of token has been
read. You don't need to know what the code value is; you can use the
symbol to stand for it.
</P>
<P>
A <EM>nonterminal symbol</EM> stands for a class of syntactically equivalent
groupings. The symbol name is used in writing grammar rules. By convention,
it should be all lower case.
</P>
<P>
Symbol names can contain letters, digits (not at the beginning),
underscores and periods. Periods make sense only in nonterminals.
</P>
<P>
There are three ways of writing terminal symbols in the grammar:
</P>
<P>
<UL>
<LI>
A <EM>named token type</EM> is written with an identifier, like an
identifier in C. By convention, it should be all upper case. Each
such name must be defined with a Bison declaration such as
<CODE>%token</CODE>. See section <A HREF="bison_6.html#SEC49">Token Type Names</A>.
<P>
</P>
<LI>
<A NAME="IDX10"></A>
<A NAME="IDX11"></A>
<A NAME="IDX12"></A>
A <EM>character token type</EM> (or <EM>literal character token</EM>) is
written in the grammar using the same syntax used in C for character
constants; for example, <CODE>'+'</CODE> is a character token type. A
character token type doesn't need to be declared unless you need to
specify its semantic value data type (see section <A HREF="bison_6.html#SEC43">Data Types of Semantic Values</A>), associativity, or precedence (see section <A HREF="bison_8.html#SEC70">Operator Precedence</A>).
<P>
By convention, a character token type is used only to represent a
token that consists of that particular character. Thus, the token
type <CODE>'+'</CODE> is used to represent the character `<SAMP>+</SAMP>' as a
token. Nothing enforces this convention, but if you depart from it,
your program will confuse other readers.
</P>
<P>
All the usual escape sequences used in character literals in C can be
used in Bison as well, but you must not use the null character as a
character literal because its ASCII code, zero, is the code <CODE>yylex</CODE>
returns for end-of-input (see section <A HREF="bison_7.html#SEC61">Calling Convention for <CODE>yylex</CODE></A>).
</P>
<P>
</P>
<LI>
<A NAME="IDX13"></A>
<A NAME="IDX14"></A>
<A NAME="IDX15"></A>
A <EM>literal string token</EM> is written like a C string constant; for
example, <CODE>"<="</CODE> is a literal string token. A literal string token
doesn't need to be declared unless you need to specify its semantic
value data type (see section <A HREF="bison_6.html#SEC43">3.5.1 Data Types of Semantic Values</A>), associativity, precedence
(see section <A HREF="bison_8.html#SEC70">5.3 Operator Precedence</A>).
<P>
You can associate the literal string token with a symbolic name as an
alias, using the <CODE>%token</CODE> declaration (see section <A HREF="bison_6.html#SEC49">Token Declarations</A>). If you don't do that, the lexical analyzer has to
retrieve the token number for the literal string token from the
<CODE>yytname</CODE> table (see section <A HREF="bison_7.html#SEC61">4.2.1 Calling Convention for <CODE>yylex</CODE></A>).
</P>
<P>
<STRONG>WARNING</STRONG>: literal string tokens do not work in Yacc.
</P>
<P>
By convention, a literal string token is used only to represent a token
that consists of that particular string. Thus, you should use the token
type <CODE>"<="</CODE> to represent the string `<SAMP><=</SAMP>' as a token. Bison
does not enforces this convention, but if you depart from it, people who
read your program will be confused.
</P>
<P>
All the escape sequences used in string literals in C can be used in
Bison as well. A literal string token must contain two or more
characters; for a token containing just one character, use a character
token (see above).
</UL>
<P>
How you choose to write a terminal symbol has no effect on its
grammatical meaning. That depends only on where it appears in rules and
on when the parser function returns that symbol.
</P>
<P>
The value returned by <CODE>yylex</CODE> is always one of the terminal symbols
(or 0 for end-of-input). Whichever way you write the token type in the
grammar rules, you write it the same way in the definition of <CODE>yylex</CODE>.
The numeric code for a character token type is simply the ASCII code for
the character, so <CODE>yylex</CODE> can use the identical character constant to
generate the requisite code. Each named token type becomes a C macro in
the parser file, so <CODE>yylex</CODE> can use the name to stand for the code.
(This is why periods don't make sense in terminal symbols.)
See section <A HREF="bison_7.html#SEC61">Calling Convention for <CODE>yylex</CODE></A>.
</P>
<P>
If <CODE>yylex</CODE> is defined in a separate file, you need to arrange for the
token-type macro definitions to be available there. Use the `<SAMP>-d</SAMP>'
option when you run Bison, so that it will write these macro definitions
into a separate header file `<TT><VAR>name</VAR>.tab.h</TT>' which you can include
in the other source files that need it. See section <A HREF="bison_12.html#SEC86">Invoking Bison</A>.
</P>
<P>
The symbol <CODE>error</CODE> is a terminal symbol reserved for error recovery
(see section <A HREF="bison_9.html#SEC80">6. Error Recovery</A>); you shouldn't use it for any other purpose.
In particular, <CODE>yylex</CODE> should never return this value.
</P>
<P>
<A NAME="Rules"></A>
<HR SIZE="6">
<A NAME="SEC40"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC39"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC41"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H2> 3.3 Syntax of Grammar Rules </H2>
<!--docid::SEC40::-->
<P>
A Bison grammar rule has the following general form:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre><VAR>result</VAR>: <VAR>components</VAR><small>...</small>
;
</pre></td></tr></table><P>
where <VAR>result</VAR> is the nonterminal symbol that this rule describes
and <VAR>components</VAR> are various terminal and nonterminal symbols that
are put together by this rule (see section <A HREF="bison_6.html#SEC39">3.2 Symbols, Terminal and Nonterminal</A>).
</P>
<P>
For example,
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>exp: exp '+' exp
;
</pre></td></tr></table><P>
says that two groupings of type <CODE>exp</CODE>, with a `<SAMP>+</SAMP>' token in between,
can be combined into a larger grouping of type <CODE>exp</CODE>.
</P>
<P>
Whitespace in rules is significant only to separate symbols. You can add
extra whitespace as you wish.
</P>
<P>
Scattered among the components can be <VAR>actions</VAR> that determine
the semantics of the rule. An action looks like this:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>{<VAR>C statements</VAR>}
</pre></td></tr></table><P>
Usually there is only one action and it follows the components.
See section <A HREF="bison_6.html#SEC45">3.5.3 Actions</A>.
</P>
<P>
<A NAME="IDX16"></A>
Multiple rules for the same <VAR>result</VAR> can be written separately or can
be joined with the vertical-bar character `<SAMP>|</SAMP>' as follows:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre><VAR>result</VAR>: <VAR>rule1-components</VAR><small>...</small>
| <VAR>rule2-components</VAR><small>...</small>
<small>...</small>
;
</pre></td></tr></table><P>
They are still considered distinct rules even when joined in this way.
</P>
<P>
If <VAR>components</VAR> in a rule is empty, it means that <VAR>result</VAR> can
match the empty string. For example, here is how to define a
comma-separated sequence of zero or more <CODE>exp</CODE> groupings:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>expseq: /* empty */
| expseq1
;
expseq1: exp
| expseq1 ',' exp
;
</pre></td></tr></table><P>
It is customary to write a comment `<SAMP>/* empty */</SAMP>' in each rule
with no components.
</P>
<P>
<A NAME="Recursion"></A>
<HR SIZE="6">
<A NAME="SEC41"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC40"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC42"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H2> 3.4 Recursive Rules </H2>
<!--docid::SEC41::-->
<P>
A rule is called <EM>recursive</EM> when its <VAR>result</VAR> nonterminal appears
also on its right hand side. Nearly all Bison grammars need to use
recursion, because that is the only way to define a sequence of any number
of somethings. Consider this recursive definition of a comma-separated
sequence of one or more expressions:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>expseq1: exp
| expseq1 ',' exp
;
</pre></td></tr></table><P>
<A NAME="IDX17"></A>
<A NAME="IDX18"></A>
Since the recursive use of <CODE>expseq1</CODE> is the leftmost symbol in the
right hand side, we call this <EM>left recursion</EM>. By contrast, here
the same construct is defined using <EM>right recursion</EM>:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>expseq1: exp
| exp ',' expseq1
;
</pre></td></tr></table><P>
Any kind of sequence can be defined using either left recursion or
right recursion, but you should always use left recursion, because it
can parse a sequence of any number of elements with bounded stack
space. Right recursion uses up space on the Bison stack in proportion
to the number of elements in the sequence, because all the elements
must be shifted onto the stack before the rule can be applied even
once. See section <A HREF="bison_8.html#SEC67">The Bison Parser Algorithm </A>, for
further explanation of this.
</P>
<P>
<A NAME="IDX19"></A>
<EM>Indirect</EM> or <EM>mutual</EM> recursion occurs when the result of the
rule does not appear directly on its right hand side, but does appear
in rules for other nonterminals which do appear on its right hand
side.
</P>
<P>
For example:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>expr: primary
| primary '+' primary
;
primary: constant
| '(' expr ')'
;
</pre></td></tr></table><P>
defines two mutually-recursive nonterminals, since each refers to the
other.
</P>
<P>
<A NAME="Semantics"></A>
<HR SIZE="6">
<A NAME="SEC42"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC41"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC43"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H2> 3.5 Defining Language Semantics </H2>
<!--docid::SEC42::-->
<P>
The grammar rules for a language determine only the syntax. The semantics
are determined by the semantic values associated with various tokens and
groupings, and by the actions taken when various groupings are recognized.
</P>
<P>
For example, the calculator calculates properly because the value
associated with each expression is the proper number; it adds properly
because the action for the grouping `<SAMP><VAR>x</VAR> + <VAR>y</VAR></SAMP>' is to add
the numbers associated with <VAR>x</VAR> and <VAR>y</VAR>.
</P>
<P>
<TABLE BORDER="0" CELLSPACING="0">
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC43">3.5.1 Data Types of Semantic Values</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Specifying one data type for all semantic values.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC44">3.5.2 More Than One Value Type</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Specifying several alternative data types.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC45">3.5.3 Actions</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">An action is the semantic definition of a grammar rule.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC46">3.5.4 Data Types of Values in Actions</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Specifying data types for actions to operate on.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC47">3.5.5 Actions in Mid-Rule</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Most actions go at the end of a rule.
This says when, why and how to use the exceptional
action in the middle of a rule.</TD></TR>
</TABLE>
<P>
<A NAME="Value Type"></A>
<HR SIZE="6">
<A NAME="SEC43"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC42"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC44"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC42"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.5.1 Data Types of Semantic Values </H3>
<!--docid::SEC43::-->
<P>
In a simple program it may be sufficient to use the same data type for
the semantic values of all language constructs. This was true in the
RPN and infix calculator examples (see section <A HREF="bison_5.html#SEC15">Reverse Polish Notation Calculator</A>).
</P>
<P>
Bison's default is to use type <CODE>int</CODE> for all semantic values. To
specify some other type, define <CODE>YYSTYPE</CODE> as a macro, like this:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>#define YYSTYPE double
</pre></td></tr></table><P>
This macro definition must go in the C declarations section of the grammar
file (see section <A HREF="bison_6.html#SEC34">Outline of a Bison Grammar</A>).
</P>
<P>
<A NAME="Multiple Types"></A>
<HR SIZE="6">
<A NAME="SEC44"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC43"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC45"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC42"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.5.2 More Than One Value Type </H3>
<!--docid::SEC44::-->
<P>
In most programs, you will need different data types for different kinds
of tokens and groupings. For example, a numeric constant may need type
<CODE>int</CODE> or <CODE>long</CODE>, while a string constant needs type <CODE>char *</CODE>,
and an identifier might need a pointer to an entry in the symbol table.
</P>
<P>
To use more than one data type for semantic values in one parser, Bison
requires you to do two things:
</P>
<P>
<UL>
<LI>
Specify the entire collection of possible data types, with the
<CODE>%union</CODE> Bison declaration (see section <A HREF="bison_6.html#SEC51">The Collection of Value Types</A>).
<P>
</P>
<LI>
Choose one of those types for each symbol (terminal or nonterminal)
for which semantic values are used. This is done for tokens with the
<CODE>%token</CODE> Bison declaration (see section <A HREF="bison_6.html#SEC49">Token Type Names</A>) and for groupings
with the <CODE>%type</CODE> Bison declaration (see section <A HREF="bison_6.html#SEC52">Nonterminal Symbols</A>).
</UL>
<P>
<A NAME="Actions"></A>
<HR SIZE="6">
<A NAME="SEC45"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC44"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC46"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC42"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.5.3 Actions </H3>
<!--docid::SEC45::-->
<P>
An action accompanies a syntactic rule and contains C code to be executed
each time an instance of that rule is recognized. The task of most actions
is to compute a semantic value for the grouping built by the rule from the
semantic values associated with tokens or smaller groupings.
</P>
<P>
An action consists of C statements surrounded by braces, much like a
compound statement in C. It can be placed at any position in the rule; it
is executed at that position. Most rules have just one action at the end
of the rule, following all the components. Actions in the middle of a rule
are tricky and used only for special purposes (see section <A HREF="bison_6.html#SEC47">Actions in Mid-Rule</A>).
</P>
<P>
The C code in an action can refer to the semantic values of the components
matched by the rule with the construct <CODE>$<VAR>n</VAR></CODE>, which stands for
the value of the <VAR>n</VAR>th component. The semantic value for the grouping
being constructed is <CODE>$$</CODE>. (Bison translates both of these constructs
into array element references when it copies the actions into the parser
file.)
</P>
<P>
Here is a typical example:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>exp: <small>...</small>
| exp '+' exp
{ $$ = $1 + $3; }
</pre></td></tr></table><P>
This rule constructs an <CODE>exp</CODE> from two smaller <CODE>exp</CODE> groupings
connected by a plus-sign token. In the action, <CODE>$1</CODE> and <CODE>$3</CODE>
refer to the semantic values of the two component <CODE>exp</CODE> groupings,
which are the first and third symbols on the right hand side of the rule.
The sum is stored into <CODE>$$</CODE> so that it becomes the semantic value of
the addition-expression just recognized by the rule. If there were a
useful semantic value associated with the `<SAMP>+</SAMP>' token, it could be
referred to as <CODE>$2</CODE>.</P>
<P>
<A NAME="IDX20"></A>
If you don't specify an action for a rule, Bison supplies a default:
<CODE>$$ = $1</CODE>. Thus, the value of the first symbol in the rule becomes
the value of the whole rule. Of course, the default rule is valid only
if the two data types match. There is no meaningful default action for
an empty rule; every empty rule must have an explicit action unless the
rule's value does not matter.
</P>
<P>
<CODE>$<VAR>n</VAR></CODE> with <VAR>n</VAR> zero or negative is allowed for reference
to tokens and groupings on the stack <EM>before</EM> those that match the
current rule. This is a very risky practice, and to use it reliably
you must be certain of the context in which the rule is applied. Here
is a case in which you can use this reliably:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>foo: expr bar '+' expr { <small>...</small> }
| expr bar '-' expr { <small>...</small> }
;
bar: /* empty */
{ previous_expr = $0; }
;
</pre></td></tr></table><P>
As long as <CODE>bar</CODE> is used only in the fashion shown here, <CODE>$0</CODE>
always refers to the <CODE>expr</CODE> which precedes <CODE>bar</CODE> in the
definition of <CODE>foo</CODE>.
</P>
<P>
<A NAME="Action Types"></A>
<HR SIZE="6">
<A NAME="SEC46"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC45"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC47"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC42"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.5.4 Data Types of Values in Actions </H3>
<!--docid::SEC46::-->
<P>
If you have chosen a single data type for semantic values, the <CODE>$$</CODE>
and <CODE>$<VAR>n</VAR></CODE> constructs always have that data type.
</P>
<P>
If you have used <CODE>%union</CODE> to specify a variety of data types, then you
must declare a choice among these types for each terminal or nonterminal
symbol that can have a semantic value. Then each time you use <CODE>$$</CODE> or
<CODE>$<VAR>n</VAR></CODE>, its data type is determined by which symbol it refers to
in the rule. In this example,</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>exp: <small>...</small>
| exp '+' exp
{ $$ = $1 + $3; }
</pre></td></tr></table><P>
<CODE>$1</CODE> and <CODE>$3</CODE> refer to instances of <CODE>exp</CODE>, so they all
have the data type declared for the nonterminal symbol <CODE>exp</CODE>. If
<CODE>$2</CODE> were used, it would have the data type declared for the
terminal symbol <CODE>'+'</CODE>, whatever that might be.</P>
<P>
Alternatively, you can specify the data type when you refer to the value,
by inserting `<SAMP><<VAR>type</VAR>></SAMP>' after the `<SAMP>$</SAMP>' at the beginning of the
reference. For example, if you have defined types as shown here:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>%union {
int itype;
double dtype;
}
</pre></td></tr></table><P>
then you can write <CODE>$<itype>1</CODE> to refer to the first subunit of the
rule as an integer, or <CODE>$<dtype>1</CODE> to refer to it as a double.
</P>
<P>
<A NAME="Mid-Rule Actions"></A>
<HR SIZE="6">
<A NAME="SEC47"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC46"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC48"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC42"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.5.5 Actions in Mid-Rule </H3>
<!--docid::SEC47::-->
<P>
Occasionally it is useful to put an action in the middle of a rule.
These actions are written just like usual end-of-rule actions, but they
are executed before the parser even recognizes the following components.
</P>
<P>
A mid-rule action may refer to the components preceding it using
<CODE>$<VAR>n</VAR></CODE>, but it may not refer to subsequent components because
it is run before they are parsed.
</P>
<P>
The mid-rule action itself counts as one of the components of the rule.
This makes a difference when there is another action later in the same rule
(and usually there is another at the end): you have to count the actions
along with the symbols when working out which number <VAR>n</VAR> to use in
<CODE>$<VAR>n</VAR></CODE>.
</P>
<P>
The mid-rule action can also have a semantic value. The action can set
its value with an assignment to <CODE>$$</CODE>, and actions later in the rule
can refer to the value using <CODE>$<VAR>n</VAR></CODE>. Since there is no symbol
to name the action, there is no way to declare a data type for the value
in advance, so you must use the `<SAMP>$<<small>...</small>></SAMP>' construct to specify a
data type each time you refer to this value.
</P>
<P>
There is no way to set the value of the entire rule with a mid-rule
action, because assignments to <CODE>$$</CODE> do not have that effect. The
only way to set the value for the entire rule is with an ordinary action
at the end of the rule.
</P>
<P>
Here is an example from a hypothetical compiler, handling a <CODE>let</CODE>
statement that looks like `<SAMP>let (<VAR>variable</VAR>) <VAR>statement</VAR></SAMP>' and
serves to create a variable named <VAR>variable</VAR> temporarily for the
duration of <VAR>statement</VAR>. To parse this construct, we must put
<VAR>variable</VAR> into the symbol table while <VAR>statement</VAR> is parsed, then
remove it afterward. Here is how it is done:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>stmt: LET '(' var ')'
{ $<context>$ = push_context ();
declare_variable ($3); }
stmt { $$ = $6;
pop_context ($<context>5); }
</pre></td></tr></table><P>
As soon as `<SAMP>let (<VAR>variable</VAR>)</SAMP>' has been recognized, the first
action is run. It saves a copy of the current semantic context (the
list of accessible variables) as its semantic value, using alternative
<CODE>context</CODE> in the data-type union. Then it calls
<CODE>declare_variable</CODE> to add the new variable to that list. Once the
first action is finished, the embedded statement <CODE>stmt</CODE> can be
parsed. Note that the mid-rule action is component number 5, so the
`<SAMP>stmt</SAMP>' is component number 6.
</P>
<P>
After the embedded statement is parsed, its semantic value becomes the
value of the entire <CODE>let</CODE>-statement. Then the semantic value from the
earlier action is used to restore the prior list of variables. This
removes the temporary <CODE>let</CODE>-variable from the list so that it won't
appear to exist while the rest of the program is parsed.
</P>
<P>
Taking action before a rule is completely recognized often leads to
conflicts since the parser must commit to a parse in order to execute the
action. For example, the following two rules, without mid-rule actions,
can coexist in a working parser because the parser can shift the open-brace
token and look at what follows before deciding whether there is a
declaration or not:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>compound: '{' declarations statements '}'
| '{' statements '}'
;
</pre></td></tr></table><P>
But when we add a mid-rule action as follows, the rules become nonfunctional:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>compound: { prepare_for_local_variables (); }
'{' declarations statements '}'
| '{' statements '}'
;
</pre></td></tr></table><P>
Now the parser is forced to decide whether to run the mid-rule action
when it has read no farther than the open-brace. In other words, it
must commit to using one rule or the other, without sufficient
information to do it correctly. (The open-brace token is what is called
the <EM>look-ahead</EM> token at this time, since the parser is still
deciding what to do about it. See section <A HREF="bison_8.html#SEC68">Look-Ahead Tokens</A>.)
</P>
<P>
You might think that you could correct the problem by putting identical
actions into the two rules, like this:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>compound: { prepare_for_local_variables (); }
'{' declarations statements '}'
| { prepare_for_local_variables (); }
'{' statements '}'
;
</pre></td></tr></table><P>
But this does not help, because Bison does not realize that the two actions
are identical. (Bison never tries to understand the C code in an action.)
</P>
<P>
If the grammar is such that a declaration can be distinguished from a
statement by the first token (which is true in C), then one solution which
does work is to put the action after the open-brace, like this:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>compound: '{' { prepare_for_local_variables (); }
declarations statements '}'
| '{' statements '}'
;
</pre></td></tr></table><P>
Now the first token of the following declaration or statement,
which would in any case tell Bison which rule to use, can still do so.
</P>
<P>
Another solution is to bury the action inside a nonterminal symbol which
serves as a subroutine:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>subroutine: /* empty */
{ prepare_for_local_variables (); }
;
compound: subroutine
'{' declarations statements '}'
| subroutine
'{' statements '}'
;
</pre></td></tr></table><P>
Now Bison can execute the action in the rule for <CODE>subroutine</CODE> without
deciding which rule for <CODE>compound</CODE> it will eventually use. Note that
the action is now at the end of its rule. Any mid-rule action can be
converted to an end-of-rule action in this way, and this is what Bison
actually does to implement mid-rule actions.
</P>
<P>
<A NAME="Declarations"></A>
<HR SIZE="6">
<A NAME="SEC48"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC47"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC49"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H2> 3.6 Bison Declarations </H2>
<!--docid::SEC48::-->
<P>
The <EM>Bison declarations</EM> section of a Bison grammar defines the symbols
used in formulating the grammar and the data types of semantic values.
See section <A HREF="bison_6.html#SEC39">3.2 Symbols, Terminal and Nonterminal</A>.
</P>
<P>
All token type names (but not single-character literal tokens such as
<CODE>'+'</CODE> and <CODE>'*'</CODE>) must be declared. Nonterminal symbols must be
declared if you need to specify which data type to use for the semantic
value (see section <A HREF="bison_6.html#SEC44">More Than One Value Type</A>).
</P>
<P>
The first rule in the file also specifies the start symbol, by default.
If you want some other symbol to be the start symbol, you must declare
it explicitly (see section <A HREF="bison_4.html#SEC7">Languages and Context-Free Grammars</A>).
</P>
<P>
<TABLE BORDER="0" CELLSPACING="0">
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC49">3.6.1 Token Type Names</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Declaring terminal symbols.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC50">3.6.2 Operator Precedence</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Declaring terminals with precedence and associativity.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC51">3.6.3 The Collection of Value Types</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Declaring the set of all semantic value types.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC52">3.6.4 Nonterminal Symbols</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Declaring the choice of type for a nonterminal symbol.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC53">3.6.5 Suppressing Conflict Warnings</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Suppressing warnings about shift/reduce conflicts.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC54">3.6.6 The Start-Symbol</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Specifying the start symbol.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC55">3.6.7 A Pure (Reentrant) Parser</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Requesting a reentrant parser.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_6.html#SEC56">3.6.8 Bison Declaration Summary</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Table of all Bison declarations.</TD></TR>
</TABLE>
<P>
<A NAME="Token Decl"></A>
<HR SIZE="6">
<A NAME="SEC49"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC48"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC50"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC48"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.6.1 Token Type Names </H3>
<!--docid::SEC49::-->
<P>
The basic way to declare a token type name (terminal symbol) is as follows:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>%token <VAR>name</VAR>
</pre></td></tr></table><P>
Bison will convert this into a <CODE>#define</CODE> directive in
the parser, so that the function <CODE>yylex</CODE> (if it is in this file)
can use the name <VAR>name</VAR> to stand for this token type's code.
</P>
<P>
Alternatively, you can use <CODE>%left</CODE>, <CODE>%right</CODE>, or <CODE>%nonassoc</CODE>
instead of <CODE>%token</CODE>, if you wish to specify precedence.
See section <A HREF="bison_6.html#SEC50">Operator Precedence</A>.
</P>
<P>
You can explicitly specify the numeric code for a token type by appending
an integer value in the field immediately following the token name:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>%token NUM 300
</pre></td></tr></table><P>
It is generally best, however, to let Bison choose the numeric codes for
all token types. Bison will automatically select codes that don't conflict
with each other or with ASCII characters.
</P>
<P>
In the event that the stack type is a union, you must augment the
<CODE>%token</CODE> or other token declaration to include the data type
alternative delimited by angle-brackets (see section <A HREF="bison_6.html#SEC44">More Than One Value Type</A>).
</P>
<P>
For example:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>%union { /* define stack type */
double val;
symrec *tptr;
}
%token <val> NUM /* define token NUM and its type */
</pre></td></tr></table><P>
You can associate a literal string token with a token type name by
writing the literal string at the end of a <CODE>%token</CODE>
declaration which declares the name. For example:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>%token arrow "=>"
</pre></td></tr></table><P>
For example, a grammar for the C language might specify these names with
equivalent literal string tokens:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>%token <operator> OR "||"
%token <operator> LE 134 "<="
%left OR "<="
</pre></td></tr></table><P>
Once you equate the literal string and the token name, you can use them
interchangeably in further declarations or the grammar rules. The
<CODE>yylex</CODE> function can use the token name or the literal string to
obtain the token type code number (see section <A HREF="bison_7.html#SEC61">4.2.1 Calling Convention for <CODE>yylex</CODE></A>).
</P>
<P>
<A NAME="Precedence Decl"></A>
<HR SIZE="6">
<A NAME="SEC50"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC49"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC51"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC48"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.6.2 Operator Precedence </H3>
<!--docid::SEC50::-->
<P>
Use the <CODE>%left</CODE>, <CODE>%right</CODE> or <CODE>%nonassoc</CODE> declaration to
declare a token and specify its precedence and associativity, all at
once. These are called <EM>precedence declarations</EM>.
See section <A HREF="bison_8.html#SEC70">Operator Precedence</A>, for general information on operator precedence.
</P>
<P>
The syntax of a precedence declaration is the same as that of
<CODE>%token</CODE>: either
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>%left <VAR>symbols</VAR><small>...</small>
</pre></td></tr></table><P>
or
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>%left <<VAR>type</VAR>> <VAR>symbols</VAR><small>...</small>
</pre></td></tr></table><P>
And indeed any of these declarations serves the purposes of <CODE>%token</CODE>.
But in addition, they specify the associativity and relative precedence for
all the <VAR>symbols</VAR>:
</P>
<P>
<UL>
<LI>
The associativity of an operator <VAR>op</VAR> determines how repeated uses
of the operator nest: whether `<SAMP><VAR>x</VAR> <VAR>op</VAR> <VAR>y</VAR> <VAR>op</VAR>
<VAR>z</VAR></SAMP>' is parsed by grouping <VAR>x</VAR> with <VAR>y</VAR> first or by
grouping <VAR>y</VAR> with <VAR>z</VAR> first. <CODE>%left</CODE> specifies
left-associativity (grouping <VAR>x</VAR> with <VAR>y</VAR> first) and
<CODE>%right</CODE> specifies right-associativity (grouping <VAR>y</VAR> with
<VAR>z</VAR> first). <CODE>%nonassoc</CODE> specifies no associativity, which
means that `<SAMP><VAR>x</VAR> <VAR>op</VAR> <VAR>y</VAR> <VAR>op</VAR> <VAR>z</VAR></SAMP>' is
considered a syntax error.
<P>
</P>
<LI>
The precedence of an operator determines how it nests with other operators.
All the tokens declared in a single precedence declaration have equal
precedence and nest together according to their associativity.
When two tokens declared in different precedence declarations associate,
the one declared later has the higher precedence and is grouped first.
</UL>
<P>
<A NAME="Union Decl"></A>
<HR SIZE="6">
<A NAME="SEC51"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC50"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC52"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC48"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.6.3 The Collection of Value Types </H3>
<!--docid::SEC51::-->
<P>
The <CODE>%union</CODE> declaration specifies the entire collection of possible
data types for semantic values. The keyword <CODE>%union</CODE> is followed by a
pair of braces containing the same thing that goes inside a <CODE>union</CODE> in
C.
</P>
<P>
For example:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>%union {
double val;
symrec *tptr;
}
</pre></td></tr></table><P>
This says that the two alternative types are <CODE>double</CODE> and <CODE>symrec
*</CODE>. They are given names <CODE>val</CODE> and <CODE>tptr</CODE>; these names are used
in the <CODE>%token</CODE> and <CODE>%type</CODE> declarations to pick one of the types
for a terminal or nonterminal symbol (see section <A HREF="bison_6.html#SEC52">Nonterminal Symbols</A>).
</P>
<P>
Note that, unlike making a <CODE>union</CODE> declaration in C, you do not write
a semicolon after the closing brace.
</P>
<P>
<A NAME="Type Decl"></A>
<HR SIZE="6">
<A NAME="SEC52"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC51"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC53"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC48"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.6.4 Nonterminal Symbols </H3>
<!--docid::SEC52::-->
<P>
When you use <CODE>%union</CODE> to specify multiple value types, you must
declare the value type of each nonterminal symbol for which values are
used. This is done with a <CODE>%type</CODE> declaration, like this:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>%type <<VAR>type</VAR>> <VAR>nonterminal</VAR><small>...</small>
</pre></td></tr></table><P>
Here <VAR>nonterminal</VAR> is the name of a nonterminal symbol, and <VAR>type</VAR>
is the name given in the <CODE>%union</CODE> to the alternative that you want
(see section <A HREF="bison_6.html#SEC51">The Collection of Value Types</A>). You can give any number of nonterminal symbols in
the same <CODE>%type</CODE> declaration, if they have the same value type. Use
spaces to separate the symbol names.
</P>
<P>
You can also declare the value type of a terminal symbol. To do this,
use the same <CODE><<VAR>type</VAR>></CODE> construction in a declaration for the
terminal symbol. All kinds of token declarations allow
<CODE><<VAR>type</VAR>></CODE>.
</P>
<P>
<A NAME="Expect Decl"></A>
<HR SIZE="6">
<A NAME="SEC53"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC52"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC54"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC48"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.6.5 Suppressing Conflict Warnings </H3>
<!--docid::SEC53::-->
<P>
Bison normally warns if there are any conflicts in the grammar
(see section <A HREF="bison_8.html#SEC69">Shift/Reduce Conflicts</A>), but most real grammars have harmless shift/reduce
conflicts which are resolved in a predictable way and would be difficult to
eliminate. It is desirable to suppress the warning about these conflicts
unless the number of conflicts changes. You can do this with the
<CODE>%expect</CODE> declaration.
</P>
<P>
The declaration looks like this:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>%expect <VAR>n</VAR>
</pre></td></tr></table><P>
Here <VAR>n</VAR> is a decimal integer. The declaration says there should be no
warning if there are <VAR>n</VAR> shift/reduce conflicts and no reduce/reduce
conflicts. The usual warning is given if there are either more or fewer
conflicts, or if there are any reduce/reduce conflicts.
</P>
<P>
In general, using <CODE>%expect</CODE> involves these steps:
</P>
<P>
<UL>
<LI>
Compile your grammar without <CODE>%expect</CODE>. Use the `<SAMP>-v</SAMP>' option
to get a verbose list of where the conflicts occur. Bison will also
print the number of conflicts.
<P>
</P>
<LI>
Check each of the conflicts to make sure that Bison's default
resolution is what you really want. If not, rewrite the grammar and
go back to the beginning.
<P>
</P>
<LI>
Add an <CODE>%expect</CODE> declaration, copying the number <VAR>n</VAR> from the
number which Bison printed.
</UL>
<P>
Now Bison will stop annoying you about the conflicts you have checked, but
it will warn you again if changes in the grammar result in additional
conflicts.
</P>
<P>
<A NAME="Start Decl"></A>
<HR SIZE="6">
<A NAME="SEC54"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC53"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC55"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC48"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.6.6 The Start-Symbol </H3>
<!--docid::SEC54::-->
<P>
Bison assumes by default that the start symbol for the grammar is the first
nonterminal specified in the grammar specification section. The programmer
may override this restriction with the <CODE>%start</CODE> declaration as follows:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>%start <VAR>symbol</VAR>
</pre></td></tr></table><P>
<A NAME="Pure Decl"></A>
<HR SIZE="6">
<A NAME="SEC55"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC54"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC56"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC48"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.6.7 A Pure (Reentrant) Parser </H3>
<!--docid::SEC55::-->
<P>
A <EM>reentrant</EM> program is one which does not alter in the course of
execution; in other words, it consists entirely of <EM>pure</EM> (read-only)
code. Reentrancy is important whenever asynchronous execution is possible;
for example, a nonreentrant program may not be safe to call from a signal
handler. In systems with multiple threads of control, a nonreentrant
program must be called only within interlocks.
</P>
<P>
Normally, Bison generates a parser which is not reentrant. This is
suitable for most uses, and it permits compatibility with YACC. (The
standard YACC interfaces are inherently nonreentrant, because they use
statically allocated variables for communication with <CODE>yylex</CODE>,
including <CODE>yylval</CODE> and <CODE>yylloc</CODE>.)
</P>
<P>
Alternatively, you can generate a pure, reentrant parser. The Bison
declaration <CODE>%pure_parser</CODE> says that you want the parser to be
reentrant. It looks like this:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>%pure_parser
</pre></td></tr></table><P>
The result is that the communication variables <CODE>yylval</CODE> and
<CODE>yylloc</CODE> become local variables in <CODE>yyparse</CODE>, and a different
calling convention is used for the lexical analyzer function
<CODE>yylex</CODE>. See section <A HREF="bison_7.html#SEC64">Calling Conventions for Pure Parsers</A>, for the details of this. The variable <CODE>yynerrs</CODE> also
becomes local in <CODE>yyparse</CODE> (see section <A HREF="bison_7.html#SEC65">The Error Reporting Function <CODE>yyerror</CODE></A>). The convention for calling
<CODE>yyparse</CODE> itself is unchanged.
</P>
<P>
Whether the parser is pure has nothing to do with the grammar rules.
You can generate either a pure parser or a nonreentrant parser from any
valid grammar.
</P>
<P>
<A NAME="Decl Summary"></A>
<HR SIZE="6">
<A NAME="SEC56"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC55"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC57"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC48"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H3> 3.6.8 Bison Declaration Summary </H3>
<!--docid::SEC56::-->
<P>
Here is a summary of all Bison declarations:
</P>
<P>
</P>
<DL COMPACT>
<DT><CODE>%union</CODE>
<DD>Declare the collection of data types that semantic values may have
(see section <A HREF="bison_6.html#SEC51">The Collection of Value Types</A>).
<P>
</P>
<DT><CODE>%token</CODE>
<DD>Declare a terminal symbol (token type name) with no precedence
or associativity specified (see section <A HREF="bison_6.html#SEC49">Token Type Names</A>).
<P>
</P>
<DT><CODE>%right</CODE>
<DD>Declare a terminal symbol (token type name) that is right-associative
(see section <A HREF="bison_6.html#SEC50">Operator Precedence</A>).
<P>
</P>
<DT><CODE>%left</CODE>
<DD>Declare a terminal symbol (token type name) that is left-associative
(see section <A HREF="bison_6.html#SEC50">Operator Precedence</A>).
<P>
</P>
<DT><CODE>%nonassoc</CODE>
<DD>Declare a terminal symbol (token type name) that is nonassociative
(using it in a way that would be associative is a syntax error)
(see section <A HREF="bison_6.html#SEC50">Operator Precedence</A>).
<P>
</P>
<DT><CODE>%type</CODE>
<DD>Declare the type of semantic values for a nonterminal symbol
(see section <A HREF="bison_6.html#SEC52">Nonterminal Symbols</A>).
<P>
</P>
<DT><CODE>%start</CODE>
<DD>Specify the grammar's start symbol (see section <A HREF="bison_6.html#SEC54">The Start-Symbol</A>).
<P>
</P>
<DT><CODE>%expect</CODE>
<DD>Declare the expected number of shift-reduce conflicts
(see section <A HREF="bison_6.html#SEC53">Suppressing Conflict Warnings</A>).
<P>
</P>
<DT><CODE>%pure_parser</CODE>
<DD>Request a pure (reentrant) parser program (see section <A HREF="bison_6.html#SEC55">A Pure (Reentrant) Parser</A>).
<P>
</P>
<DT><CODE>%no_lines</CODE>
<DD>Don't generate any <CODE>#line</CODE> preprocessor commands in the parser
file. Ordinarily Bison writes these commands in the parser file so that
the C compiler and debuggers will associate errors and object code with
your source file (the grammar file). This directive causes them to
associate errors with the parser file, treating it an independent source
file in its own right.
<P>
</P>
<DT><CODE>%raw</CODE>
<DD>The output file `<TT><VAR>name</VAR>.h</TT>' normally defines the tokens with
Yacc-compatible token numbers. If this option is specified, the
internal Bison numbers are used instead. (Yacc-compatible numbers start
at 257 except for single character tokens; Bison assigns token numbers
sequentially for all tokens starting at 3.)
<P>
</P>
<DT><CODE>%token_table</CODE>
<DD>Generate an array of token names in the parser file. The name of the
array is <CODE>yytname</CODE>; <CODE>yytname[<VAR>i</VAR>]</CODE> is the name of the
token whose internal Bison token code number is <VAR>i</VAR>. The first three
elements of <CODE>yytname</CODE> are always <CODE>"$"</CODE>, <CODE>"error"</CODE>, and
<CODE>"$illegal"</CODE>; after these come the symbols defined in the grammar
file.
<P>
For single-character literal tokens and literal string tokens, the name
in the table includes the single-quote or double-quote characters: for
example, <CODE>"'+'"</CODE> is a single-character literal and <CODE>"\"<=\""</CODE>
is a literal string token. All the characters of the literal string
token appear verbatim in the string found in the table; even
double-quote characters are not escaped. For example, if the token
consists of three characters `<SAMP>*"*</SAMP>', its string in <CODE>yytname</CODE>
contains `<SAMP>"*"*"</SAMP>'. (In C, that would be written as
<CODE>"\"*\"*\""</CODE>).
</P>
<P>
When you specify <CODE>%token_table</CODE>, Bison also generates macro
definitions for macros <CODE>YYNTOKENS</CODE>, <CODE>YYNNTS</CODE>, and
<CODE>YYNRULES</CODE>, and <CODE>YYNSTATES</CODE>:
</P>
<P>
</P>
<DL COMPACT>
<DT><CODE>YYNTOKENS</CODE>
<DD>The highest token number, plus one.
<DT><CODE>YYNNTS</CODE>
<DD>The number of non-terminal symbols.
<DT><CODE>YYNRULES</CODE>
<DD>The number of grammar rules,
<DT><CODE>YYNSTATES</CODE>
<DD>The number of parser states (see section <A HREF="bison_8.html#SEC76">5.5 Parser States</A>).
</DL>
</DL>
<P>
<A NAME="Multiple Parsers"></A>
<HR SIZE="6">
<A NAME="SEC57"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC56"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<H2> 3.7 Multiple Parsers in the Same Program </H2>
<!--docid::SEC57::-->
<P>
Most programs that use Bison parse only one language and therefore contain
only one Bison parser. But what if you want to parse more than one
language with the same program? Then you need to avoid a name conflict
between different definitions of <CODE>yyparse</CODE>, <CODE>yylval</CODE>, and so on.
</P>
<P>
The easy way to do this is to use the option `<SAMP>-p <VAR>prefix</VAR></SAMP>'
(see section <A HREF="bison_12.html#SEC86">Invoking Bison</A>). This renames the interface functions and
variables of the Bison parser to start with <VAR>prefix</VAR> instead of
`<SAMP>yy</SAMP>'. You can use this to give each parser distinct names that do
not conflict.
</P>
<P>
The precise list of symbols renamed is <CODE>yyparse</CODE>, <CODE>yylex</CODE>,
<CODE>yyerror</CODE>, <CODE>yynerrs</CODE>, <CODE>yylval</CODE>, <CODE>yychar</CODE> and
<CODE>yydebug</CODE>. For example, if you use `<SAMP>-p c</SAMP>', the names become
<CODE>cparse</CODE>, <CODE>clex</CODE>, and so on.
</P>
<P>
<STRONG>All the other variables and macros associated with Bison are not
renamed.</STRONG> These others are not global; there is no conflict if the same
name is used in different parsers. For example, <CODE>YYSTYPE</CODE> is not
renamed, but defining this in different ways in different parsers causes
no trouble (see section <A HREF="bison_6.html#SEC43">Data Types of Semantic Values</A>).
</P>
<P>
The `<SAMP>-p</SAMP>' option works by adding macro definitions to the beginning
of the parser source file, defining <CODE>yyparse</CODE> as
<CODE><VAR>prefix</VAR>parse</CODE>, and so on. This effectively substitutes one
name for the other in the entire parser file.
</P>
<P>
<A NAME="Interface"></A>
<HR SIZE="6">
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC33"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> >> </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison.html#SEC_Top">Top</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_toc.html#SEC_Contents">Contents</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_15.html#SEC92">Index</A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_abt.html#SEC_About"> ? </A>]</TD>
</TR></TABLE>
<BR>
<FONT SIZE="-1">
This document was generated
by <I>Frank B. Brokken</I> on <I>January, 28 2005</I>
using <A HREF="http://texi2html.cvshome.org"><I>texi2html</I></A>
</FONT>
</BODY>
</HTML>
|