Commit Graph

1767 Commits

Author SHA1 Message Date
Adela Vais
ee4ec08513 d: m4 style consistency fix
* data/skeletons/lalr1.d: Here.
2020-12-21 06:49:13 +01:00
Adela Vais
13bb2b78b3 d: create alias Symbol for YYParse.Symbol
* data/skeletons/lalr1.d: Here.
* doc/bison.texi: Document it.
* examples/d/calc/calc.y, examples/d/simple/calc.y: Adjust.
* tests/calc.at, tests/d.at, tests/scanner.at: Test it.
2020-12-21 06:47:57 +01:00
Adela Vais
848a17fa33 d: change name of YYParser.Symbol's semanticValue() to value()
Member value was renamed to value_ to avoid the name clash.

* data/skeletons/d.m4: Change member names.
* data/skeletons/lalr1.d: Adjust.
2020-12-21 06:46:09 +01:00
Akim Demaille
c6a39f8409 glr2.cc: formatting changes
* data/skeletons/glr2.cc (glr_state): here.
(glr_state::MAGIC): Make it easier to recognize in decimal.
2020-12-20 18:49:06 +01:00
Akim Demaille
df34ad7c6e glr2.cc: call the destructor in a way that complies with clang
examples/c++/glr/c++-types.cc:721:24: error:
      expected the class name after '~' to name a destructor
    yysval.YYSTYPE::~semantic_type ();
                    ^

Using a local typedef, for some reaon, result in clang complaining
about a useless local typedef.  Since anyway we don't want to keep on
using YYSTYPE and YYLTYPE, it is time to introduce proper typedefs to
reach these guys.  And to be slightly in advance of the other
skeletons: use value_type, not semantic_type.  This is much more
consistent with our use of the (kind, value, location) triplet.

* data/skeletons/glr2.cc (glr_state::value_type)
(glr_state::location_type): New.
(glr_state::~glr_state): Use value_type to name the dtor.
2020-12-20 18:46:17 +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
611348e67b glr2.cc: more checks
* data/skeletons/glr2.cc: Clarify use of magic numbers.
(glr_stack_item::getState): Check the returned state.
2020-12-20 15:56:22 +01:00
Akim Demaille
fe0102d4d5 glr2.cc: fix GLR stack expansion
When expanding the GLR stack, none of the pointers were updated to
reflect the new location of the displaced objects.

This fixes

    748: Incorrect lookahead during nondeterministic GLR: glr2.cc

* data/skeletons/glr2.cc (yyexpandGLRStack): Update the split point
and the stack tops.
(reduceToOneStack): Factor a bit.
2020-12-20 14:54:47 +01:00
Akim Demaille
718cb1ab38 glr2.cc: factor the computation of the accessing symbol
* data/skeletons/glr2.cc (yy_accessing_symbol): New, as in glr.c.
Use it.
2020-12-20 14:54:46 +01:00
Akim Demaille
08f657f4a4 glr2.cc: fix calling conventions for yyexpandGLRStackIfNeeded
This test fails:

748: Incorrect lookahead during nondeterministic GLR: glr2.cc

It consumes lots of stack space, so at some point we need to expand
it.  Because of Boolean logic mistakes, we then claim
memory-exhausted (first error).  Hence we jump to cleaning the
stack (popall_), calling all the destructors, and at some point we
crash with heap-use-after-free (second error).

This commit fixes the first error.  Unfortunately, even though we now
do expand the stack, we crash again with (another)
heap-use-after-free, not addressed here.

Eventually, we should make sure popall_() properly works.

* data/skeletons/glr2.cc (yyexpandGLRStackIfNeeded): Return true iff
success (i.e., memory not exhausted).
2020-12-20 14:54:46 +01:00
Akim Demaille
3f473dd2d7 glr: formatting changes
* data/skeletons/glr.c: Formatting changes.
* data/skeletons/glr2.cc: Ditto.
(glr_state_set::INITIAL_NUMBER_STATES): Remove, unused (and useless:
let std::vector deal with that).
2020-12-20 14:54:46 +01:00
Akim Demaille
03d33fd3a4 skeletons: better comments for some tables
And also, remove the incorrect indentation of these comments:

    -  /* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN.  */
    +/* YYR2[RULE-NUM] -- Number of symbols on the right-hand side of rule RULE-NUM.  */
     static const yytype_int8 yyr2[] =
     {
            0,     2,     4,     0,     2,     1,     1,     1,     3,     2,

I don't remember why this indentation was added (in
0991e29b75), but it seems wrong,
at least for yacc.c.  I suspect this was done with lalr1.cc (where
this is embeded in the class definition, so it should be indented),
but today lalr1.cc uses other routines to output these comments.

