[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
4.6.4 Calling Conventions for Pure Parsers
When you use the Bison declaration %define api.pure
to request a
pure, reentrant parser, the global communication variables yylval
and yylloc
cannot be used. (See section 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 section The Parser Function yyparse
).
- Directive: lex-param {argument-declaration}
-
Declare that the braced-code argument-declaration is an additional
yylex
argument declaration.
For instance:
%parse-param {int *nastiness} %lex-param {int *nastiness} %parse-param {int *randomness} |
results in the following signature:
int yylex (int *nastiness); int yyparse (int *nastiness, int *randomness); |
If %define api.pure
is added:
int yylex (YYSTYPE *lvalp, int *nastiness); int yyparse (int *nastiness, int *randomness); |
and finally, if both %define api.pure
and %locations
are used:
int yylex (YYSTYPE *lvalp, YYLTYPE *llocp, int *nastiness); int yyparse (int *nastiness, int *randomness); |