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
|
<!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: Interface</TITLE>
<META NAME="description" CONTENT="Bison 2.21.5: Interface">
<META NAME="keywords" CONTENT="Bison 2.21.5: Interface">
<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="SEC58"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_6.html#SEC57"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC59"> > </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.html#SEC_Top"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_8.html#SEC67"> >> </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> 4. Parser C-Language Interface </H1>
<!--docid::SEC58::-->
<P>
The Bison parser is actually a C function named <CODE>yyparse</CODE>. Here we
describe the interface conventions of <CODE>yyparse</CODE> and the other
functions that it needs to use.
</P>
<P>
Keep in mind that the parser uses many C identifiers starting with
`<SAMP>yy</SAMP>' and `<SAMP>YY</SAMP>' for internal purposes. If you use such an
identifier (aside from those in this manual) in an action or in additional
C code in the grammar file, you are likely to run into trouble.
</P>
<P>
<TABLE BORDER="0" CELLSPACING="0">
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_7.html#SEC59">4.1 The Parser Function <CODE>yyparse</CODE></A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">How to call <CODE>yyparse</CODE> and what it returns.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_7.html#SEC60">4.2 The Lexical Analyzer Function <CODE>yylex</CODE></A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">You must supply a function <CODE>yylex</CODE>
which reads tokens.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_7.html#SEC65">4.3 The Error Reporting Function <CODE>yyerror</CODE></A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">You must supply a function <CODE>yyerror</CODE>.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_7.html#SEC66">4.4 Special Features for Use in Actions</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">Special features for use in actions.</TD></TR>
</TABLE>
<P>
<A NAME="Parser Function"></A>
<HR SIZE="6">
<A NAME="SEC59"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC60"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_8.html#SEC67"> >> </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> 4.1 The Parser Function <CODE>yyparse</CODE> </H2>
<!--docid::SEC59::-->
<P>
You call the function <CODE>yyparse</CODE> to cause parsing to occur. This
function reads tokens, executes actions, and ultimately returns when it
encounters end-of-input or an unrecoverable syntax error. You can also
write an action which directs <CODE>yyparse</CODE> to return immediately without
reading further.
</P>
<P>
The value returned by <CODE>yyparse</CODE> is 0 if parsing was successful (return
is due to end-of-input).
</P>
<P>
The value is 1 if parsing failed (return is due to a syntax error).
</P>
<P>
In an action, you can cause immediate return from <CODE>yyparse</CODE> by using
these macros:
</P>
<P>
</P>
<DL COMPACT>
<DT><CODE>YYACCEPT</CODE>
<DD><A NAME="IDX21"></A>
Return immediately with value 0 (to report success).
<P>
</P>
<DT><CODE>YYABORT</CODE>
<DD><A NAME="IDX22"></A>
Return immediately with value 1 (to report failure).
</DL>
<P>
<A NAME="Lexical"></A>
<HR SIZE="6">
<A NAME="SEC60"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC59"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC61"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_8.html#SEC67"> >> </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> 4.2 The Lexical Analyzer Function <CODE>yylex</CODE> </H2>
<!--docid::SEC60::-->
<P>
The <EM>lexical analyzer</EM> function, <CODE>yylex</CODE>, recognizes tokens from
the input stream and returns them to the parser. Bison does not create
this function automatically; you must write it so that <CODE>yyparse</CODE> can
call it. The function is sometimes referred to as a lexical scanner.
</P>
<P>
In simple programs, <CODE>yylex</CODE> is often defined at the end of the Bison
grammar file. If <CODE>yylex</CODE> is defined in a separate source file, you
need to arrange for the token-type macro definitions to be available there.
To do this, 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>
<TABLE BORDER="0" CELLSPACING="0">
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_7.html#SEC61">4.2.1 Calling Convention for <CODE>yylex</CODE></A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">How <CODE>yyparse</CODE> calls <CODE>yylex</CODE>.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_7.html#SEC62">4.2.2 Semantic Values of Tokens</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">How <CODE>yylex</CODE> must return the semantic value
of the token it has read.</TD></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_7.html#SEC63">4.2.3 Textual Positions of Tokens</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">How <CODE>yylex</CODE> must return the text position</TD></TR>
<TR><TH COLSPAN="3" ALIGN="left" VALIGN="TOP"> (line number, etc.) of the token, if the
</TH></TR>
<TR><TH COLSPAN="3" ALIGN="left" VALIGN="TOP"> actions want that.
</TH></TR>
<TR><TD ALIGN="left" VALIGN="TOP"><A HREF="bison_7.html#SEC64">4.2.4 Calling Conventions for Pure Parsers</A></TD><TD> </TD><TD ALIGN="left" VALIGN="TOP">How the calling convention differs
in a pure parser (@pxref{Pure Decl, ,A Pure (Reentrant) Parser}).</TD></TR>
</TABLE>
<P>
<A NAME="Calling Convention"></A>
<HR SIZE="6">
<A NAME="SEC61"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC60"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC62"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC60"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_8.html#SEC67"> >> </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> 4.2.1 Calling Convention for <CODE>yylex</CODE> </H3>
<!--docid::SEC61::-->
<P>
The value that <CODE>yylex</CODE> returns must be the numeric code for the type
of token it has just found, or 0 for end-of-input.
</P>
<P>
When a token is referred to in the grammar rules by a name, that name
in the parser file becomes a C macro whose definition is the proper
numeric code for that token type. So <CODE>yylex</CODE> can use the name
to indicate that type. See section <A HREF="bison_6.html#SEC39">3.2 Symbols, Terminal and Nonterminal</A>.
</P>
<P>
When a token is referred to in the grammar rules by a character literal,
the numeric code for that character is also the code for the token type.
So <CODE>yylex</CODE> can simply return that character code. The null character
must not be used this way, because its code is zero and that is what
signifies end-of-input.
</P>
<P>
Here is an example showing these things:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>yylex ()
{
<small>...</small>
if (c == EOF) /* Detect end of file. */
return 0;
<small>...</small>
if (c == '+' || c == '-')
return c; /* Assume token type for `+' is '+'. */
<small>...</small>
return INT; /* Return the type of the token. */
<small>...</small>
}
</pre></td></tr></table><P>
This interface has been designed so that the output from the <CODE>lex</CODE>
utility can be used without change as the definition of <CODE>yylex</CODE>.
</P>
<P>
If the grammar uses literal string tokens, there are two ways that
<CODE>yylex</CODE> can determine the token type codes for them:
</P>
<P>
<UL>
<LI>
If the grammar defines symbolic token names as aliases for the
literal string tokens, <CODE>yylex</CODE> can use these symbolic names like
all others. In this case, the use of the literal string tokens in
the grammar file has no effect on <CODE>yylex</CODE>.
<P>
</P>
<LI>
<CODE>yylex</CODE> can find the multi-character token in the <CODE>yytname</CODE>
table. The index of the token in the table is the token type's code.
The name of a multi-character token is recorded in <CODE>yytname</CODE> with a
double-quote, the token's characters, and another double-quote. The
token's characters are not escaped in any way; they appear verbatim in
the contents of the string in the table.
<P>
Here's code for looking up a token in <CODE>yytname</CODE>, assuming that the
characters of the token are stored in <CODE>token_buffer</CODE>.
</P>
<P>
<TABLE><tr><td> </td><td class=smallexample><pre><FONT SIZE=-1>for (i = 0; i < YYNTOKENS; i++)
{
if (yytname[i] != 0
&& yytname[i][0] == '"'
&& strncmp (yytname[i] + 1, token_buffer,
strlen (token_buffer))
&& yytname[i][strlen (token_buffer) + 1] == '"'
&& yytname[i][strlen (token_buffer) + 2] == 0)
break;
}
</FONT></pre></td></tr></table><P>
The <CODE>yytname</CODE> table is generated only if you use the
<CODE>%token_table</CODE> declaration. See section <A HREF="bison_6.html#SEC56">3.6.8 Bison Declaration Summary</A>.
</UL>
<P>
<A NAME="Token Values"></A>
<HR SIZE="6">
<A NAME="SEC62"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC61"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC63"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC60"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_8.html#SEC67"> >> </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> 4.2.2 Semantic Values of Tokens </H3>
<!--docid::SEC62::-->
<P>
<A NAME="IDX23"></A>
In an ordinary (nonreentrant) parser, the semantic value of the token must
be stored into the global variable <CODE>yylval</CODE>. When you are using
just one data type for semantic values, <CODE>yylval</CODE> has that type.
Thus, if the type is <CODE>int</CODE> (the default), you might write this in
<CODE>yylex</CODE>:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre> <small>...</small>
yylval = value; /* Put value onto Bison stack. */
return INT; /* Return the type of the token. */
<small>...</small>
</pre></td></tr></table><P>
When you are using multiple data types, <CODE>yylval</CODE>'s type is a union
made from the <CODE>%union</CODE> declaration (see section <A HREF="bison_6.html#SEC51">The Collection of Value Types</A>). So when
you store a token's value, you must use the proper member of the union.
If the <CODE>%union</CODE> declaration looks like this:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>%union {
int intval;
double val;
symrec *tptr;
}
</pre></td></tr></table><P>
then the code in <CODE>yylex</CODE> might look like this:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre> <small>...</small>
yylval.intval = value; /* Put value onto Bison stack. */
return INT; /* Return the type of the token. */
<small>...</small>
</pre></td></tr></table><P>
<A NAME="Token Positions"></A>
<HR SIZE="6">
<A NAME="SEC63"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC62"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC64"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC60"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_8.html#SEC67"> >> </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> 4.2.3 Textual Positions of Tokens </H3>
<!--docid::SEC63::-->
<P>
<A NAME="IDX24"></A>
If you are using the `<SAMP>@<VAR>n</VAR></SAMP>'-feature (see section <A HREF="bison_7.html#SEC66">Special Features for Use in Actions</A>) in
actions to keep track of the textual locations of tokens and groupings,
then you must provide this information in <CODE>yylex</CODE>. The function
<CODE>yyparse</CODE> expects to find the textual location of a token just parsed
in the global variable <CODE>yylloc</CODE>. So <CODE>yylex</CODE> must store the
proper data in that variable. The value of <CODE>yylloc</CODE> is a structure
and you need only initialize the members that are going to be used by the
actions. The four members are called <CODE>first_line</CODE>,
<CODE>first_column</CODE>, <CODE>last_line</CODE> and <CODE>last_column</CODE>. Note that
the use of this feature makes the parser noticeably slower.
</P>
<P>
<A NAME="IDX25"></A>
The data type of <CODE>yylloc</CODE> has the name <CODE>YYLTYPE</CODE>.
</P>
<P>
<A NAME="Pure Calling"></A>
<HR SIZE="6">
<A NAME="SEC64"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC63"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC65"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC60"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_8.html#SEC67"> >> </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> 4.2.4 Calling Conventions for Pure Parsers </H3>
<!--docid::SEC64::-->
<P>
When you use the Bison declaration <CODE>%pure_parser</CODE> to request a
pure, reentrant parser, the global communication variables <CODE>yylval</CODE>
and <CODE>yylloc</CODE> cannot be used. (See section <A HREF="bison_6.html#SEC55">A Pure (Reentrant) Parser</A>.) In such parsers the two global variables are replaced by
pointers passed as arguments to <CODE>yylex</CODE>. You must declare them as
shown here, and pass the information back by storing it through those
pointers.
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>yylex (lvalp, llocp)
YYSTYPE *lvalp;
YYLTYPE *llocp;
{
<small>...</small>
*lvalp = value; /* Put value onto Bison stack. */
return INT; /* Return the type of the token. */
<small>...</small>
}
</pre></td></tr></table><P>
If the grammar file does not use the `<SAMP>@</SAMP>' constructs to refer to
textual positions, then the type <CODE>YYLTYPE</CODE> will not be defined. In
this case, omit the second argument; <CODE>yylex</CODE> will be called with
only one argument.
</P>
<P>
<A NAME="IDX26"></A>
If you use a reentrant parser, you can optionally pass additional
parameter information to it in a reentrant way. To do so, define the
macro <CODE>YYPARSE_PARAM</CODE> as a variable name. This modifies the
<CODE>yyparse</CODE> function to accept one argument, of type <CODE>void *</CODE>,
with that name.
</P>
<P>
When you call <CODE>yyparse</CODE>, pass the address of an object, casting the
address to <CODE>void *</CODE>. The grammar actions can refer to the contents
of the object by casting the pointer value back to its proper type and
then dereferencing it. Here's an example. Write this in the parser:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>%{
struct parser_control
{
int nastiness;
int randomness;
};
#define YYPARSE_PARAM parm
%}
</pre></td></tr></table><P>
Then call the parser like this:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>struct parser_control
{
int nastiness;
int randomness;
};
<small>...</small>
{
struct parser_control foo;
<small>...</small> /* Store proper data in <CODE>foo</CODE>. */
value = yyparse ((void *) &foo);
<small>...</small>
}
</pre></td></tr></table><P>
In the grammar actions, use expressions like this to refer to the data:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>((struct parser_control *) parm)->randomness
</pre></td></tr></table><P>
<A NAME="IDX27"></A>
If you wish to pass the additional parameter data to <CODE>yylex</CODE>,
define the macro <CODE>YYLEX_PARAM</CODE> just like <CODE>YYPARSE_PARAM</CODE>, as
shown here:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>%{
struct parser_control
{
int nastiness;
int randomness;
};
#define YYPARSE_PARAM parm
#define YYLEX_PARAM parm
%}
</pre></td></tr></table><P>
You should then define <CODE>yylex</CODE> to accept one additional
argument--the value of <CODE>parm</CODE>. (This makes either two or three
arguments in total, depending on whether an argument of type
<CODE>YYLTYPE</CODE> is passed.) You can declare the argument as a pointer to
the proper object type, or you can declare it as <CODE>void *</CODE> and
access the contents as shown above.
</P>
<P>
You can use `<SAMP>%pure_parser</SAMP>' to request a reentrant parser without
also using <CODE>YYPARSE_PARAM</CODE>. Then you should call <CODE>yyparse</CODE>
with no arguments, as usual.
</P>
<P>
<A NAME="Error Reporting"></A>
<HR SIZE="6">
<A NAME="SEC65"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC64"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC66"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_8.html#SEC67"> >> </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> 4.3 The Error Reporting Function <CODE>yyerror</CODE> </H2>
<!--docid::SEC65::-->
<P>
The Bison parser detects a <EM>parse error</EM> or <EM>syntax error</EM>
whenever it reads a token which cannot satisfy any syntax rule. A
action in the grammar can also explicitly proclaim an error, using the
macro <CODE>YYERROR</CODE> (see section <A HREF="bison_7.html#SEC66">Special Features for Use in Actions</A>).
</P>
<P>
The Bison parser expects to report the error by calling an error
reporting function named <CODE>yyerror</CODE>, which you must supply. It is
called by <CODE>yyparse</CODE> whenever a syntax error is found, and it
receives one argument. For a parse error, the string is normally
<CODE>"parse error"</CODE>.
</P>
<P>
<A NAME="IDX28"></A>
If you define the macro <CODE>YYERROR_VERBOSE</CODE> in the Bison declarations
section (see section <A HREF="bison_6.html#SEC36">The Bison Declarations Section</A>), then Bison provides a more verbose
and specific error message string instead of just plain <CODE>"parse
error"</CODE>. It doesn't matter what definition you use for
<CODE>YYERROR_VERBOSE</CODE>, just whether you define it.
</P>
<P>
The parser can detect one other kind of error: stack overflow. This
happens when the input contains constructions that are very deeply
nested. It isn't likely you will encounter this, since the Bison
parser extends its stack automatically up to a very large limit. But
if overflow happens, <CODE>yyparse</CODE> calls <CODE>yyerror</CODE> in the usual
fashion, except that the argument string is <CODE>"parser stack
overflow"</CODE>.
</P>
<P>
The following definition suffices in simple programs:
</P>
<P>
<TABLE><tr><td> </td><td class=example><pre>yyerror (s)
char *s;
{
fprintf (stderr, "%s\n", s);
}
</pre></td></tr></table><P>
After <CODE>yyerror</CODE> returns to <CODE>yyparse</CODE>, the latter will attempt
error recovery if you have written suitable error recovery grammar rules
(see section <A HREF="bison_9.html#SEC80">6. Error Recovery</A>). If recovery is impossible, <CODE>yyparse</CODE> will
immediately return 1.
</P>
<P>
<A NAME="IDX29"></A>
The variable <CODE>yynerrs</CODE> contains the number of syntax errors
encountered so far. Normally this variable is global; but if you
request a pure parser (see section <A HREF="bison_6.html#SEC55">A Pure (Reentrant) Parser</A>) then it is a local variable
which only the actions can access.
</P>
<P>
<A NAME="Action Features"></A>
<HR SIZE="6">
<A NAME="SEC66"></A>
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC65"> < </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_8.html#SEC67"> > </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> Up </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_8.html#SEC67"> >> </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> 4.4 Special Features for Use in Actions </H2>
<!--docid::SEC66::-->
<P>
Here is a table of Bison constructs, variables and macros that
are useful in actions.
</P>
<P>
</P>
<DL COMPACT>
<DT>`<SAMP>$$</SAMP>'
<DD>Acts like a variable that contains the semantic value for the
grouping made by the current rule. See section <A HREF="bison_6.html#SEC45">3.5.3 Actions</A>.
<P>
</P>
<DT>`<SAMP>$<VAR>n</VAR></SAMP>'
<DD>Acts like a variable that contains the semantic value for the
<VAR>n</VAR>th component of the current rule. See section <A HREF="bison_6.html#SEC45">3.5.3 Actions</A>.
<P>
</P>
<DT>`<SAMP>$<<VAR>typealt</VAR>>$</SAMP>'
<DD>Like <CODE>$$</CODE> but specifies alternative <VAR>typealt</VAR> in the union
specified by the <CODE>%union</CODE> declaration. See section <A HREF="bison_6.html#SEC46">Data Types of Values in Actions</A>.
<P>
</P>
<DT>`<SAMP>$<<VAR>typealt</VAR>><VAR>n</VAR></SAMP>'
<DD>Like <CODE>$<VAR>n</VAR></CODE> but specifies alternative <VAR>typealt</VAR> in the
union specified by the <CODE>%union</CODE> declaration.
See section <A HREF="bison_6.html#SEC46">Data Types of Values in Actions</A>.<P>
</P>
<DT>`<SAMP>YYABORT;</SAMP>'
<DD>Return immediately from <CODE>yyparse</CODE>, indicating failure.
See section <A HREF="bison_7.html#SEC59">The Parser Function <CODE>yyparse</CODE></A>.
<P>
</P>
<DT>`<SAMP>YYACCEPT;</SAMP>'
<DD>Return immediately from <CODE>yyparse</CODE>, indicating success.
See section <A HREF="bison_7.html#SEC59">The Parser Function <CODE>yyparse</CODE></A>.
<P>
</P>
<DT>`<SAMP>YYBACKUP (<VAR>token</VAR>, <VAR>value</VAR>);</SAMP>'
<DD><A NAME="IDX30"></A>
Unshift a token. This macro is allowed only for rules that reduce
a single value, and only when there is no look-ahead token.
It installs a look-ahead token with token type <VAR>token</VAR> and
semantic value <VAR>value</VAR>; then it discards the value that was
going to be reduced by this rule.
<P>
If the macro is used when it is not valid, such as when there is
a look-ahead token already, then it reports a syntax error with
a message `<SAMP>cannot back up</SAMP>' and performs ordinary error
recovery.
</P>
<P>
In either case, the rest of the action is not executed.
</P>
<P>
</P>
<DT>`<SAMP>YYEMPTY</SAMP>'
<DD><A NAME="IDX31"></A>
Value stored in <CODE>yychar</CODE> when there is no look-ahead token.
<P>
</P>
<DT>`<SAMP>YYERROR;</SAMP>'
<DD><A NAME="IDX32"></A>
Cause an immediate syntax error. This statement initiates error
recovery just as if the parser itself had detected an error; however, it
does not call <CODE>yyerror</CODE>, and does not print any message. If you
want to print an error message, call <CODE>yyerror</CODE> explicitly before
the `<SAMP>YYERROR;</SAMP>' statement. See section <A HREF="bison_9.html#SEC80">6. Error Recovery</A>.
<P>
</P>
<DT>`<SAMP>YYRECOVERING</SAMP>'
<DD>This macro stands for an expression that has the value 1 when the parser
is recovering from a syntax error, and 0 the rest of the time.
See section <A HREF="bison_9.html#SEC80">6. Error Recovery</A>.
<P>
</P>
<DT>`<SAMP>yychar</SAMP>'
<DD>Variable containing the current look-ahead token. (In a pure parser,
this is actually a local variable within <CODE>yyparse</CODE>.) When there is
no look-ahead token, the value <CODE>YYEMPTY</CODE> is stored in the variable.
See section <A HREF="bison_8.html#SEC68">Look-Ahead Tokens</A>.
<P>
</P>
<DT>`<SAMP>yyclearin;</SAMP>'
<DD>Discard the current look-ahead token. This is useful primarily in
error rules. See section <A HREF="bison_9.html#SEC80">6. Error Recovery</A>.
<P>
</P>
<DT>`<SAMP>yyerrok;</SAMP>'
<DD>Resume generating error messages immediately for subsequent syntax
errors. This is useful primarily in error rules.
See section <A HREF="bison_9.html#SEC80">6. Error Recovery</A>.
<P>
</P>
<DT>`<SAMP>@<VAR>n</VAR></SAMP>'
<DD><A NAME="IDX33"></A>
Acts like a structure variable containing information on the line
numbers and column numbers of the <VAR>n</VAR>th component of the current
rule. The structure has four members, like this:
<P>
<TABLE><tr><td> </td><td class=example><pre>struct {
int first_line, last_line;
int first_column, last_column;
};
</pre></td></tr></table><P>
Thus, to get the starting line number of the third component, you would
use `<SAMP>@3.first_line</SAMP>'.
</P>
<P>
In order for the members of this structure to contain valid information,
you must make <CODE>yylex</CODE> supply this information about each token.
If you need only certain members, then <CODE>yylex</CODE> need only fill in
those members.
</P>
<P>
The use of this feature makes the parser noticeably slower.
</DL>
<P>
<A NAME="Algorithm"></A>
<HR SIZE="6">
<TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0>
<TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_7.html#SEC58"> << </A>]</TD>
<TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="bison_8.html#SEC67"> >> </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>
|