bench.pl: clean up the dust

* etc/bench.pl.in: Adjust to the current use of %define's values.
Don't use %error-verbose.
Prefer Bison to CPP (e.g., api.value.type).
Avoid returning characters directly, so that %define api.token.raw works.
This commit is contained in:
Akim Demaille
2020-02-23 14:39:43 +01:00
parent 296660304c
commit 6fe063da8f

View File

@@ -38,13 +38,13 @@ I<directives>:
| ( directives> ) -- Parentheses | ( directives> ) -- Parentheses
| %b PATH -- Use bison at PATH for this bench | %b PATH -- Use bison at PATH for this bench
| #d NAME[=VALUE] -- %code { #define NAME [VALUE] } | #d NAME[=VALUE] -- %code { #define NAME [VALUE] }
| %d NAME[=VALUE] -- %define NAME ["VALUE"] | %d NAME[=VALUE] -- %define NAME [VALUE]
| %s skeleton -- %skeleton "skeleton" | %s skeleton -- %skeleton "skeleton"
| directive | directive
Parentheses only group to override precedence. For instance: Parentheses only group to override precedence. For instance:
[ %debug ] & [ %error-verbose ] & [ %define variant ] [ %debug ] & [ %d parse.error=verbose ] & [ %define variant ]
will generate eight different cases. will generate eight different cases.
@@ -173,7 +173,7 @@ my $cxx = $ENV{'CXX'} || 'g++';
my $cflags = '-O2'; my $cflags = '-O2';
my @directive = (); my @directive = ();
my $grammar = 'calc'; my $grammar = 'calc';
my $iterations = -1; my $iterations = 100;
my $verbose = 1; my $verbose = 1;
=head1 FUNCTIONS =head1 FUNCTIONS
@@ -247,7 +247,7 @@ sub generate_grammar_triangular ($$@)
my $out = new IO::File ">$base.y" my $out = new IO::File ">$base.y"
or die; or die;
print $out <<EOF; print $out <<EOF;
%error-verbose %define parse.error detailed
%{ %{
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -380,20 +380,13 @@ sub generate_grammar_calc ($$@)
#include <ctype.h> #include <ctype.h>
#define USE(Var) #define USE(Var)
/* Exercise pre-prologue dependency to %union. */ static int global_result = 0;
typedef int semantic_value;
static semantic_value global_result = 0;
static int global_count = 0; static int global_count = 0;
%} %}
%define api.value.type union
%define parse.error detailed
$directives $directives
%error-verbose
/* Exercise %union. */
%union
{
semantic_value ival;
};
%{ %{
static int power (int base, int exponent); static int power (int base, int exponent);
@@ -409,15 +402,29 @@ static int yylex (void);
%} %}
/* Bison Declarations */ /* Bison Declarations */
%token CALC_EOF 0 "end of input" %token
%token <ival> NUM "number" PLUS "+"
%type <ival> exp MINUS "-"
STAR "*"
SLASH "/"
CARET "^"
LPAREN "("
RPAREN ")"
EQUAL "="
BANG "!"
EOL "end of line"
CALC_EOF 0 "end of input"
<int>
NUM "number"
%type <int> exp
%nonassoc '=' /* comparison */ %printer { fprintf (yyo, "%d", \$\$); } <int>
%left '-' '+'
%left '*' '/' %nonassoc "=" /* comparison */
%left "-" "+"
%left "*" "/"
%left NEG /* negation--unary minus */ %left NEG /* negation--unary minus */
%right '^' /* exponentiation */ %right "^" /* exponentiation */
/* Grammar follows */ /* Grammar follows */
%% %%
@@ -427,28 +434,28 @@ input:
; ;
line: line:
'\\n' EOL
| exp '\\n' { USE (\$1); } | exp EOL { USE (\$1); }
; ;
exp: exp:
NUM { \$\$ = \$1; } NUM { \$\$ = \$1; }
| exp '=' exp | exp "=" exp
{ {
if (\$1 != \$3) if (\$1 != \$3)
fprintf (stderr, "calc: error: %d != %d\\n", \$1, \$3); fprintf (stderr, "calc: error: %d != %d\\n", \$1, \$3);
\$\$ = \$1; \$\$ = \$1;
} }
| exp '+' exp { \$\$ = \$1 + \$3; } | exp "+" exp { \$\$ = \$1 + \$3; }
| exp '-' exp { \$\$ = \$1 - \$3; } | exp "-" exp { \$\$ = \$1 - \$3; }
| exp '*' exp { \$\$ = \$1 * \$3; } | exp "*" exp { \$\$ = \$1 * \$3; }
| exp '/' exp { \$\$ = \$1 / \$3; } | exp "/" exp { \$\$ = \$1 / \$3; }
| '-' exp %prec NEG { \$\$ = -\$2; } | "-" exp %prec NEG { \$\$ = -\$2; }
| exp '^' exp { \$\$ = power (\$1, \$3); } | exp "^" exp { \$\$ = power (\$1, \$3); }
| '(' exp ')' { \$\$ = \$2; } | "(" exp ")" { \$\$ = \$2; }
| '(' error ')' { \$\$ = 1111; } | "(" error ")" { \$\$ = 1111; }
| '!' { \$\$ = 0; YYERROR; } | "!" { \$\$ = 0; YYERROR; }
| '-' error { \$\$ = 0; YYERROR; } | "-" error { \$\$ = 0; YYERROR; }
; ;
%% %%
/* The input. */ /* The input. */
@@ -507,24 +514,33 @@ yylex (void)
{ {
int c; int c;
/* Skip white space. */
while ((c = get_char ()) == ' ' || c == '\t') while ((c = get_char ()) == ' ' || c == '\t')
continue; continue;
/* process numbers */ switch (c)
if (c == '.' || isdigit (c)) {
{ case '0': case '1': case '2': case '3': case '4':
unget_char ( c); case '5': case '6': case '7': case '8': case '9':
yylval.ival = read_signed_integer (); case '.':
unget_char (c);
yylval.NUM = read_signed_integer ();
return NUM; return NUM;
}
/* Return end-of-file. */ case '+': return PLUS;
if (c == EOF) case '-': return MINUS;
return CALC_EOF; case '*': return STAR;
case '/': return SLASH;
case '^': return CARET;
case '=': return EQUAL;
case '!': return BANG;
case '(': return LPAREN;
case ')': return RPAREN;
/* Return single chars. */ case '\\n': return EOL;
return c; case EOF: return CALC_EOF;
default: return c;
}
} }
static int static int
@@ -541,7 +557,7 @@ power (int base, int exponent)
int int
main (int argc, const char **argv) main (int argc, const char **argv)
{ {
semantic_value result = 0; int result = 0;
int count = 0; int count = 0;
int status; int status;
@@ -957,8 +973,8 @@ sub eat ($)
# Parse directive specifications: # Parse directive specifications:
# expr: term (| term)* # expr: term (| term)*
# term: fact (& fact)* # term: fact (& fact)*
# fact: ( expr ) | [ expr ] | dirs # fact: ( expr ) | [ expr ] | dir
# dirs: %s SKELETON | #d NAME[=VALUE] | %d NAME[=VALUE] | directive # dir: %s SKELETON | #d NAME[=VALUE] | %d NAME[=VALUE] | directive
sub parse (@) sub parse (@)
{ {
@token = (@_, $eod); @token = (@_, $eod);
@@ -1020,12 +1036,12 @@ sub parse_fact ()
} }
else else
{ {
@res = parse_dirs (); @res = parse_dir ();
} }
return @res; return @res;
} }
sub parse_dirs () sub parse_dir ()
{ {
my @res; my @res;
die "unexpected end of expression" die "unexpected end of expression"
@@ -1041,7 +1057,7 @@ sub parse_dirs ()
elsif ($token[0] eq '%d') elsif ($token[0] eq '%d')
{ {
shift @token; shift @token;
$token[0] =~ s/(.*?)=(.*)/$1 "$2"/; $token[0] =~ s/(.*?)=(.*)/$1 $2/;
@res = ("%define $token[0]"); @res = ("%define $token[0]");
shift @token; shift @token;
} }