Previous: Token Locations, Up: Lexical   [Contents][Index]


4.6.4 Calling Conventions for Pure Parsers

When you use the Bison declaration %define api.pure full to request a pure, reentrant parser, the global communication variables yylval and yylloc cannot be used. (See A Pure (Reentrant) Parser.) In such parsers the two global variables are replaced by pointers passed as arguments to yylex. You must declare them as shown here, and pass the information back by storing it through those pointers.

int
yylex (YYSTYPE *lvalp, YYLTYPE *llocp)
{
  …
  *lvalp = value;  /* Put value onto Bison stack.  */
  return INT;      /* Return the type of the token.  */
  …
}

If the grammar file does not use the ‘@’ constructs to refer to textual locations, then the type YYLTYPE will not be defined. In this case, omit the second argument; yylex will be called with only one argument.

If you wish to pass the additional parameter data to yylex, use %lex-param just like %parse-param (see Parser Function).

Directive: lex-param {argument-declaration}

Declare that the braced-code argument-declaration is an additional yylex argument declaration.

For instance:

%lex-param   {int *nastiness}

results in the following signature:

int yylex (int *nastiness);

If %define api.pure full (or just %define api.pure) is added:

int yylex (YYSTYPE *lvalp, int *nastiness);