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