* data/skeletons/bison.m4 (b4_integral_parser_tables_map): Improve the
wording of the comments of some tables.
* data/skeletons/c.m4 (b4_integral_parser_table_define): Remove
indentation.
2020-12-20 14:54:46 +01:00
Akim Demaille
0a6c2e6400 glr2.cc: fix glr_stack_item::setState
A glr_stack_item has "raw" memory to store either a glr_state or a
semantic_option.  glr_stack_item::setState stores a state using a copy
assignment.  However, this is more like a construction: we are
starting from "raw" memory, so use the placement new operator instead.
While it probably makes no difference when parse.assert is disabled,
it does make one when it is: the constructor properly initialize the
magic number, the assignment does not.  So without these changes, the
next commit (which stores genuine objects in semantic values) fails
tests 712 and 730 because of incorrect magic numbers.

* data/skeletons/glr2.cc (glr_stack_item::setState): Build the state,
don't just copy it.
2020-12-19 07:29:37 +01:00
Akim Demaille
640b1313d7 glr2.cc: more self checks
* data/skeletons/glr2.cc: here.
2020-12-19 07:29:37 +01:00
Akim Demaille
66706a7c19 glr2.cc: style: s/Type/Kind/g
* data/skeletons/glr2.cc (YY_SYMBOL_PRINT): Use Kind, not Type.
As in the other skeletons.
2020-12-19 07:29:37 +01:00
Akim Demaille
c97dbc46af glr.c: comment changes
* data/skeletons/glr.c (yycompressStack): Reduce scope, and import some
nice comments from glr2.cc.
2020-12-14 06:33:22 +01:00
Akim Demaille
855d46678a glr2.cc: make yyparse a member function
Amusingly enough, glr2.cc still had its core function, yyparse, being
a free function instead of a member function.

* data/skeletons/glr2.cc (yyparse): Remove this free function called
from yyparser::parse.  Inline its body into...
(yyparser::parse): this member function.
This requires moving a bit the yychar, etc. macros.
Access to token can be simplified (the
b4_namespace_ref::b4_parser_class prefix is no longer needed).
2020-12-14 06:33:22 +01:00
Akim Demaille
178a033e8b glr2.cc: being pure is not an option
Remove the useless conditional b4_pure_if: the skeleton is, of course,
pure (no global variables).  Make glr2.cc intrinsically pure.

* data/skeletons/glr2.cc (b4_pure_if): Remove definition and uses.
(b4_lex): New.
Stolen from lalr1.cc to avoid needing to use the one from c.m4.
2020-12-14 06:33:22 +01:00
Akim Demaille
c8006f4637 glr2.cc: fix yycompressStack
Currently, yycompressStack expects the free items to be states only.
That's not the case.

Fixes 712 and 730 pass.  748 still fails, but later and
differently (heap-use-after-free).

* data/skeletons/glr2.cc (glr_stack_item::setState): New.
(glr_stack_item::yycompressStack): Use it.
* tests/glr-regression.at: Adjust.
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
2b52bd1644 glr2.cc: style fixes
* data/skeletons/glr2.cc: Formatting changes.
2020-12-14 06:33:12 +01:00
Akim Demaille
9ee51ece65 glr2.cc: add sanity check in glr_state
The use of YY_IGNORE_NULL_DEREFERENCE_BEGIN/END in `check_` is to
please GCC 10:

    glr-regr8.cc: In member function 'YYRESULTTAG glr_stack::yyresolveValue(glr_state&)':
    glr-regr8.cc:1433:21: error: potential null pointer dereference [-Werror=null-dereference]
     1433 |     YYASSERT (this->magic_ == MAGIC);
          |               ~~~~~~^~~~~~
    glr-regr8.cc:905:40: note: in definition of macro 'YYASSERT'
      905 | # define YYASSERT(Condition) ((void) ((Condition) || (abort (), 0)))
          |                                        ^~~~~~~~~

