yacc: use the most appropriate integral type for state numbers

Currently we properly use the "best" integral type for tables,
including those storing state numbers.  However the variables for
state numbers used in yyparse (and its dependencies such as
yy_stack_print) still use int16_t invariably.  As a consequence, very
large models overflow these variables.

Let's use the "best" type for these variables too.  It turns out that
we can still use 16 bits for twice larger automata: stick to unsigned
types.

However using 'unsigned' when 16 bits are not enough is troublesome
and generates tons of warnings about signedness issues.  Instead,
let's use 'int'.

Reported by Tom Kramer.
https://lists.gnu.org/archive/html/bug-bison/2019-09/msg00018.html

* data/skeletons/yacc.c (b4_state_num_type): New.
(yy_state_num): Be computed from YYNSTATES.
* tests/linear: New.
* tests/torture.at (State number type): New.
Use it.
This commit is contained in:
Akim Demaille
2019-09-28 13:48:35 +02:00
parent 871c02b327
commit 2ca6b71967
5 changed files with 160 additions and 25 deletions

View File

@@ -233,7 +233,7 @@ AT_DATA_HORIZONTAL_GRAMMAR([input.y], [1000])
# Ask for 200 MiB, which should be plenty even on a 64-bit host.
AT_INCREASE_DATA_SIZE(204000)
AT_BISON_CHECK_NO_XML([-v -o input.c input.y])
AT_BISON_CHECK_NO_XML([-o input.c input.y])
AT_COMPILE([input])
AT_PARSER_CHECK([input])
@@ -241,6 +241,43 @@ AT_CLEANUP
## ------------------- ##
## State number type. ##
## ------------------- ##
# AT_TEST(NUM-STATES, TYPE)
# -------------------------
# Check that automaton with NUM-STATES uses TYPE has state number type.
# Check that parser works.
m4_pushdef([AT_TEST],
[AT_SETUP([State number type: $1 states])
AT_BISON_OPTION_PUSHDEFS
AT_CHECK([ruby $abs_top_srcdir/tests/linear $1 >input.y || { echo "ruby does not work"; exit 77; }])
# Old versions of GCC reject large values given to #line.
AT_FULL_COMPILE([input], [], [], [], [--no-line])
AT_CHECK([grep 'define YYNSTATES *$1' input.c], [], [ignore])
AT_CHECK([grep 'typedef $2 yy_state_num' input.c], [], [ignore])
AT_PARSER_CHECK([input])
AT_BISON_OPTION_POPDEFS
AT_CLEANUP])
AT_TEST( [256], [yytype_uint8])
AT_TEST( [257], [yytype_uint16])
AT_TEST([65536], [yytype_uint16])
AT_TEST([65537], [int])
m4_popdef([AT_TEST])
## ------------------------ ##
## Many lookahead tokens. ##
## ------------------------ ##
# AT_DATA_LOOKAHEAD_TOKENS_GRAMMAR(FILE-NAME, SIZE)
# --------------------------------------------------
# Create FILE-NAME, containing a self checking parser for a grammar
@@ -340,11 +377,6 @@ mv stdout $1
AT_BISON_OPTION_POPDEFS
])
## ------------------------ ##
## Many lookahead tokens. ##
## ------------------------ ##
AT_SETUP([Many lookahead tokens])
AT_DATA_LOOKAHEAD_TOKENS_GRAMMAR([input.y], [1000])