Commit Graph

20 Commits

Author SHA1 Message Date
Akim Demaille
0c0cf6f5a4 examples: modernize the example Makefiles
* examples/c++/Makefile, examples/c++/calc++/Makefile,
* examples/c++/glr/Makefile, examples/c/bistromathic/Makefile,
* examples/c/calc/Makefile, examples/c/glr/Makefile,
* examples/c/lexcalc/Makefile, examples/c/mfcalc/Makefile,
* examples/c/pushcalc/Makefile, examples/c/reccalc/Makefile,
* examples/c/rpcalc/Makefile, examples/d/calc/Makefile,
* examples/d/simple/Makefile, examples/java/calc/Makefile,
* examples/java/simple/Makefile:
Use --html to generate *.html directly.
No longer demonstrate --xml.
No longer show rules for xml to html.
Use --header, not --defines.
Use --graph without specifying the output file now that we
generate *.gv by default.
2021-08-04 10:07:48 +02:00
Paul Eggert
b4582f1918 Update URLs to prefer https: to http:
Also, fix a few http: URLs that were no longer working.
2021-01-29 13:48:43 -08:00
Akim Demaille
4fd2e5c22a package: fixes
* examples/c++/glr/local.mk, examples/c/glr/local.mk (CLEANFILES):
Complete to fix distcheck.
* src/output.c (merger_output): Obfuscate to pacify syntax-check's
sc_space_before_open_paren.
2021-01-16 17:30:17 +01:00
Akim Demaille
d7e8aaa271 package: bump copyrights to 2021
Run 'make update-copyright'.
2021-01-16 16:11:17 +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
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
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
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
c1884d3002 glr.c: example: use a printer
* examples/c++/glr/c++-types.yy: Here.
2021-01-03 08:14:22 +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
89296e3962 glr2.cc: example: simplify
* examples/c++/glr/c++-types.yy: Formatting changes.
Remove unused support for '@'.
* examples/c/glr/c++-types.y: Ditto.
2020-12-26 17:40:18 +01:00
Akim Demaille
2157ced3dd c++: rename semantic_type as value_type
We always refer to the triplet "kind, value, location".  All of them
are nouns, and we support api.value.type and api.location.type.  On
this regard, "semantic_type" was a poor choice.  Make it "value_type".

The test suite was not updated to use value_type, on purpose, to
enforce backward compatibility.

* data/skeletons/c++.m4, data/skeletons/glr.cc, data/skeletons/glr2.cc,
* data/skeletons/variant.hh, doc/bison.texi: Define value_type rather
than semantic_type.
Add a backward compatibility typedef.
* examples/c++/glr/c++-types.yy: Migrate.
2020-12-26 09:05:45 +01:00
Akim Demaille
0a82316e54 glr2.cc: example: use objects (not pointers) to represent the AST
Currently we are using pointers.  The whole point of
glr2.cc (vs. glr.cc) is precisely to allow genuine C++ objects to be
semantic values.  Let's make that work.

* data/skeletons/glr2.cc (glr_state::glr_state): Be sure to initialize
yysval.
(glr_state): Add copy-ctor, assignment and dtor.
(glr_state::copyFrom): Be sure to initialize the destination if it was
not.
(glr_state::~glr_state): Destroy the semantic value.
* examples/c++/glr/ast.hh: Rewrite so that we use genuine objects,
rather than a traditional OOP hierarchy that requires to deal with
pointers.
With help from Bruno Belanyi <bruno.belanyi@epita.fr>.
* examples/c++/glr/c++-types.yy: Remove memory management.
Use true objects.
(main): Don't reach yydebug directly.

* examples/c++/glr/local.mk: We need C++11.
2020-12-20 18:41:42 +01:00
Akim Demaille
7292ed6e64 glr2.cc: example: use streams and accept arguments
From a debugger, it is easier to pass a file name than working on
stdin.

* examples/c++/glr/c++-types.yy: Reduce scopes.
Avoid YYSTYPE/YYLTYPE: use the C++ types.
(input, process): New.
(main): Use them.
2020-12-20 14:54:46 +01:00
Akim Demaille
80664353b2 glr2.cc: example: style: add missing copyright headers
* examples/c++/glr/ast.hh, examples/c++/glr/c++-types.yy: here.
* examples/c++/glr/local.mk: Fix distribution of ast.hh.
2020-12-19 07:29:30 +01:00
Akim Demaille
d6fbeb2798 glr2.cc: example: address Clang warnings
ast.hh:24:7: error: 'Node' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit [-Werror,-Wweak-vtables]
    class Node
          ^
    ast.hh:57:7: error: 'Nterm' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit [-Werror,-Wweak-vtables]
    class Nterm : public Node
          ^
    ast.hh:102:7: error: 'Term' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit [-Werror,-Wweak-vtables]
    class Term : public Node
          ^

* examples/c++/glr/ast.hh: Define the destructors out of the class
definition.
This does not change anything, it is still in the header, but that
does pacify clang.
2020-12-19 07:29:29 +01:00
Akim Demaille
b2d35f5cb8 glr2.cc: make the example more C++
Currently the example really looks like C.  Instead of a union of
structs to implement the AST, use a hierarchy.  It would be nice to
feature a C++17 version with std variants.

* examples/c++/glr/c++-types.yy (Node, free_node, new_nterm)
(new_term): Move into...
* examples/c++/glr/ast.hh: here, a proper C++ hierarchy.
2020-12-14 06:33:22 +01:00
Akim Demaille
5b65b3d543 glr2.cc: fix pointer arithmethics
A glr_state keeps tracks of its predecessor using an offset relative
to itself (i.e., pointer subtraction).  Unfortunately we sometimes
have to compute offsets for pointers that live in different
containers, in particular in yyfillin.  In that case there is no
reason for the distance between the two objects to be a multiple of
the object size (0x40 on my machine), and the resulting ptrdiff_t may
be "wrong", i.e., it does allow to recover one from the other.  We
cannot use "typed" pointer arithmetics here, the Euclidean division
has it wrong.  So use "plain" char* pointers.

Fixes 718 (Duplicate representation of merged trees: glr2.cc) and
examples/c++/glr/c++-types.

Still XFAIL:

    712: Improper handling of embedded actions and dollar(-N) in GLR parsers: glr2.cc
    730: Incorrectly initialized location for empty right-hand side in GLR: glr2.cc
    748: Incorrect lookahead during nondeterministic GLR: glr2.cc

* data/skeletons/glr2.cc (glr_state::as_pointer_): New.
(glr_state::pred): Use it.
* examples/c++/glr/c++-types.test: The test passes.
* tests/glr-regression.at (Duplicate representation of merged trees:
glr2.cc): Passes.
2020-12-14 06:33:21 +01:00
Akim Demaille
f9b360663b glr2.cc: add an example
Currently this example crashes on input such as "T (x) + y;".
The same example with glr.c works properly.

* examples/c++/glr/Makefile, examples/c++/glr/README.md,
* examples/c++/glr/c++-types.test, examples/c++/glr/c++-types.yy,
* examples/c++/glr/local.mk, examples/c++/local.mk: New.
Based on examples/c/glr/c++-types.y.
2020-09-26 18:33:48 +02:00