Commit Graph

56 Commits

Author SHA1 Message Date
Akim Demaille
a1da9b9fe8 yacc.c: fix misleading indentation
* data/skeletons/yacc.c: here.
2020-01-29 07:15:19 +01:00
Akim Demaille
16c77b4ba2 yacc.c: fixes
* data/skeletons/yacc.c: Avoid warnings about unused functions.
Fix typo.
2020-01-22 22:31:07 +01:00
Akim Demaille
9096955fba parsers: support translatable token aliases
In addition to

    %token NUM "number"

accept

    %token NUM _("number")

in which case the token will be translated in error messages.
Do not use _() in the output if there are no translatable tokens.

* src/symtab.h, src/symtab.c (symbol): Add a 'translatable' member.
* src/parse-gram.y (TSTRING): New token.
(string_as_id.opt): Replace with...
(alias): this.
Use it.
* src/scan-gram.l (SC_ESCAPED_TSTRING): New start conditions, to match
TSTRINGs.
* src/output.c (prepare_symbols): Define b4_translatable if there are
translatable strings.

* data/skeletons/glr.c, data/skeletons/lalr1.cc,
* data/skeletons/yacc.c (yytnamerr): Receive b4_translatable, and use it.
2020-01-19 21:23:11 +01:00
Akim Demaille
f443673450 yacc.c: add support for parse.error detailed
"detailed" error messages are almost like "verbose", except that we
don't double escape them, they don't get inner quotes, we don't use
yytnamerr, and we hide the table.

"custom" is exposed with the "detailed" tokens, not the "verbose"
ones: they are not double-quoted.

Because there's a risk that some people use yytname even without
"verbose", let's keep yytname (instead of yys_name) in "simple"
parse.error.

* src/output.c (prepare_symbol_names): Be ready to output symbol names
unquoted.
(prepare_symbol_names): Output both the old tname table, and the new
symbol_names one.
* data/skeletons/bison.m4: Accept 'detailed'.
* data/skeletons/yacc.c: When parse.error is 'detailed', don't emit
yytname and yytnamerr, just yysymbol_name with the table inside.
* tests/calc.at: Adjust.
2020-01-19 14:51:14 +01:00
Akim Demaille
e1197fcc3d yacc.c: portability to G++ 4.8
Currently we get warnings with GCC 4.8 when running the
maintainer-check-g++ tests:

    143. skeletons.at:85: testing Installed skeleton file names ...
    ../../tests/skeletons.at:120: COLUMNS=1000; export COLUMNS;  bison --color=no -fno-caret --skeleton=yacc.c -o input-cmd-line.c input-cmd-line.y
    ../../tests/skeletons.at:121: $CC $CFLAGS $CPPFLAGS  $LDFLAGS -o input-cmd-line input-cmd-line.c $LIBS
    stderr:
    input-cmd-line.c: In function 'int yysyntax_error(long int*, char**, const yyparse_context_t*)':
    input-cmd-line.c:977:52: error: conversion to 'int' from 'long int' may alter its value [-Werror=conversion]
                                       YYSIZEOF (yyarg) / YYSIZEOF (*yyarg));
                                                        ^
    cc1plus: all warnings being treated as errors
    stdout:
    ../../tests/skeletons.at:121: exit code was 1, expected 0

and

    429. calc.at:823: testing Calculator parse.error=custom %locations api.prefix={calc}  ...
    ../../tests/calc.at:823: COLUMNS=1000; export COLUMNS;  bison --color=no -fno-caret -Wno-deprecated -o calc.c calc.y
    ../../tests/calc.at:823: $CC $CFLAGS $CPPFLAGS  $LDFLAGS -o calc calc.c $LIBS
    stderr:
    calc.y: In function 'int yyreport_syntax_error(const yyparse_context_t*)':
    calc.y:157:58: error: conversion to 'int' from 'long unsigned int' may alter its value [-Werror=conversion]
       int n = yysyntax_error_arguments (ctx, arg, sizeof arg / sizeof *arg);
                                                              ^
    cc1plus: all warnings being treated as errors
    stdout:
    ../../tests/calc.at:823: exit code was 1, expected 0

We could use a cast to avoid the warning, but it becomes too
cluttered.  We can also use YYPTRDIFF_T, but that forces the user to
use YYPTRDIFF_T too, although this is an array of tokens, which is
limited by YYNTOKENS, an int.  So let's completely avoid this warning.

* data/skeletons/yacc.c, tests/local.at (yyreport_syntax_error): Avoid
relying on sizeof to compute the array capacity.
2020-01-17 06:49:59 +01:00
Akim Demaille
1854429e40 yacc.c: pass the parse-params to yyreport_syntax_error
Enhance the calculator tests: show that passing arguments to yyerror
works.

* tests/calc.at: Add a new parse-param, nerrs, which counts the number
of syntax errors in a run.
* tests/local.at: Adjust to handle the new 'nerrs' argument, when
present.

The custom error reporting function show sees the user's additional
arguments.  Let's experiment with passing them as arguments to
yyreport_syntax_error, but maybe storing them in the context would be
a bettter alternative.

* data/skeletons/yacc.c (yyreport_syntax_error): Handle the
parse-params.
* tests/calc.at, tests/local.at: Adjust.
2020-01-17 06:49:59 +01:00
Akim Demaille
fd33cc9c8b yacc.c: let custom error messages see the location
* data/skeletons/yacc.c (yyparse_context_t): Add yylloc when
applicable.
(yyparse_context_location): New.
* tests/local.at (AT_YYERROR_DEFINE(c)): Handle the location.
* tests/calc.at: Check it.
2020-01-17 06:49:59 +01:00
Akim Demaille
8637438cee yacc.c: isolate yyexpected_tokens
Provide users with a means to query for the currently allowed tokens.
Could be used for autocompletion for instance.

* data/skeletons/yacc.c (yyexpected_tokens): New, extracted from
yysyntax_error_arguments.
* examples/c/calc/calc.y (PRINT_EXPECTED_TOKENS): New.
Use it.
2020-01-17 06:49:59 +01:00
Akim Demaille
cda1934606 yacc.c: add custom error message generation
When parse.error is custom, let users define a yyreport_syntax_error
function, and use it.

* data/skeletons/bison.m4 (b4_error_verbose_if): Accept 'custom'.
* data/skeletons/yacc.c: Implement it.
* examples/c/calc/calc.y: Experiment with it.
2020-01-17 06:49:59 +01:00
Akim Demaille
5b883180e6 yacc.c: style: avoid macros
* data/skeletons/yacc.c (YYSYNTAX_ERROR): Remove, the call is now
sufficiently small so that we can afford to duplicate it.
2020-01-17 06:49:59 +01:00
Akim Demaille
135d0336d5 yacc.c: store token numbers, not token strings
That allows users to cover more cases, such as easily filtering some
arguments they don't want to expose.  But they now have to call
yysymbol_name explicitly.

* data/skeletons/yacc.c (yysyntax_error_arguments, yysyntax_error):
Deal with symbol numbers instead of symbol names.
2020-01-17 06:49:59 +01:00
Akim Demaille
cc6c647f00 yacc.c: extract yyerror_message_arguments
Isolate a function that returns the list of expected and unexpected
tokens.  It will be exposed to users willing to customize their error
messages.

* data/skeletons/yacc.c (yyparse_context_t): New.
(yyerror_message_arguments): New, extracted from yysyntax_error.
2020-01-17 06:49:59 +01:00
Akim Demaille
1c0adb410d yacc.c: comment changes
In particular, import Adrian Vogelsgesang's comments about LAC from
lalr1.cc.

* data/skeletons/yacc.c: here.
2020-01-11 18:01:39 +01:00
Akim Demaille
32b529f038 yacc.c: style: double-quote the argument of b4_percent_define_get
* data/skeletons/yacc.c: Here, for consistency.
2020-01-11 17:35:24 +01:00
Akim Demaille
46cce832fd yacc.c: introduce yysymbol_name
Provide the users with a public API to get the name of the tokens.  A
thin wrapper around yytname.

* data/skeletons/yacc.c (yysymbol_name): New.
Use it.
2020-01-11 16:14:06 +01:00
Akim Demaille
2116af766a yacc.c: simplify use of YYDPRINTF
* data/skeletons/yacc.c (YYDPRINTF): Expand to no-op (instead of
nothing) when disabled.
Simplify callers.
2020-01-09 09:02:55 +01:00
Akim Demaille
8036635251 package: bump copyrights to 2020
Run 'make update-copyright'.
2020-01-05 10:26:35 +01:00
Akim Demaille
7122d747cf yacc.c: no longer support YYERROR_VERBOSE
Supporting YYERROR_VERBOSE via cpp is a nuisance: m4 is in charge of
handling alternatives.  When adding more options for %define
parse.error, supporting both CPP and M4 is too complex.  Anyway,
YYERROR_VERBOSE was deprecated long ago.

* data/skeletons/yacc.c: Use m4 only to handle verbose/simple error
messages.
2020-01-04 09:12:43 +01:00
Akim Demaille
69fe4b9eb6 yacc.c: avoid negations
* data/skeletons/yacc.c (yyerrlab): here.
2020-01-03 09:07:40 +01:00
Akim Demaille
f983d00e77 yacc: restructure and fix yysyntax_error
I would like to offer new ways to build the error message.  As a first
step, let's simplify yysyntax_error whose first loop does two things
at the same time: (i) collect the tokens to be reported in the error
message, and (ii) accumulate their sizes and possibly return
"overflow".  Let's pull (ii) in a second step.

Then test 525 (regression.at:1193: parse.error=verbose overflow)
failed.  This test checks that we correctly report "memory overflow"
when the error message is too large.  However the test is mistaken: it
is triggered in a place where there are five (large) expected tokens,
so anyway we would not display them, so there is no (memory) overflow
here!  Transform this test to (i) check that indeed there is no
overflow, and (ii) create syntax_error3 which does check the intended
behavior, but with four expected tokens.

* data/skeletons/yacc.c (yysyntax_error): First compute the list of
arguments, then compute yysize.
* tests/regression.at (parse.error=verbose overflow): Enhance and fix.
2019-12-31 12:00:04 +01:00
Paul Eggert
139d065594 warnings: pacify ‘gcc -Wchar-subscripts’ in yacc.c
Problem reported by Andy Fiddaman in:
https://lists.gnu.org/r/bug-bison/2019-12/msg00021.html
* data/skeletons/yacc.c (yy_reduce_print, yy_lac, yysyntax_error)
(yyreturn): If I might be a char, write a[+I] instead of a[I],
so that ‘gcc -Wchar-subscripts’ does not complain.
2019-12-18 13:35:28 -08:00
Akim Demaille
f8d82ff039 warnings: enable -Wuseless-cast, and eliminate warnings
Prompted by Frank Heckenbach.
https://lists.gnu.org/archive/html/bug-bison/2019-11/msg00016.html.

* configure.ac (warn_cxx): Add -Wuseless-cast.
* data/skeletons/c.m4 (b4_attribute_define): Define
YY_IGNORE_USELESS_CAST_BEGIN and YY_IGNORE_USELESS_CAST_END.
* data/skeletons/glr.c (YY_FPRINTF): New, replaces YYFPRINTF, wrapped
with YY_IGNORE_USELESS_CAST_BEGIN and YY_IGNORE_USELESS_CAST_END.
(YY_DPRINTF): Likewise.
* tests/actions.at: Remove useless cast.
* tests/headers.at: Adjust.
2019-12-06 08:27:55 +01:00
Akim Demaille
2f7097d1b1 yacc.c, glr.c: fix crash when reporting errors in consistent states
The current code for yysyntax_error for %define parse.error verbose is
fishy (given that YYEMPTY is -2, invalid argument for yytname[]):

    static int
    yysyntax_error ([...])
    {
      YYPTRDIFF_T yysize0 = yytnamerr (YY_NULLPTR, yytname[yytoken]);
    [...]
      if (yytoken != YYEMPTY)

A nearby comment reports

    The only way there can be no lookahead present (in yychar) is if
    this state is a consistent state with a default action.  Thus,
    detecting the absence of a lookahead is sufficient to determine
    that there is no unexpected or expected token to report.  In that
    case, just report a simple "syntax error".

So it _is_ possible to call yysyntax_error with yytoken == YYEMPTY,
albeit quite difficult when meaning to, so virtually impossible by
accident (after all, there was never a bug report about this).

I failed to produce a test case, but Joel E. Denny provided me with
one (added to the test suite below).  The yacc.c skeleton fails on
this, and once fixed dies on a second problem.  The glr.c skeleton was
also dying, but immediately of this second problem.

Indeed we were not allocating space for the error message's final \0.
This was hidden by the fact that we only had error messages with at
least an unexpected token displayed, so with at least one "%s" in the
format string, whose size (2) was included (incorrectly) in the final
size of the message (where the %s have been replaced by the actual
content).

* data/skeletons/glr.c, data/skeletons/yacc.c (yysyntax_error):
Do not invoke yytnamerr on YYEMPTY.
Clarify the computation of the length of the _final_ error message,
with the NUL terminator but without the '%s's.
* tests/conflicts.at (Syntax error in consistent error state):
New, contributed by Joel E. Denny.
2019-11-29 18:21:43 +01:00
Akim Demaille
ad32ec64c8 style: pacify syntax-check
* cfg.mk: No need to translate *.md files.
* data/skeletons/glr.c, data/skeletons/yacc.c: Fix space issues.
2019-11-20 07:10:27 +01:00
Akim Demaille
3398b0fa90 c++: fix old cast warnings
We still have a few old C casts in lalr1.cc, let's get rid of them.
Reported by Frank Heckenbach.

Actually, let's monitor all our casts using easy to grep macros.
Let's use these macros to use the C++ standard casts when we are in
C++.

* data/skeletons/c.m4 (b4_cast_define): New.
* data/skeletons/glr.c, data/skeletons/glr.cc,
* data/skeletons/lalr1.cc, data/skeletons/stack.hh,
* data/skeletons/yacc.c:
Use it and/or its casts.

* tests/actions.at, tests/cxx-type.at,
* tests/glr-regression.at, tests/headers.at, tests/torture.at,
* tests/types.at:
Use YY_CAST instead of C casts.

* configure.ac (warn_cxx): Add -Wold-style-cast.
* doc/bison.texi: Disable it.
2019-11-02 16:40:50 +01:00
kaneko y
c86b7815fc yacc.c: fix a typo
* data/skeletons/yacc.c (yysetstate): fix comment.
2019-10-22 19:05:02 +02:00
Akim Demaille
e5cbac98b6 yacc: rename types for states
* data/skeletons/yacc.c (yy_state_num): Rename as...
(yy_state_t): this.
(yy_state_fast_t): New.
Use it.
2019-10-15 07:02:26 +02:00
Akim Demaille
a428a9fa6c yacc: style changes
* data/skeletons/yacc.c: Move call to lac discard to clarify the
shifting of the token.
Like in lalr1.cc.
2019-10-15 07:02:26 +02:00
Paul Eggert
d4b6c86c7f Move the integer-type selection into c.m4
That way, glr.c can use it too.
* data/skeletons/c.m4 (b4_int_type):
Do not special-case ‘char’; it’s not worth the trouble,
as clang complains about char subscripts.
(b4_c99_int_type, b4_c99_int_type_define): New macros,
taken from yacc.c.
* data/skeletons/glr.c: Use b4_int_type_define.
* data/skeletons/yacc.c (b4_int_type): Remove, since there’s
no longer any need to redefine it.
Use b4_c99_int_type_define rather than its body.
2019-10-07 00:08:19 -07:00
Paul Eggert
5463291a91 Use “least” types for integers in Yacc tables
This changes the Yacc skeleton to use “least” integer types to
keep tables smaller on some platforms, which should lessen cache
pressure.  Since Bison uses the Yacc skeleton, it follows suit.
* data/skeletons/yacc.c: Include limits.h and stdint.h if this
seems to be needed.
(yytype_uint8, yytype_int8, yytype_uint16, yytype_int16):
If available, use GCC predefined macros __INT_MAX__ etc. to select
a “least” type, as this avoids namespace hassles.  Otherwise, if
available fall back on selecting a “least” type via the C99 macros
INT_MAX, INT_LEAST8_MAX, etc.  Otherwise, fall further back on one of
the builtin C99 types signed char, short, and int.  Make sure that
any selected type promotes to int.  Ignore any macros YYTYPE_INT16,
YYTYPE_INT8, YYTYPE_UINT16, YYTYPE_UINT8 defined by the user.
(ptrdiff_t, PTRDIFF_MAX): Simplify in the light of the above.
(yytype_uint8, yytype_uint16): Do not assume that unsigned char
and unsigned short promote to int, as this isn’t true on some
platforms (e.g., TI TMS320C55x).
* src/parse-gram.y (YYTYPE_INT16, YYTYPE_INT8, YYTYPE_UINT16)
(YYTYPE_UINT8): Remove, as these are no longer effective.
2019-10-07 00:08:19 -07:00
Paul Eggert
6373b90fc8 Port better to C++ platforms
* data/skeletons/yacc.c (YYPTRDIFF_T, YYPTRDIFF_MAXIMUM):
Default to long, not int.
(yy_lac_stack_realloc, yy_lac, yytnamerr, yyparse):
Avoid casts to YYPTRDIFF_T that were masking the problem.
2019-10-06 11:59:16 -07:00
Paul Eggert
beceb2fa93 Work around GCC 4.8 false alarms without casts
* data/skeletons/yacc.c (yyparse):
Initialize yyes_capacity with a signed expression.
* tests/local.at (AT_YYLEX_DEFINE(c)):
Use enum to avoid cast.
2019-10-06 11:59:16 -07:00
Akim Demaille
32e5a91a91 yacc.c: work around warnings from G++ 4.8
input.c: In function 'int yyparse()':
input.c: error: conversion to 'long int' from 'long unsigned int'
                may change the sign of the result [-Werror=sign-conversion]
   yyes_capacity = sizeof yyesa / sizeof *yyes;
                                ^
cc1plus: all warnings being treated as errors

* data/skeletons/yacc.c: here.
2019-10-06 08:07:40 +02:00
Akim Demaille
5973d763c0 yacc.c: work around warnings from Clang++ 3.3 and 3.4
When we run the test suite with these C++ compilers to compile C code,
we get:

    239. synclines.at:440: testing syncline escapes: yacc.c ...
    ../../tests/synclines.at:440: $CC $CFLAGS $CPPFLAGS \"\\\"\".c -o \"\\\"\" ||
              exit 77
    stderr:
    stdout:
    ../../tests/synclines.at:440: COLUMNS=1000; export COLUMNS;  bison --color=no -fno-caret  -o \"\\\"\".c \"\\\"\".y
    ../../tests/synclines.at:440: $CC $CFLAGS $CPPFLAGS  $LDFLAGS -o \"\\\"\" \"\\\"\".c $LIBS
    stderr:
    "\"".c:1102:41: error: implicit conversion loses integer precision: 'long' to 'int' [-Werror,-Wshorten-64-to-32]
          YYPTRDIFF_T yysize = yyssp - yyss + 1;
                      ~~~~~~   ~~~~~~~~~~~~~^~~
    1 error generated.

    193. conflicts.at:545: testing parse.error=verbose and consistent errors: lr.type=canonical-lr parse.lac=full ...
    input.c:737:75: error: implicit conversion loses integer precision: 'long' to 'int'
                           [-Werror,-Wshorten-64-to-32]
      YYPTRDIFF_T yysize_old = *yytop == yytop_empty ? 0 : *yytop - *yybottom + 1;
                  ~~~~~~~~~~                               ~~~~~~~~~~~~~~~~~~~^~~
    input.c:901:48: error: implicit conversion loses integer precision: 'long' to 'int'
                           [-Werror,-Wshorten-64-to-32]
                YYPTRDIFF_T yysize = yyesp - *yyes + 1;
                            ~~~~~~   ~~~~~~~~~~~~~~^~~

* data/skeletons/yacc.c: Add more casts.
2019-10-06 08:03:43 +02:00
Akim Demaille
5709f94a91 yacc.c: use casts instead of pragmas when losing integer width
For instance with Clang 4, 8, etc.:

    input.c:1166:12: error: implicit conversion loses integer precision: 'int' to 'yy_state_num' (aka 'signed char') [-Werror,-Wconversion]
      *yyssp = yystate;
             ~ ^~~~~~~

And GCC 8:

    input.c:1166:12: error: implicit conversion loses integer precision: 'int' to 'yy_state_num' (aka 'signed char') [-Werror,-Wimplicit-int-conversion]
      *yyssp = yystate;
             ~ ^~~~~~~

* data/skeletons/yacc.c (YY_CONVERT_INT_BEGIN): Remove.
Adjust callers.
2019-10-05 09:01:56 +02:00
Akim Demaille
bc96b757ca yacc.c: fix warnings about undefined macros
For instance with GCC 4.9 and --enable-gcc-warnings:

    25. input.at:1201: testing Torturing the Scanner ...
    ../../tests/input.at:1344: $CC $CFLAGS $CPPFLAGS  -c -o input.o input.c
    stderr:
    input.c:239:18: error: "__STDC_VERSION__" is not defined [-Werror=undef]
     # elif 199901 <= __STDC_VERSION__
                      ^
    input.c:256:18: error: "__STDC_VERSION__" is not defined [-Werror=undef]
     # elif 199901 <= __STDC_VERSION__
                      ^

* data/skeletons/yacc.c: Check that __STDC_VERSION__ is defined before
using it.
2019-10-04 06:58:44 +02:00
Paul Eggert
133edcd248 Prefer signed to unsigned integers
This patch contains more fixes to prefer signed to unsigned
integer types, as modern tools like 'gcc -fsanitize=undefined'
can check for signed integer overflow but not unsigned overflow.
* NEWS: Document the API change.
* boostrap.conf (gnulib_modules): Add intprops.
* data/skeletons/glr.c: Include stddef.h and stdint.h,
since this skeleton can assume C99 or later.
(YYSIZEMAX): Now signed, and the minimum of SIZE_MAX and PTRDIFF_MAX.
(yybool) [!__cplusplus]: Now signed (which is how bool behaves).
(YYTRANSLATE): Avoid use of unsigned, and make the macro
safe even for values greater than UINT_MAX.
(yytnamerr, struct yyGLRState, struct yyGLRStateSet, struct yyGLRStack)
(yyaddDeferredAction, yyinitStateSet, yyinitGLRStack)
(yyexpandGLRStack, yymarkStackDeleted, yyremoveDeletes)
(yyglrShift, yyglrShiftDefer, yy_reduce_print, yydoAction)
(yyglrReduce, yysplitStack, yyreportTree, yycompressStack)
(yyprocessOneStack, yyreportSyntaxError, yyrecoverSyntaxError)
(yyparse, yy_yypstack, yypstack, yypdumpstack):
* tests/input.at (Torturing the Scanner):
Prefer ptrdiff_t to size_t.
* data/skeletons/c++.m4 (b4_yytranslate_define):
* src/AnnotationList.c (AnnotationList__computePredecessorAnnotations):
* src/AnnotationList.h (AnnotationIndex):
* src/InadequacyList.h (InadequacyListNodeCount):
* src/closure.c (closure_new):
* src/complain.c (error_message, complains, complain_indent)
(complain_args, duplicate_directive, duplicate_rule_directive):
* src/gram.c (nritems, ritem_print, grammar_dump):
* src/ielr.c (ielr_compute_ritem_sees_lookahead_set)
(ielr_item_has_lookahead, ielr_compute_annotation_lists)
(ielr_compute_lookaheads):
* src/location.c (columns, boundary_print, location_print):
* src/muscle-tab.c (muscle_percent_define_insert)
(muscle_percent_define_check_values):
* src/output.c (prepare_rules, prepare_actions):
* src/parse-gram.y (id, handle_require):
* src/reader.c (record_merge_function_type, packgram):
* src/reduce.c (nuseless_productions, nuseless_nonterminals)
(inaccessable_symbols):
* src/relation.c (relation_print):
* src/scan-code.l (variant, variant_table_size, variant_count)
(variant_add, get_at_spec, show_sub_message, show_sub_messages)
(parse_ref):
* src/scan-gram.l (<SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>)
(scan_integer, convert_ucn_to_byte, handle_syncline):
* src/scan-skel.l (at_complain):
* src/symtab.c (complain_symbol_redeclared)
(complain_semantic_type_redeclared, complain_class_redeclared)
(symbol_class_set, complain_user_token_number_redeclared):
* src/tables.c (conflict_tos, conflrow, conflict_table)
(conflict_list, save_row, pack_vector):
* tests/local.at (AT_YYLEX_DEFINE(c)):
Prefer signed to unsigned integer.
* data/skeletons/lalr1.cc (yy_lac_check_):
* tests/actions.at (_AT_CHECK_PRINTER_AND_DESTRUCTOR):
* tests/local.at (AT_YYLEX_DEFINE(c)):
Omit now-unnecessary casts.
* data/skeletons/location.cc (b4_location_define):
* doc/bison.texi (Mfcalc Lexer, C++ position, C++ location):
Prefer int to unsigned for line and column numbers.
Change example to abort explicitly on memory exhaustion,
and fix an off-by-one bug that led to undefined behavior.
* data/skeletons/stack.hh (stack::operator[]):
Also allow ptrdiff_t indexes.
(stack::pop, slice::slice, slice::operator[]):
Index arg is now ptrdiff_t, not int.
(stack::ssize): New method.
(slice::range_): Now ptrdiff_t, not int.
* data/skeletons/yacc.c (b4_state_num_type): Remove.
All uses replaced by b4_int_type.
(YY_CONVERT_INT_BEGIN, YY_CONVERT_INT_END): New macros.
(yylac, yyparse): Use them around conversions that -Wconversion
would give false alarms about. 	Omit unnecessary casts.
(yy_stack_print): Use int rather than unsigned, and omit
a cast that doesn’t seem to be needed here any more.
* examples/c++/variant.yy (yylex):
* examples/c++/variant-11.yy (yylex):
Omit no-longer-needed conversions to unsigned.
* src/InadequacyList.c (InadequacyList__new_conflict):
Don’t assume *node_count is unsigned.
* src/output.c (muscle_insert_unsigned_table):
Remove; no longer used.
2019-10-02 17:11:33 -07:00
Paul Eggert
4d9ff272cf Prefer signed types for indexes in skeletons
* NEWS: Mention this.
* data/skeletons/c.m4 (b4_int_type):
Prefer char if it will do, and prefer signed types to unsigned if
either will do.
* data/skeletons/glr.c (yy_reduce_print): No need to
convert rule line to unsigned long.
(yyrecoverSyntaxError): Put action into an int to
avoid GCC warning of using a char subscript.
* data/skeletons/lalr1.cc (yy_lac_check_, yysyntax_error_):
Prefer ptrdiff_t to size_t.
* data/skeletons/yacc.c (b4_int_type):
Prefer signed types to unsigned if either will do.
* data/skeletons/yacc.c (b4_declare_parser_state_variables):
(YYSTACK_RELOCATE, YYCOPY, yy_lac_stack_realloc, yy_lac)
(yytnamerr, yysyntax_error, yyparse): Prefer ptrdiff_t to size_t.
(YYPTRDIFF_T, YYPTRDIFF_MAXIMUM): New macros.
(YYSIZE_T): Fix "! defined YYSIZE_T" typo.
(YYSIZE_MAXIMUM): Take the minimum of PTRDIFF_MAX and SIZE_MAX.
(YYSIZEOF): New macro.
(YYSTACK_GAP_MAXIMUM, YYSTACK_BYTES, YYSTACK_RELOCATE)
(yy_lac_stack_realloc, yyparse): Use it.
(YYCOPY, yy_lac_stack_realloc): Cast to YYSIZE_T to pacify GCC.
(yy_reduce_print): Use int instead of unsigned long when int
will do.
(yy_lac_stack_realloc): Prefer long to unsigned long when
either will do.
* tests/regression.at: Adjust to these changes.
2019-10-02 07:10:03 +02:00
Akim Demaille
2ca6b71967 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.
2019-09-30 18:31:55 +02:00
Akim Demaille
871c02b327 yacc: introduce a type for states
* data/skeletons/yacc.c (yy_state_num): New.
Use it for arrays of states.
2019-09-30 07:26:17 +02:00
Akim Demaille
a57e74a5bf style: prefer symbolic values rather than litterals
Instead of

    #define YYPACT_NINF -130
    #define yypact_value_is_default(Yystate) \
      (!!((Yystate) == (-130)))

generate

    #define YYPACT_NINF (-130)
    #define yypact_value_is_default(Yyn) \
      ((Yyn) == YYPACT_NINF)

* data/skeletons/c.m4 (b4_table_value_equals): Add support for $4.
* data/skeletons/glr.c, data/skeletons/yacc.c: Use it.
Also, use shorter macro argument names, the name of the macro is clear
enough.
2019-09-30 07:25:56 +02:00
Akim Demaille
4971409e39 style: change misleading macro argument name
* data/skeletons/glr.c, data/skeletons/yacc.c
(yypact_value_is_default): It does not take a rule number as argument.
2019-09-30 07:25:48 +02:00
Akim Demaille
9861bcc540 api.token.raw: implement
Bison used to feature %raw, documented as follows:

    @item %raw
    The output file @file{@var{name}.h} normally defines the tokens with
    Yacc-compatible token numbers.  If this option is specified, the
    internal Bison numbers are used instead.  (Yacc-compatible numbers start
    at 257 except for single character tokens; Bison assigns token numbers
    sequentially for all tokens starting at 3.)

Unfortunately, as far as I can tell, it never worked: token numbers
are indeed changed in the generated tables (from external token number
to internal), yet the code was still applying the mapping from
external token numbers to internal token numbers.

This commit reintroduces the feature as it was expected to be.

* data/skeletons/bison.m4 (b4_token_format): When api.token.raw is
enabled, use the internal token number.
* data/skeletons/yacc.c (yytranslate): Don't emit if api.token.raw is
enabled.
(YYTRANSLATE): Adjust.
2019-09-14 09:55:17 +02:00
Akim Demaille
d94d83e10b style: tidy yacc.c
* data/skeletons/yacc.c: Include 'c.m4' first.
Then sort the handling of %define variables.
* tests/input.at: Adjust.
2019-09-14 09:55:17 +02:00
Adrian Vogelsgesang
996abe62d7 style: readability improvements to yacc.c
* data/skeletons/yacc.c (yysyntax_error): Change the nesting of `m4`
conditions slightly to make it more readable.
The generated C code stays unchanged.
2019-08-09 06:06:02 -05:00
Akim Demaille
61d53e606d yacc.c: minor style change
* data/skeletons/yacc.c: To improve consistency with other similar
pieces of code.
2019-04-12 08:38:30 +02:00
Akim Demaille
0f193d2d21 no-lines: avoid leaving an empty line instead of the syncline
Currently, with --no-lines, instead of "#line file line\n", we emit
"\n".  Let's emit nothing.

* data/skeletons/bison.m4 (b4_syncline): Emit at end-of-line when enabled.
* data/skeletons/bison.m4, data/skeletons/c.m4, data/skeletons/glr.cc,
* data/skeletons/lalr1.cc, src/output.c: Use dnl after b4_syncline to
avoid spurious empty lines.

* tests/synclines.at (Sync Lines): Make sure that --no-lines is like
grep -v #line.
* tests/calc.at: Make sure that a rich grammar file behaves properly
with %no-lines.
2019-04-03 19:20:39 +02:00
Akim Demaille
b5cd777ad6 yacc.c: don't suggest api.header.include when --defines is not used
See 4e19ab9fcd: the suggestion to
include the header file should not be emitted when the header is not
generated.

* data/skeletons/yacc.c: Here.
2019-03-24 18:52:58 +01:00
Akim Demaille
58ae95670b style: rename spec_defines_file as spec_header_file
The variable spec_defines_file denotes the name of the generated
header.  Its name is derived from --defines/%defines, whose name in
turn is derived from the fact that the header, in Yacc, contained the

Not only does the header now contain a lot more than just the token
definitions, but we no longer even generate macros, but an enum...

Let's modernize our vocabulary.

* src/files.h, src/files.c (spec_defines_file): Rename as...
(spec_header_file): this.
2019-03-17 16:36:05 +01:00
Akim Demaille
4e19ab9fcd yacc.c: provide a means to include the header in the implementation
Currently when --defines is used, we generate a header, and paste an
exact copy of it into the generated parser implementation file.  Let's
provide a means to #include it instead.

We don't do it by default because of the Autotools' ylwrap.  This
program wraps invocations of yacc (that uses a fixed output name:
y.tab.c, y.tab.h, y.output) to support a more modern naming
scheme (dir/foo.y -> dir/foo.tab.c, dir/foo.tab.h, etc.).  It does
that by renaming the generated files, and then by running sed to
propagate these renamings inside the files themselves.

Unfortunately Automake's Makefiles uses Bison as if it were Yacc (with
--yacc or with -o y.tab.c) and invoke bison via ylwrap.  As a
consequence, as far as Bison is concerned, the output files are
y.tab.c and y.tab.h, so it emits '#include "y.tab.h"'.  So far, so
good.  But now ylwrap processes this '#include "y.tab.h"' into
'#include "dir/foo.tab.h"', which is not guaranteed to always work.

So, let's do the Right Thing when the output file is not y.tab.c, in
which case the user should %define api.header.include.  Binding this
behavior to --yacc is tempting, but we recently told people to stop
using --yacc (as it also enables the Yacc warnings), but rather to use
-o y.tab.c.

Yacc.c is the only skeleton concerned: all the others do include their
header.

* data/skeletons/yacc.c (b4_header_include_if): New.
(api.header.include): Provide a default value when the output is not
y.tab.c.
* src/parse-gram.y (api.header.include): Define.
2019-03-17 16:36:05 +01:00