* data/skeletons/glr2.cc (glr_state::check_): New.
Use it in the member functions.
2020-12-13 06:59:04 +01:00
Akim Demaille
2313f891bd glr2.cc: add sanity checks in glr_stack_item
We obviously have broken pointer arithmetics that hands us
glr_stack_items that are not glr_stack_items.  Have a simple check for
this, to have earlier failures.

* data/skeletons/glr2.cc (glr_stack_item::check_): New.
Use it.
(glr_stack_item::contents): Avoid the useless struct.
Fix minor stylistic issues.
2020-12-13 06:31:41 +01:00
Akim Demaille
4beefee4bf glr2.cc: use the same format for traces as glr.c
* data/skeletons/glr2.cc: here.
This allows to share the same expected output.
2020-12-06 14:02:38 +01:00
Akim Demaille
3301849f0f glr2.cc: add support for parse.assert
* data/skeletons/glr2.cc: Fake support of parse.assert, so that the
tests can use it without failing.
2020-12-06 14:02:38 +01:00
Akim Demaille
349ea900f5 glr2.cc: fix yyresolveValue
When "tests: glr2.cc: run the glr-regression tests" tests are run,
before this commit the following tests used to loop endlessly:

    709: Badly Collapsed GLR States: glr2.cc             FAILED (glr-regression.at:123)
    715: Improper merging of GLR delayed action sets: glr2.cc FAILED (glr-regression.at:397)
    718: Duplicate representation of merged trees: glr2.cc FAILED (glr-regression.at:495)
    751: Leaked semantic values when reporting ambiguity: glr2.cc FAILED (glr-regression.at:1632)

After this commit, no test loops and 709, 715, and 751 pass.  Only 718
still fails.

* data/skeletons/glr2.cc (yyresolveValue): Add missing incrementation
of the iteration variable.
2020-12-06 14:02:38 +01:00
Valentin Tolmer
1b85ac4586 glr2.cc: misc cleanups
* data/skeletons/glr2.cc: Use 'const' on variables and applicable
member functions.
Improve comments.
Use references where applicable.
Enforce names_like_this, notLikeThis.
Reduce scopes.
2020-12-06 14:02:38 +01:00
Valentin Tolmer
2ec6df3b07 glr2.cc: fix memory corruption bug
* data/skeletons/glr2.cc (yyremoveDeletes): Remove double-increment in
the loop.
(glr_state::copyFrom): Handle gracefully when other is resolved.
2020-12-06 14:02:38 +01:00
Akim Demaille
e72eda7aee glr2.cc: turn some pointers into references
* data/skeletons/glr2.cc: Prefer references to pointers.
Add a few more const.
2020-12-06 14:02:38 +01:00
Valentin Tolmer
4f24f5f304 glr2.cc: use 'const' for some constant local variables
* data/skeletons/glr2.cc: here.
2020-12-06 13:54:45 +01:00
Akim Demaille
12b5924939 glr2.cc: fix when the stack is not expandable
* data/skeletons/glr2.cc (yyexpandGLRStackIfNeeded): Fix the
implementation when !YYSTACKEXPANDABLE.
2020-12-06 13:54:45 +01:00
Akim Demaille
d4188398f1 glr.c: fix line numbers in logs
* data/skeletons/glr.c (yyglrReduce): Fix line numbers.
* tests/glr-regression.at: Fix expectations.
2020-12-06 13:54:45 +01:00
Akim Demaille
84a84560c3 glr.cc: don't "leak" yyparse
When using glr.cc, the C function yyparse is an internal detail that
should not be exposed.  Users might call it by accident (I did).

* data/skeletons/glr.c (yyparse): When used for glr.cc, rename as yy_parse_impl.
* data/skeletons/glr.cc: Adjust.
2020-12-05 07:36:01 +01:00
Akim Demaille
9840e5b621 c++: use noexcept where appropriate
Reported by Don Macpherson.
https://github.com/akimd/bison/issues/63
https://github.com/akimd/bison/issues/64

* data/skeletons/c++.m4, data/skeletons/lalr1.cc: here.
2020-12-03 06:19:17 +01:00
Akim Demaille
38cdb2aba2 multistart: also use the api.prefix in the implementation
We were declaring foo_parse_expr but implementing yyparse_expr.

