Manual
Manual
Manual
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Chapter 4: Parser C-Language Interface 63<br />
4 Parser C-Language Interface<br />
The Bison parser is actually a C function named yyparse. Here we describe the interface<br />
conventions of yyparse and the other functions that it needs to use.<br />
Keep in mind that the parser uses many C identifiers starting with ‘yy’ and ‘YY’ for<br />
internal purposes. If you use such an identifier (aside from those in this manual) in an<br />
action or in epilogue in the grammar file, you are likely to run into trouble.<br />
4.1 The Parser Function yyparse<br />
You call the function yyparse to cause parsing to occur. This function reads tokens,<br />
executes actions, and ultimately returns when it encounters end-of-input or an unrecoverable<br />
syntax error. You can also write an action which directs yyparse to return immediately<br />
without reading further.<br />
int yyparse (void)<br />
[Function]<br />
The value returned by yyparse is 0 if parsing was successful (return is due to end-ofinput).<br />
The value is 1 if parsing failed because of invalid input, i.e., input that contains a<br />
syntax error or that causes YYABORT to be invoked.<br />
The value is 2 if parsing failed due to memory exhaustion.<br />
In an action, you can cause immediate return from yyparse by using these macros:<br />
YYACCEPT<br />
Return immediately with value 0 (to report success).<br />
YYABORT<br />
Return immediately with value 1 (to report failure).<br />
[Macro]<br />
[Macro]<br />
If you use a reentrant parser, you can optionally pass additional parameter information<br />
to it in a reentrant way. To do so, use the declaration %parse-param:<br />
%parse-param {argument-declaration}<br />
[Directive]<br />
Declare that an argument declared by the braced-code argument-declaration is an additional<br />
yyparse argument. The argument-declaration is used when declaring functions<br />
or prototypes. The last identifier in argument-declaration must be the argument<br />
name.<br />
Here’s an example. Write this in the parser:<br />
%parse-param {int *nastiness}<br />
%parse-param {int *randomness}<br />
Then call the parser like this:<br />
{<br />
int nastiness, randomness;<br />
... /* Store proper data in nastiness and randomness. */<br />
value = yyparse (&nastiness, &randomness);<br />
...<br />
}<br />
In the grammar actions, use expressions like this to refer to the data: