Commit Graph

7737 Commits

Author SHA1 Message Date
Akim Demaille
da4d38f830 glr2.cc: remove some useless qualifications
* data/skeletons/glr2.cc: Rely on the type-aliases provided by
glr_stack.
2021-01-16 07:14:23 +01:00
Akim Demaille
769322e01e glr2.cc: more free functions about the automaton into glr_stack
* data/skeletons/glr2.cc (yypact_value_is_default)
(yytable_value_is_error, yyisShiftAction, yyisErrorAction)
(yyisDefaultedState, yydefaultAction):
Move into...
(glr_stack): here.
Fix naming conventions.
2021-01-16 07:14:00 +01:00
Akim Demaille
19418cfc75 glr2.cc: move free-functions into glr_stack
* data/skeletons/glr2.cc (yypreference, yyLRgotoState): Move into...
(glr_stack): here.
2021-01-16 07:13:50 +01:00
Akim Demaille
42d5a16f8d glr2.cc: make yygetToken a member of glr_stack
It's on purpose that I keep the `this->` now.  We'll see later if we
want to remove them.

* data/skeletons/glr2.cc (yygetToken): Move into...
(glr_stack::yyget_token): this.
(b4_lex): Adjust.
2021-01-16 07:13:27 +01:00
Akim Demaille
118c4bd3a1 glr2.cc: move parser::parse into glr_stack
Currently we have two classes that actually should be fused together:
parser and glr_stack.  Both carry part of the parsing: (i) parser
contains `parse`, which is the top-level of the parsing process, it
uses yygetToken, etc., and (ii) glr_stack takes care of all the
details (dealing with the stack), and also calls yygetToken.

However if we fuse them together, we would get a large parser class,
in the header file.  So it is probably better to split this large
class using the pimpl idiom.  But then it appears that glr_stack is
very close from being the impl of parser.

Let's improve this.

For a start...

* data/skeletons/glr2.cc (parser::parse): Move to...
(glr_stack::parse): here.
(parser::parse): Use it.
2021-01-16 07:11:05 +01:00
Akim Demaille
82133a6103 cex: add support for $TIME_LIMIT
* src/counterexample.c (TIME_LIMIT): Replace with...
(time_limit): this.
(counterexample_init): Check $TIME_LIMIT.
* src/scan-gram.l: Reorder includes.
2021-01-14 06:35:09 +01:00
Akim Demaille
cca770a156 c: adjust _Noreturn to pedantic clang
Reported by Joe Nelson <joe@begriffs.com>.
https://lists.gnu.org/r/help-bison/2021-01/msg00004.html
Fixed by Paul Eggert in gnulib.
https://lists.gnu.org/r/bug-gnulib/2021-01/msg00156.html

* data/skeletons/c.m4 (b4_attribute_define): Adjust _Noreturn to
pedantic clang.
2021-01-13 08:03:45 +01:00
Akim Demaille
430ca0fc63 cex: send traces to stderr, not stdout
When comparing traces from different machines, the mixture of
stdout/stderr in the output are making things uselessly difficult.

* src/lssi.c, src/state-item.c: Output debug traces on stderr.
2021-01-13 08:03:45 +01:00
Akim Demaille
983364d76a glr2.cc: remove remains from glr.cc
* data/skeletons/glr2.cc: We no longer play dirty tricks with
parse-params, remove the now useless dance around them.
We now have genuine objects for locations, leave the initial action
alone.
2021-01-10 21:30:34 +01:00
Akim Demaille
6833b1be47 glr2.cc: add support for api.token.constructor
* data/skeletons/glr2.cc: Add support for api.token.constructor.
* examples/c++/glr/c++-types.yy: Use it.
* examples/c++/glr/c++-types.test: Adjust expectations for error
messages.
2021-01-10 17:12:50 +01:00
Akim Demaille
4aa731d110 glr2.cc: introduce the yytranslate_ member function
* data/skeletons/c++.m4 (b4_yytranslate_define): Use static_cast
rather than the YY_CAST macro.
Avoids the need to define YY_CAST in the header.
* data/skeletons/glr2.cc: Fix calls to b4_shared_declarations: pass
the type of file we are in.
Don't define YYTRANSLATE.
(parser::yytranslate_): New, as in lalr1.cc.
Adjust to use it.

* tests/glr-regression.at: Adjust.
2021-01-10 17:04:06 +01:00
Akim Demaille
3ad6d862b7 glr2.cc: tests: simplify
* tests/glr-regression.at: Remove useless qualification for tokens.
2021-01-10 17:04:06 +01:00
Akim Demaille
3fa59c32fc glr2.cc: rely on symbol kinds rather than token kinds
Instead of tracking the lookahead with yychar, use yytoken.  This is
consistent with lalr1.cc, saves us from calls to YYTRANSLATE (except
when calling yylex), and makes it easier to migrate to using
symbol_type.

* data/skeletons/glr2.cc: Replace all uses of `int yychar` with
`symbol_kind_type yytoken`.
(yygetToken): Don't take/return the lookahead's token-kind and
symbol-kind, just work directly on yystack's `yytoken` member.

* tests/glr-regression.at (AT_PRINT_LOOKAHEAD_DECLARE)
(AT_PRINT_LOOKAHEAD_DEFINE): New.
Adjust to the fact that we have yytoken, not yychar, in glr2.cc.
2021-01-10 17:04:06 +01:00
Akim Demaille
47612d987b glr2.cc: example: style changes
* examples/c++/glr/c++-types.yy: We need C++ 11 (we use shared_ptr).
2021-01-10 17:04:04 +01:00
Akim Demaille
3a98be5284 glr2.cc: use references to print symbols
* data/skeletons/glr2.cc (b4_symbol_action): New, so that we use
`yyval` and `yyloc` rather than the `yyvaluep` and `yylocationp`
pointers.
(yy_symbol_value_print_, yy_symbol_print_, yy_destroy_): Use
references rather than pointers.
Drop support for the undocumented, obsolete, `yyoutput` variable.
Adjust callers.
(glr_state::destroy): Don't use yy_symbol_print_ when we don't have a
symbol.  Rather, write dedicated code.
2021-01-10 17:02:54 +01:00
Akim Demaille
fbdff2bc38 glr2.cc: fix memory leak
* data/skeletons/glr2.cc (glr_stack_item::setState): Free the previous
state before installing the new one.
2021-01-10 17:02:54 +01:00
Akim Demaille
3bcc090758 glr: examples: fix locations
The locations are actually false: they should include the location of
the semicolon (we print statements), but they don't.

* examples/c++/glr/c++-types.test, examples/c++/glr/c++-types.yy,
* examples/c/glr/c++-types.test, examples/c/glr/c++-types.y,
* tests/cxx-type.at:
Fix locations and adjust expectations.
2021-01-10 17:02:54 +01:00
Akim Demaille
5433b0a84b glr: tests: more macros
This will be useful to support changes in glr2.cc.

tests/glr-regression.at
(Incorrect lookahead during deterministic GLR)
(Incorrect lookahead during nondeterministc GLR):
Introduce and use PRINT_LOOKAHEAD.
2021-01-10 17:02:54 +01:00
Akim Demaille
21c812e2f0 glr: tests: formatting changes
* tests/glr-regression.at: here.
Use %empty where applicable.
2021-01-10 17:02:46 +01:00
Akim Demaille
442c8d85aa tests: fix definition for parser class name
* tests/local.at (AT_PARSER_CLASS): Fix it.
And use it.
2021-01-10 12:14:47 +01:00
Akim Demaille
428999c117 CI: disable the installation of dmd
Currently all the builds fail:

    $ mkdir -p ~/dlang && wget https://dlang.org/install.sh -O ~/dlang/install.sh
    --2021-01-10 06:05:39--  https://dlang.org/install.sh
    Resolving dlang.org (dlang.org)... 162.217.114.56, 2607:fc50:1:ff02::5:0
    Connecting to dlang.org (dlang.org)|162.217.114.56|:443... connected.
    ERROR: cannot verify dlang.org's certificate, issued by ‘CN=R3,O=Let's Encrypt,C=US’:
      Unable to locally verify the issuer's authority.
    To connect to dlang.org insecurely, use `--no-check-certificate'.

* .travis.yml: Disable dmd.
2021-01-10 12:14:43 +01:00
Akim Demaille
b52a2172a1 tests: remove some redundant tests
* tests/calc.at: Cut the CI some slack.
2021-01-07 18:51:48 +01:00
Adela Vais
8d01c60e9c d: create getter for the number of errors from the parser
* data/skeletons/lalr1.d: Here.
2021-01-07 18:51:48 +01:00
Adela Vais
c13b3c02d3 d: remove support for parse.error verbose
Without the history, D should not support this option. Before the
removal, 'detailed' and 'verbose' options generated the same code.

* data/skeletons/lalr1.d: Here.
* doc/bison.texi: Adapt tests to use 'detailed' instead of 'verbose'.
* tests/calc.at: Document it.
2021-01-07 18:51:48 +01:00
Akim Demaille
f859462658 d: add support for %printer
Currently we display the addresses of the semantic values.  Instead,
print the values.

Add support for YY_USE across languages.

* data/skeletons/c.m4, data/skeletons/d.m4 (b4_use): New.
* data/skeletons/bison.m4 (b4_symbol_actions): Use b4_use to be
portable to D.

Add support for %printer, and use it.

* data/skeletons/d.m4: Instead of duplicating what's already in
c-like.m4, include it.
(b4_symbol_action): New.
Differs from the one in bison.m4 in that it uses yyval/yyloc instead
of *yyvaluep and *yylocationp.

* data/skeletons/lalr1.d (yy_symbol_print): Avoid calls to formatting,
just call write directly.
Use the %printer.
* examples/d/calc/calc.y: Specify a printer.
Enable traces when $YYDEBUG is set.
* tests/calc.at: Fix the use of %printer with D.
2021-01-07 06:31:28 +01:00
Adela Vais
dc8b16424a d: remove unnecessary imports
* data/skeletons/lalr1.d: Here.
2021-01-06 18:33:41 +01:00
Adela Vais
5bac3ddcee d: remove yytnamerr usage
It is a backwards-compatible feature for the other parsers.
D should not support this option.

* data/skeletons/d.m4: Here.
2021-01-06 18:33:41 +01:00
Adela Vais
594cae57ca d: add internationalisation support
The D parser implements this feature similarly to the C parser,
by using Gettext. Functions gettext() and dgettext() are
imported using extern(C). The internationalisation uses yysymbol_name
to report the name of the SymbolKinds.

* data/skeletons/d.m4 (SymbolKind.toString.yytranslatable,
SymbolKind.toString.yysymbol_name: New), data/skeletons/lalr1.d: Here.
* doc/bison.texi: Document it.
* tests/calc.at: Test it.
2021-01-06 18:33:41 +01:00
Akim Demaille
e51e89856a glr2.cc: add support for variants
(Bison) Variants are extremely picky, which makes them both
annoying (lots of micro-details must be taken care of) and
precious (all the micro-details must be taken care of, in particular
object lifetime).

So (i) each time a semantic value is stored, it must be stored in a
place that exists, and (ii) each time a semantic value is discarded,
its place must have been emptied.

Example of (i)

    - new (&yys.value ()) value_type (s->value ());
    + {]b4_variant_if([[
    +   new (&yys.value ()) value_type ();
    +   ]b4_symbol_variant([yy_accessing_symbol (s->yylrState)],
    +                      [yys.value ()], [copy], [s->value ()])], [[
    +   new (&yys.value ()) value_type (s->value ());]])[
    + }

Example of (ii)

      yyparser.yy_destroy_ ("Error: discarding",
    -                       yytoken, &yylval]b4_locations_if([, &yylloc])[);
    +                       yytoken, &yylval]b4_locations_if([, &yylloc])[);]b4_variant_if([[
    + // Value type destructor.
    + ]b4_symbol_variant([[YYTRANSLATE (this->yychar)]], [[yylval]], [[template destroy]])])[
      this->yychar = ]b4_namespace_ref[::]b4_parser_class[::token::]b4_symbol(empty, id)[;

However, in some places we must not be "pure".  In particular:

    glr_stack_item (const glr_stack_item& other) YY_NOEXCEPT YY_NOTHROW
      : is_state_ (other.is_state_)
    {
      std::memcpy (raw_, other.raw_, union_size);
    }

still must use memcpy, because the constructor would change pred, and
it must not.  This constructor is used only when resizing the stack,
in which case pred (which is relative) must not be "adjusted".

The result works, but is messy.  Its verbosity comes from at least two
factors:

- we don't have support for complete symbols (binding kind, value and
  location), and we should at least try to have it.  That simplified
  lalr1.cc a lot.

- I have not tried to be smart and use 'move' when possible.  As a
  consequence many places have 'copy' and then 'destroy'.  That kind
  of clean up can be done once everything appears to be solid.

* data/skeletons/glr2.cc: Be more rigorous in object lifetime.
In particular, don't forget to discard the lookahead when we're done
with it.
Call variant routines where needed.
Deal with plenty of details.
(b4_call_merger): Add support for variants.
Use references in mergers, rather than pointers.

* examples/c++/glr/c++-types.yy: Exercise variants.
2021-01-05 09:28:20 +01:00
Akim Demaille
c2a06bf791 glr: strengthen the tests
On some experimentation I was running, the test suite was passing, yet
the example crashed when run in verbose mode.  Let's add this case to
the test suite.

* tests/cxx-type.at: Run all these tests in verbose mode too.
2021-01-05 07:23:44 +01:00
Akim Demaille
8733959954 c++: I'm tired of Flex's warnings
* doc/bison.texi: Disable another warning I'm tired to see.
New releases would be most welcome.
2021-01-03 20:11:48 +01:00
Akim Demaille
31b8b8f179 glr: example: flush the output
* examples/c/glr/c++-types.y: Flush stdout so that the logs (on
stderr) and the effective output (on stdout) mix correctly.
While at it, be a bit more const-correct.
2021-01-03 19:58:23 +01:00
Akim Demaille
e2199d0fb2 style: YYUSE is private, make it YY_USE
This macro is not exposed to users, make start it with 'YY_'.

* data/skeletons/bison.m4, data/skeletons/c.m4, data/skeletons/glr.c,
* data/skeletons/glr.cc, data/skeletons/glr2.cc, data/skeletons/lalr1.cc,
* src/parse-gram.c, tests/actions.at, tests/c++.at, tests/headers.at,
* tests/local.at (YYUSE): Rename as...
(YY_USE): this.
2021-01-03 19:57:10 +01:00
Akim Demaille
c1884d3002 glr.c: example: use a printer
* examples/c++/glr/c++-types.yy: Here.
2021-01-03 08:14:22 +01:00
Akim Demaille
ab1208e263 glr: consistently use the same wording in traces
* data/skeletons/glr.c, data/skeletons/glr2.cc (yyglrReduce): Traces
refer to "state 42", not to "state #42".
2021-01-03 08:14:22 +01:00
Akim Demaille
74d1e881a7 glr2.cc: also equip semantic_option with self check
* data/skeletons/glr2.cc (semantic_option): Add MAGIC_, magic_ and
check_ members.
Use it.
2021-01-02 13:02:34 +01:00
Akim Demaille
f30067ed51 glr2.cc: log the execution of deferred actions
See "glr.c: log the execution of deferred actions".

* data/skeletons/glr2.cc (yyuserAction): Take yyk as a new argument.
Rename argument yyn as yyrule for clarity.
Log before and after the user action.
Adjust callers to not call YY_REDUCE_PRINT and YY_SYMBOL_PRINT.
2021-01-02 13:02:19 +01:00
Akim Demaille
630448ba6b glr2.cc: minor clean up
* data/skeletons/glr2.cc (YYUNDEFTOK): Now useless.
Formatting/coding style changes.
2021-01-02 08:28:26 +01:00
Akim Demaille
1283dc7243 glr.c: log the execution of deferred actions
Currently deferred reductions are not "verbose" at all: only immediate
reductions are displayed in the YYDEBUG traces.  I don't understand
why.  Besides it seems actually simpler the install the reduction
traces right around the user action inside yyuserAction rather that
around calls to yyuserAction.

This only trouble is that yyuserAction does not know the stack number
it works on, so we have to pass it.  And pass -1 when we are actually
running on a temporary stack.

The glr example, on "T(x) + y;" as input, adds these logs, which
allow to see when the `<cast>` is built:

     Stack 0 Entering state 26
     Reduced stack 0 by rule 7 (line 108); action deferred.  Now in state 7.
     Stack 0 Entering state 7
     Reading a token
     Next token is token '+' (1.6: )
     Stack 1 Entering state 27
     Reduced stack 1 by rule 13 (line 123); action deferred.  Now in state 12.
     Stack 1 Entering state 12
     Next token is token '+' (1.6: )
     Stack 1 dies.
     Removing dead stacks.
     On stack 0, shifting token '+' (1.6: )
     Stack 0 now in state #14
    +Reducing stack -1 by rule 6 (line 107):
    +   $1 = token identifier (1.3: x)
    +-> $$ = nterm expr (1.3: x)
    +Reducing stack -1 by rule 7 (line 108):
    +   $1 = token typename (1.0: T)
    +   $2 = token '(' (1.2: )
    +   $3 = nterm expr (1.3: x)
    +   $4 = token ')' (1.4: )
    +-> $$ = nterm expr (1.0-3: <cast>(x,T))
     Returning to deterministic operation.

* data/skeletons/glr.c (yyuserAction): Take yyk as a new argument.
Rename argument yyn as yyrule for clarity.
Log before and after the user action.
Adjust callers to not call YY_REDUCE_PRINT and YY_SYMBOL_PRINT.
2021-01-02 07:37:00 +01:00
Akim Demaille
e3d4b42f58 glr.c: reorder routines
The next commit wants to use YY_REDUCE_PRINT above its current
definition.  Move it higher.

* data/skeletons/glr.c (yylhsNonterm, YY_REDUCE_PRINT): Make available
earlier.
2021-01-02 07:37:00 +01:00
Akim Demaille
f67c1ce937 glr.c: example: use the exact same display as in the C++ example
* examples/c/glr/c++-types.y: Add a space after the commas.
* examples/c/glr/c++-types.test: Adjust expectations.
2021-01-02 07:37:00 +01:00
Akim Demaille
5a4e606275 glr.c: example: several improvements
* examples/c/glr/c++-types.y (node_print): New.
Use YY_LOCATION_PRINT instead of duplicating it.
And actually use it in the action instead of badly duplicating it.
(main): Add proper option support.
* examples/c/glr/c++-types.test: Adjust expectations on locations.
* examples/c++/glr/c++-types.yy: Fix bad iteration.
2021-01-02 07:36:35 +01:00
Akim Demaille
83f2eb3737 glr2.cc: the example requires Bison 3.8
This will save us from generating the position.hh file.

* src/parse-gram.y: Claim we are 3.8.
* examples/c++/glr/c++-types.yy: Require 3.8.
2020-12-31 08:21:25 +01:00
Akim Demaille
3911aba39a %merge: associate it to its first definition, not the latest
Currently each time we meet %merge we record this location as the
defining location (and symbol).  Instead, record the first definition.

In the generated code we go from

    yy0->A = merge (*yy0, *yy1);

to

    yy0->S = merge (*yy0, *yy1);

where S was indeed the first symbol, and in the diagnostics we go from

    glr-regr18.y:30.18-24: error: result type clash on merge function 'merge': <type2> != <type1>
       30 | sym2: sym3 %merge<merge> { $$ = $1; } ;
          |                  ^~~~~~~
    glr-regr18.y:29.18-24: note: previous declaration
       29 | sym1: sym2 %merge<merge> { $$ = $1; } ;
          |                  ^~~~~~~
    glr-regr18.y:31.13-19: error: result type clash on merge function 'merge': <type3> != <type2>
       31 | sym3: %merge<merge> { $$ = 0; } ;
          |             ^~~~~~~
    glr-regr18.y:30.18-24: note: previous declaration
       30 | sym2: sym3 %merge<merge> { $$ = $1; } ;
          |                  ^~~~~~~

to

    glr-regr18.y:30.18-24: error: result type clash on merge function 'merge': <type2> != <type1>
       30 | sym2: sym3 %merge<merge> { $$ = $1; } ;
          |                  ^~~~~~~
    glr-regr18.y:29.18-24: note: previous declaration
       29 | sym1: sym2 %merge<merge> { $$ = $1; } ;
          |                  ^~~~~~~
    glr-regr18.y:31.13-19: error: result type clash on merge function 'merge': <type3> != <type1>
       31 | sym3: %merge<merge> { $$ = 0; } ;
          |             ^~~~~~~
    glr-regr18.y:29.18-24: note: previous declaration
       29 | sym1: sym2 %merge<merge> { $$ = $1; } ;
          |                  ^~~~~~~

where both duplicates are reported against definition 1, rather than
using definition 1 as a reference when diagnosing about definition 2,
and then 2 as a reference for 3.

* src/reader.c (record_merge_function_type): Keep the first definition.
* tests/glr-regression.at: Adjust.
2020-12-31 08:07:34 +01:00
Akim Demaille
8bc45673d5 %merge: test support for api.value.type=union
* tests/glr-regression.at: here.
2020-12-31 08:07:34 +01:00
Akim Demaille
fbe5abd23d %merge: fix compatibility with api.value.type=union
Reported by Jot Dot.
https://lists.gnu.org/r/help-bison/2020-12/msg00014.html

* data/skeletons/glr.c, data/skeletons/glr2.cc (b4_call_merger): Use
the symbol's slot, not its type.
* examples/c/glr/c++-types.y: Use explicit per-symbol typing together
with api.value.type=union.
(yylex): Use yytoken_kind_t.
2020-12-31 08:07:25 +01:00
Akim Demaille
c09f2e4c7b %merge: delegate the generation of calls to mergers to m4
Don't generate C code from bison, leave that to the skeletons.

* src/output.c (merger_output): Emit invocations to b4_call_merger.
* data/skeletons/glr.c, data/skeletons/glr2.cc (b4_call_merger): New.
2020-12-31 08:07:11 +01:00
Akim Demaille
ac3d5b76f7 %merge: let mergers record a typing-symbol, rather than a type
Symbols are richer than types, and in M4 it is my simpler (and more
common) to deal with symbols rather than types.  So let's associate
mergers to a symbol rather than a type name.

* src/reader.h (merger_list): Replace the 'type' member by a symbol
member.
* src/reader.c (record_merge_function_type): Take a symbol as
argument, rather than a type name.
* src/output.c (merger_output): Adjust.
2020-12-31 08:07:11 +01:00
Akim Demaille
edfcca8481 %merge: clearer tests on diagnostics
* tests/glr-regression.at: Use caret errors.
2020-12-31 08:07:11 +01:00
Akim Demaille
92e943bd24 glr2.cc: style: quoting changes
* data/skeletons/glr2.cc: Use stricter quoting rules.
2020-12-28 08:20:22 +01:00