* data/skeletons/yacc.c (_b4_define_sub_yyparse): Use api.prefix for
the parse function name.
2020-11-21 14:57:29 +01:00
Adela Vais
e5854bbddd d: change YYLocation's type from class to struct
This avoids heap allocation and gives minimal costs for the
creation and destruction of the YYParser.Symbol struct if
the location tracking is active.

Suggested by H. S. Teoh.

* data/skeletons/lalr1.d: Here.
* doc/bison.texi: Document it.
* examples/d/calc/calc.y: Adjust.
* tests/calc.at: Test it.
2020-11-20 22:09:31 +01:00
Adela Vais
10305f3e94 d: change the return value of yylex from TokenKind to YYParser.Symbol
The complete symbol approach was deemed to be the right approach for Dlang.
Now, the user can return from yylex() an instance of YYParser.Symbol structure,
which binds together the TokenKind, the semantic value and the location. Before,
the last two were reported separately to the parser.
Only the user API is changed, Bisons's internal structure is kept the same.

* data/skeletons/d.m4 (struct YYParser.Symbol): New.
* data/skeletons/lalr1.d: Change the return value.
* doc/bison.texi: Document it.
* examples/d/calc/calc.y, examples/d/simple/calc.y: Demonstrate it.
* tests/calc.at, tests/scanner.at: Test it.
2020-11-20 22:09:31 +01:00
Adela Vais
593724366f d: add support for lookahead correction
When using lookahead correction, the method YYParser.Context.getExpectedTokens
is not annotated with const, because the method calls yylacCheck, which is not
const. Also, because of yylacStack and yylacEstablished, yylacCheck needs to
be called from the context of the parser class, which is sent as parameter to
the Context's constructor.

* data/skeletons/lalr1.d (yylacCheck, yylacEstablish, yylacDiscard,
yylacStack, yylacEstablished): New.
(Context): Use it.
* doc/bison.texi: Document it.
* tests/calc.at: Check it.
2020-11-18 08:14:43 +01:00
Adela Vais
0e51f6146a d: change the name of the custom error message function to reportSyntaxError
Changed from syntax_error to reportSyntaxError to be similar to the Java parser.

* data/skeletons/lalr1.d: Change the function name.
* doc/bison.texi: Document it.
* tests/local.at: Adjust.
2020-11-18 08:14:21 +01:00
Akim Demaille
b9b13602ed style: prefer b4_symbol(empty, ...) to b4_symbol(-2, ...)
* data/skeletons/c.m4: here.
2020-11-13 07:01:36 +01:00
Akim Demaille
23472033ee Merge branch 'maint'
* maint:
  c++: shorten the assertions that check whether tokens are correct
  c++: don't glue functions together
  lalr1.cc: YY_ASSERT should use api.prefix
  c++: don't use YY_ASSERT at all if parse.assert is disabled
  c++: style: follow the Bison m4 quoting pattern
  yacc.c: provide the Bison version as an integral macro
  regen
  style: make conversion of version string to int public
  %require: accept version numbers with three parts ("3.7.4")
  yacc.c: fix #definition of YYEMPTY
  gnulib: update
  doc: fix incorrect section title
  doc: minor grammar fixes in counterexamples section
2020-11-13 07:01:19 +01:00
Akim Demaille
d8cc6b073e c++: shorten the assertions that check whether tokens are correct
Before:

    YY_ASSERT (tok == token::YYEOF || tok == token::YYerror || tok == token::YYUNDEF || tok == 120 || tok == 49 || tok == 50 || tok == 51 || tok == 52 || tok == 53 || tok == 54 || tok == 55 || tok == 56 || tok == 57 || tok == 97 || tok == 98);

After:

    YY_ASSERT (tok == token::YYEOF
               || (token::YYerror <= tok && tok <= token::YYUNDEF)
               || tok == 120
               || (49 <= tok && tok <= 57)
               || (97 <= tok && tok <= 98));

Clauses are now also wrapped on several lines.  This is nicer to read
and diff, but also avoids pushing Visual C++ to its arbitrary
limits (640K and lines of 16380 bytes ought to be enough for anybody,
otherwise make an C2026 error).

The useless parens are there for the dummy warnings about
precedence (in the future, will we also have to put parens in
`1+2*3`?).

