manpagez: man pages & more
info bison
Home | html | info | man
[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.1 The Parser Function yyparse

You call the function yyparse 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 yyparse to return immediately without reading further.

Function: int yyparse (void)

The value returned by yyparse is 0 if parsing was successful (return is due to end-of-input).

The value is 1 if parsing failed because of invalid input, i.e., input that contains a syntax error or that causes YYABORT to be invoked.

The value is 2 if parsing failed due to memory exhaustion.

In an action, you can cause immediate return from yyparse by using these macros:

Macro: YYACCEPT

Return immediately with value 0 (to report success).

Macro: YYABORT

Return immediately with value 1 (to report failure).

If you use a reentrant parser, you can optionally pass additional parameter information to it in a reentrant way. To do so, use the declaration %parse-param:

Directive: %parse-param {argument-declaration}

Declare that an argument declared by the braced-code argument-declaration is an additional yyparse argument. The argument-declaration is used when declaring functions or prototypes. The last identifier in argument-declaration must be the argument name.

Here's an example. Write this in the parser:

 
%parse-param {int *nastiness}
%parse-param {int *randomness}

Then call the parser like this:

 
{
  int nastiness, randomness;
  …  /* Store proper data in nastiness and randomness.  */
  value = yyparse (&nastiness, &randomness);
  …
}

In the grammar actions, use expressions like this to refer to the data:

 
exp: …    { …; *randomness += 1; … }

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]
© manpagez.com 2000-2024
Individual documents may contain additional copyright information.