Okay, let's break down the "A complete reentrant parser example" section from the GNU Bison Wikipedia page.
1. What is a Reentrant Parser?
First, it's crucial to understand what "reentrant" means in this context. A standard, non-reentrant Bison parser often uses global variables to store its state (like the current token's value yylval, location yylloc, or internal parsing stacks). This works fine if you only ever have one instance of the parser running at a time in your program.
However, if you want to:
- Parse multiple inputs simultaneously in different threads.
- Have multiple independent parsers running within the same program.
- Call the parser recursively (though less common).
...then using global variables becomes problematic because different threads or instances would interfere with each other's state.
A reentrant parser solves this. It does not rely on global variables for its operational state. Instead, all the necessary state information is passed as parameters to the parser function (yyparse) and the lexer function (yylex), typically bundled within a custom data structure or context object. This makes the parser thread-safe and allows multiple instances to run concurrently without conflict.
2. The Example: A Reentrant RPN Calculator
The Wikipedia example demonstrates how to create a reentrant parser for a simple Reverse Polish Notation (RPN) calculator using Bison. RPN avoids parentheses by placing operators after their operands (e.g., 3 4 + instead of 3 + 4).
3. Key Bison Directives for Reentrancy
The example uses specific Bison directives to achieve reentrancy:
%define api.pure full: This is the main directive that tells Bison to generate a "pure" (reentrant) parser. It changes the signature of the parser function yyparse and ensures internal state is not stored globally.
%lex-param {void *scanner}: This directive specifies that the lexer function (yylex) will accept an additional parameter. The type is void *, named scanner in this example. This scanner pointer is intended to point to a structure holding all the state needed by the lexer and potentially shared with the parser.
%parse-param {void *scanner}: Similarly, this tells Bison that the parser function (yyparse) will also accept the same void *scanner parameter. Bison ensures this parameter is automatically passed down to the yylex and yyerror functions when they are called from within yyparse.
4. Code Breakdown
Let's look at the different parts of the example code:
%code requires { ... }: This block includes C code (like the enum yytokentype definition) that needs to be visible in both the parser's C output file and potentially other files (like the lexer's file, if it were separate). The enum is standard when %token-table is implicitly or explicitly used.
- Bison Declarations (
%token, %type):
%token NUM: Declares a token type named NUM for numbers.
%type <ival> exp NUM: Associates the semantic value type int (assumed to be the ival field of a %union, though the %union declaration itself isn't shown in the snippet) with the non-terminal symbol exp (expression) and the terminal NUM. This means when the parser handles an exp or a NUM, it expects its associated value to be an integer.
- Grammar Rules (
%% ... %%): These define the structure of the RPN language:
input: /* empty */ | input line ;: An input can be empty or consist of previous input followed by a line.
line: '\n' | exp '\n' { printf ("\t%d\n", $1); };: A line is either just a newline or an expression followed by a newline. If it's an expression, its calculated value ($1) is printed.
exp: NUM { $$ = $1; }: An expression can be a single number. Its value ($$) is the value of the number ($1).
| exp exp '+' { $$ = $1 + $2; }: An expression can be two sub-expressions followed by '+'. The value is the sum of the first ($1) and second ($2) sub-expressions.
| exp exp '-' { $$ = $1 - $2; }: Defines subtraction.
| exp exp '*' { $$ = $1 * $2; }: Defines multiplication.
| exp exp '/' { $$ = $1 / $2; }: Defines division.
- Error Handling (
yyerror function):
void yyerror (void *scanner, const char *s): Notice the function signature includes the void *scanner parameter, as required for a reentrant parser. It uses yyget_lloc(scanner) to get the location information (which also requires the scanner context) and prints an error message.
- Lexer ( function):
5. Summary of Reentrancy in the Example
The example demonstrates reentrancy by:
- Using
%define api.pure full to generate functions without reliance on global parser state.
- Using
%lex-param and %parse-param to add a void *scanner argument to yyparse, yylex, and yyerror.
- Passing this
scanner context during the yyparse call. Bison automatically propagates this context to yylex and yyerror calls made internally.
- Accessing location data via
yyget_lloc(scanner).
- Having
yylex receive the semantic value location via the YYSTYPE * lvalp parameter instead of using a global yylval.
This setup ensures that all state specific to a particular parsing session is contained within or accessible via the scanner object, making the parser safe to use in multiple threads or instances simultaneously.