mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-09 12:23:04 +00:00
pstate_clear is lacking a prototype. Reported by Ryan https://lists.gnu.org/r/bug-bison/2020-05/msg00101.html Besides, none of the C examples were compiled with the warning flags. * configure.ac (warn_c): Add -Wmissing-prototypes. * data/skeletons/yacc.c (pstate_clear): Make it static. * examples/local.mk (TEST_CFLAGS): New. * examples/c/bistromathic/local.mk, examples/c/calc/local.mk, * examples/c/lexcalc/local.mk, examples/c/mfcalc/local.mk, * examples/c/pushcalc/local.mk, examples/c/reccalc/local.mk, * examples/c/rpcalc/local.mk: Use it. GCC's warn_unused_result is not silenced by a cast to void, so we have to "use" scanf's result. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 Flex generated code produces too many warnings, including things such as, with ICC: examples/c/lexcalc/scan.c(1088): error #1682: implicit conversion of a 64-bit integral type to a smaller integral type (potential portability problem) 2259 YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]), 2260 ^ 2261 2262 I am tired of trying to fix Flex's output. The project does not seem maintained. We ought to avoid it. So, for the time being, don't try to enable warnings with Flex. * examples/c/bistromathic/parse.y, examples/c/reccalc/scan.l: Fix warnings. * doc/bison.texi: Discard scanf's return value to defeat -Werror=unused-result.
116 lines
2.1 KiB
Plaintext
116 lines
2.1 KiB
Plaintext
%code top {
|
|
#include <ctype.h> /* isdigit. */
|
|
#include <stdio.h> /* printf. */
|
|
#include <stdlib.h> /* abort. */
|
|
#include <string.h> /* strcmp. */
|
|
}
|
|
|
|
%code {
|
|
int yylex (YYSTYPE *yylval);
|
|
void yyerror (char const *);
|
|
}
|
|
|
|
%define api.header.include {"calc.h"}
|
|
|
|
/* Generate YYSTYPE from the types used in %token and %type. */
|
|
%define api.value.type union
|
|
%token <double> NUM "number"
|
|
%type <double> expr term fact
|
|
|
|
/* Don't share global variables between the scanner and the parser. */
|
|
%define api.pure full
|
|
/* Generate a push parser. */
|
|
%define api.push-pull push
|
|
|
|
/* Nice error messages with details. */
|
|
%define parse.error detailed
|
|
|
|
/* Generate the parser description file (calc.output). */
|
|
%verbose
|
|
|
|
/* Enable run-time traces (yydebug). */
|
|
%define parse.trace
|
|
|
|
/* Formatting semantic values in debug traces. */
|
|
%printer { fprintf (yyo, "%g", $$); } <double>;
|
|
|
|
%% /* The grammar follows. */
|
|
input:
|
|
%empty
|
|
| input line
|
|
;
|
|
|
|
line:
|
|
'\n'
|
|
| expr '\n' { printf ("%.10g\n", $1); }
|
|
| error '\n' { yyerrok; }
|
|
;
|
|
|
|
expr:
|
|
expr '+' term { $$ = $1 + $3; }
|
|
| expr '-' term { $$ = $1 - $3; }
|
|
| term
|
|
;
|
|
|
|
term:
|
|
term '*' fact { $$ = $1 * $3; }
|
|
| term '/' fact { $$ = $1 / $3; }
|
|
| fact
|
|
;
|
|
|
|
fact:
|
|
"number"
|
|
| '(' expr ')' { $$ = $expr; }
|
|
;
|
|
|
|
%%
|
|
|
|
int
|
|
yylex (YYSTYPE *yylval)
|
|
{
|
|
int c;
|
|
|
|
/* Ignore white space, get first nonwhite character. */
|
|
while ((c = getchar ()) == ' ' || c == '\t')
|
|
continue;
|
|
|
|
if (c == EOF)
|
|
return 0;
|
|
|
|
/* Char starts a number => parse the number. */
|
|
if (c == '.' || isdigit (c))
|
|
{
|
|
ungetc (c, stdin);
|
|
if (scanf ("%lf", &yylval->NUM) != 1)
|
|
abort ();
|
|
return NUM;
|
|
}
|
|
|
|
/* Any other character is a token by itself. */
|
|
return c;
|
|
}
|
|
|
|
/* Called by yyparse on error. */
|
|
void
|
|
yyerror (char const *s)
|
|
{
|
|
fprintf (stderr, "%s\n", s);
|
|
}
|
|
|
|
int
|
|
main (int argc, char const* argv[])
|
|
{
|
|
/* Enable parse traces on option -p. */
|
|
for (int i = 1; i < argc; ++i)
|
|
if (!strcmp (argv[i], "-p"))
|
|
yydebug = 1;
|
|
int status;
|
|
yypstate *ps = yypstate_new ();
|
|
do {
|
|
YYSTYPE lval;
|
|
status = yypush_parse (ps, yylex (&lval), &lval);
|
|
} while (status == YYPUSH_MORE);
|
|
yypstate_delete (ps);
|
|
return status;
|
|
}
|