* data/skeletons/variant.hh (_b4_filter_tokens, b4_tok_in, b4_tok_in):
New.
(_b4_token_constructor_define): Use them.
2020-11-13 06:17:52 +01:00
Akim Demaille
0264b4bca0 c++: don't glue functions together
* data/skeletons/bison.m4 (b4_type_foreach): Accept a separator.
* data/skeletons/c++.m4: Use it.
And fix an incorrect comment.
2020-11-13 06:17:52 +01:00
Akim Demaille
8b424b865e lalr1.cc: YY_ASSERT should use api.prefix
Working on the previous commit I realized that YY_ASSERT was used in
the generated headers, so must follow api.prefix to avoid clashes when
multiple C++ parser with variants are used.

Actually many more macros should obey api.prefix (YY_CPLUSPLUS,
YY_COPY, etc.).  There was no complaint so far, so it's not urgent
enough for 3.7.4, but it should be addressed in 3.8.

* data/skeletons/variant.hh (b4_assert): New.
Use it.
* tests/local.at (AT_YYLEX_RETURN): Fix.
* tests/headers.at: Make sure variant-based C++ parsers are checked
too.
This test did find that YY_ASSERT escaped renaming (before the fix in
this commit).
2020-11-13 06:17:52 +01:00
Akim Demaille
f4431ea115 c++: don't use YY_ASSERT at all if parse.assert is disabled
In some extreme situations (about 800 tokens), we generate a
single-line assertion long enough for Visual C++ to discard the end of
the line, thus falling into parse ends for the missing `);`.  On a
shorter example:

    YY_ASSERT (tok == token::TOK_YYEOF || tok == token::TOK_YYerror || tok == token::TOK_YYUNDEF || tok == token::TOK_ASSIGN || tok == token::TOK_MINUS || tok == token::TOK_PLUS || tok == token::TOK_STAR || tok == token::TOK_SLASH || tok == token::TOK_LPAREN || tok == token::TOK_RPAREN);

Whether NDEBUG is used or not is irrelevant, the parser dies anyway.

Reported by Jot Dot <jotdot@shaw.ca>.
https://lists.gnu.org/r/bug-bison/2020-11/msg00002.html

We should avoid emitting lines so long.

We probably should also use a range-based assertion (with extraneous
parens to pacify fascist compilers):

    YY_ASSERT ((token::TOK_YYEOF <= tok && tok <= token::TOK_YYUNDEF)
               || (token::TOK_ASSIGN <= tok && ...)

But anyway, we should simply not emit this assertion at all when not
asked for.

* data/skeletons/variant.hh: Do not define, nor use, YY_ASSERT when it
is not enabled.
2020-11-13 06:17:52 +01:00
Akim Demaille
fe8c36ddca c++: style: follow the Bison m4 quoting pattern
* data/skeletons/variant.hh: here.
2020-11-13 06:17:24 +01:00
Akim Demaille
21c147b6e5 yacc.c: provide the Bison version as an integral macro
Suggested by Balazs Scheidler.
https://github.com/akimd/bison/issues/55

* src/muscle-tab.c (muscle_init): Move/rename `b4_version` to/as...
* src/output.c (prepare): `b4_version_string`.
Also define `b4_version`.
* data/skeletons/bison.m4, data/skeletons/c.m4, data/skeletons/d.m4,
* data/skeletons/java.m4: Adjust.
* doc/bison.texi: Document it.
2020-11-11 09:08:57 +01:00
Akim Demaille
14c65a35f0 %require: accept version numbers with three parts ("3.7.4")
* src/parse-gram.y (str_to_version): Support three parts.
* data/skeletons/location.cc, data/skeletons/stack.hh:
Adjust.
2020-11-11 08:47:23 +01:00
Todd C. Miller
c47bb87f9f yacc.c: fix #definition of YYEMPTY
When generating a C parser, YYEMPTY is present in enum yytokentype but
there is no corresponding #define like there is for the other values.
There is a special case for YYEMPTY in b4_token_enums but no
corresponding case in b4_token_defines.

* data/skeletons/c.m4 (b4_token_defines): Do define YYEMPTY.
2020-11-11 08:47:21 +01:00
Akim Demaille
3b80811cfa glr2.cc: remove scaffolding for glr.c
* data/skeletons/glr2.cc (b4_glr_cc_setup, b4_glr_cc_cleanup): Remove.
2020-11-07 17:28:25 +01:00