[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 4/9] glr2.cc: fix pointer arithmethics
From: |
Akim Demaille |
Subject: |
[PATCH 4/9] glr2.cc: fix pointer arithmethics |
Date: |
Sun, 13 Dec 2020 08:49:22 +0100 |
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.
---
TODO | 3 +++
data/skeletons/glr2.cc | 14 +++++++++++---
examples/c++/glr/c++-types.test | 2 --
tests/glr-regression.at | 1 -
4 files changed, 14 insertions(+), 6 deletions(-)
diff --git a/TODO b/TODO
index e1bcb8d1..b857199a 100644
--- a/TODO
+++ b/TODO
@@ -202,6 +202,9 @@ parser::location_type. Undefine YYSTYPE and YYLTYPE.
Currently all the assertions are enabled. Once we are confident in glr2.cc,
let parse.assert use the same approach as in lalr1.cc.
+*** debug_stream
+Stop using std::cerr everywhere.
+
*** glr.c
When glr2.cc fully replaces glr.cc, get rid of the glr.cc scaffolding in
glr.c.
diff --git a/data/skeletons/glr2.cc b/data/skeletons/glr2.cc
index 45c8ddbf..321a0b5c 100644
--- a/data/skeletons/glr2.cc
+++ b/data/skeletons/glr2.cc
@@ -877,6 +877,14 @@ public:
static glr_stack_item* asItem(T* state) {
return reinterpret_cast<glr_stack_item*>(state);
}
+ static const char *as_pointer_ (const glr_state *state)
+ {
+ return reinterpret_cast<const char *>(state);
+ }
+ static char *as_pointer_ (glr_state *state)
+ {
+ return reinterpret_cast<char *>(state);
+ }
/** Preceding state in this stack */
std::ptrdiff_t yypred;
union {
@@ -1285,7 +1293,7 @@ glr_state* glr_state::pred ()
{]b4_parse_assert_if([[
check_ ();]])[
YY_IGNORE_NULL_DEREFERENCE_BEGIN
- return yypred ? &(asItem (this) - yypred)->getState () : YY_NULLPTR;
+ return yypred ? &asItem (as_pointer_ (this) - yypred)->getState () :
YY_NULLPTR;
YY_IGNORE_NULL_DEREFERENCE_END
}
@@ -1293,14 +1301,14 @@ const glr_state *glr_state::pred () const
{]b4_parse_assert_if([[
check_ ();]])[
YY_IGNORE_NULL_DEREFERENCE_BEGIN
- return yypred ? &(asItem (this) - yypred)->getState () : YY_NULLPTR;
+ return yypred ? &asItem (as_pointer_ (this) - yypred)->getState () :
YY_NULLPTR;
YY_IGNORE_NULL_DEREFERENCE_END
}
void glr_state::setPred (const glr_state *state)
{]b4_parse_assert_if([[
check_ ();]])[
- yypred = state ? asItem(this) - asItem(state) : 0;
+ yypred = state ? as_pointer_ (this) - as_pointer_ (state) : 0;
}
semantic_option *glr_state::firstVal ()
diff --git a/examples/c++/glr/c++-types.test b/examples/c++/glr/c++-types.test
index 6f55c8ab..a814d363 100644
--- a/examples/c++/glr/c++-types.test
+++ b/examples/c++/glr/c++-types.test
@@ -30,8 +30,6 @@ run 0 "\
5.0-6: <init-declare>(T, x, y)
7.0-4: =(x, y)"
-exit 77
-
cat >input <<EOF
T (x) + y;
diff --git a/tests/glr-regression.at b/tests/glr-regression.at
index e0d61958..55075f80 100644
--- a/tests/glr-regression.at
+++ b/tests/glr-regression.at
@@ -419,7 +419,6 @@ m4_pushdef([AT_TEST],
[AT_SETUP([Duplicate representation of merged trees: $1])
AT_BISON_OPTION_PUSHDEFS([%glr-parser $1])
-AT_GLR2_CC_IF([AT_XFAIL_IF([true])])
AT_DATA_GRAMMAR([glr-regr4.y],
[[
--
2.29.2
- [PATCH 0/9] glr2.cc: fixes, Akim Demaille, 2020/12/13
- [PATCH 1/9] glr2.cc: add sanity checks in glr_stack_item, Akim Demaille, 2020/12/13
- [PATCH 3/9] glr2.cc: style fixes, Akim Demaille, 2020/12/13
- [PATCH 2/9] glr2.cc: add sanity check in glr_state, Akim Demaille, 2020/12/13
- [PATCH 4/9] glr2.cc: fix pointer arithmethics,
Akim Demaille <=
- [PATCH 5/9] glr2.cc: fix yycompressStack, Akim Demaille, 2020/12/13
- [PATCH 6/9] glr2.cc: being pure is not an option, Akim Demaille, 2020/12/13
- [PATCH 7/9] glr2.cc: make yyparse a member function, Akim Demaille, 2020/12/13
- [PATCH 8/9] glr2.cc: make the example more C++, Akim Demaille, 2020/12/13
- [PATCH 9/9] glr.c: comment changes, Akim Demaille, 2020/12/13