bench: fix support for pure parser

* etc/bench.pl.in (is_pure): New.
(generate_grammar_calc): Use code provides where needed.
Use is_pure to call yylex properly.
Coding style fixes.
This commit is contained in:
Akim Demaille
2020-08-05 06:48:56 +02:00
parent 0a5bfb4fda
commit 31d4ec28bd

View File

@@ -218,6 +218,27 @@ sub directives($@)
###################################################################### ######################################################################
=item C<is_pure (@directive)>
Whether api.pure is set.
=cut
sub is_pure (@)
{
my (@directive) = @_;
for my $dir (@directive)
{
if ($dir =~ /\A%define api.pure/)
{
return 1;
}
}
return 0;
}
######################################################################
=item C<generate_grammar_triangular ($base, $max, @directive)> =item C<generate_grammar_triangular ($base, $max, @directive)>
Create a large triangular grammar which looks like : Create a large triangular grammar which looks like :
@@ -389,18 +410,14 @@ sub generate_grammar_calc ($$@)
%define api.value.type union %define api.value.type union
$directives $directives
%{ %code provides {
static int power (int base, int exponent); static int power (int base, int exponent);
/* yyerror receives the location if: /* yyerror receives the location if:
- %location & %pure & %glr - %location & %pure & %glr
- %location & %pure & %yacc & %parse-param. */ - %location & %pure & %yacc & %parse-param. */
static void yyerror (const char *s); static void yyerror (const char *s);
#if YYPURE static int yylex (@{[is_pure (@directive) ? "YYSTYPE *yylvalp" : "void"]});
static int yylex (YYSTYPE* yylvalp); }
#else
static int yylex (void);
#endif
%}
/* Bison Declarations */ /* Bison Declarations */
%token %token
@@ -467,12 +484,7 @@ yyerror (const char *s)
} }
static int static int
#if YYPURE yylex (@{[is_pure (@directive) ? "YYSTYPE *yylvalp" : "void"]})
# define yylval (*yylvalp)
yylex (YYSTYPE* yylvalp)
#else
yylex (void)
#endif
{ {
int c; int c;
@@ -498,7 +510,7 @@ yylex (void)
case '5': case '6': case '7': case '8': case '9': case '5': case '6': case '7': case '8': case '9':
{ {
int nchars = 0; int nchars = 0;
int n = sscanf (input - 1, "%d%n", &yylval.NUM, &nchars); int n = sscanf (input - 1, "%d%n", &@{[is_pure (@directive) ? "yylvalp->" : "yylval."]}NUM, &nchars);
assert (n == 1); assert (n == 1);
input += nchars - 1; input += nchars - 1;
return NUM; return NUM;
@@ -506,7 +518,7 @@ yylex (void)
default: default:
yyerror ("error: invalid character"); yyerror ("error: invalid character");
return yylex (); return yylex (@{[is_pure (@directive) ? "yylvalp" : ""]});
} }
} }
EOF EOF
@@ -618,8 +630,8 @@ EOF
print $out <<'EOF'; print $out <<'EOF';
%token <std::string> TEXT %token <std::string> TEXT
%token <int> NUMBER %token <int> NUMBER
%printer { std::cerr << "Number: " << $$; } <int> %printer { yyo << "Number: " << $$; } <int>
%printer { std::cerr << "Text: " << $$; } <std::string> %printer { yyo << "Text: " << $$; } <std::string>
%type <std::string> text result %type <std::string> text result
%% %%
@@ -641,8 +653,8 @@ EOF
%union {int ival; std::string* sval;} %union {int ival; std::string* sval;}
%token <sval> TEXT %token <sval> TEXT
%token <ival> NUMBER %token <ival> NUMBER
%printer { std::cerr << "Number: " << $$; } <ival> %printer { yyo << "Number: " << $$; } <ival>
%printer { std::cerr << "Text: " << *$$; } <sval> %printer { yyo << "Text: " << *$$; } <sval>
%type <sval> text result %type <sval> text result
%% %%