[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
FYI: Adjust @$ for empty reductions
From: |
Akim Demaille |
Subject: |
FYI: Adjust @$ for empty reductions |
Date: |
Tue, 05 Oct 2004 10:37:59 +0200 |
User-agent: |
Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) |
2004-10-05 Akim Demaille <address@hidden>
In Bison 1.875's yacc.c, YYLLOC_DEFAULT was called regardless
whether the reducion was empty or not. This leaves room to
improve the use of YYLLOC_DEFAULT in such a case.
lalr1.cc is still experimental, so changing this is acceptable.
And finally, there are probably not many users who changed the
handling of locations in GLR, so changing is admissible too.
* data/glr.c, data/lalr1.cc, data/yacc.c (YYLLOC_DEFAULT): On an
empty reduction, set @$ to an empty location ending the previously
stacked symbol.
Adjust uses to make sure the code is triggered on empty
reductions.
* tests/actions.at (_AT_CHECK_PRINTER_AND_DESTRUCTOR): Adjust the
expected output: empty reductions have empty locations.
Index: data/glr.c
===================================================================
RCS file: /cvsroot/bison/bison/data/glr.c,v
retrieving revision 1.78
diff -u -u -r1.78 glr.c
--- data/glr.c 21 Sep 2004 06:59:19 -0000 1.78
+++ data/glr.c 5 Oct 2004 08:35:03 -0000
@@ -420,17 +420,31 @@
/* Error token number */
#define YYTERROR 1
-/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N]. */
+/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
+ If N is 0, then set CURRENT to the empty location which ends
+ the previous symbol: RHS[0] (always defined). */
]b4_location_if([[
#define YYRHSLOC(yyRhs,YYK) ((yyRhs)[YYK].yystate.yyloc)
#ifndef YYLLOC_DEFAULT
-# define YYLLOC_DEFAULT(yyCurrent, yyRhs, YYN) \
- ((yyCurrent).first_line = YYRHSLOC(yyRhs, 1).first_line, \
- (yyCurrent).first_column = YYRHSLOC(yyRhs, 1).first_column, \
- (yyCurrent).last_line = YYRHSLOC(yyRhs, YYN).last_line, \
- (yyCurrent).last_column = YYRHSLOC(yyRhs, YYN).last_column)
+# define YYLLOC_DEFAULT(yyCurrent, yyRhs, YYN) \
+do { \
+ if (YYN) \
+ { \
+ (yyCurrent).first_line = YYRHSLOC(yyRhs, 1).first_line;
\
+ (yyCurrent).first_column = YYRHSLOC(yyRhs, 1).first_column; \
+ (yyCurrent).last_line = YYRHSLOC(yyRhs, YYN).last_line; \
+ (yyCurrent).last_column = YYRHSLOC(yyRhs, YYN).last_column; \
+ } \
+ else \
+ { \
+ (yyCurrent).first_line = YYRHSLOC(yyRhs, 0).last_line; \
+ (yyCurrent).first_column = YYRHSLOC(yyRhs, 0).last_column; \
+ (yyCurrent).last_line = YYRHSLOC(yyRhs, 0).last_line; \
+ (yyCurrent).last_column = YYRHSLOC(yyRhs, 0).last_column; \
+ } \
+} while(0)
/* YY_LOCATION_PRINT -- Print the location on the stream.
This macro was not mandated originally: define only if we know
@@ -718,15 +732,10 @@
yylow = 1;
if (yyrhslen == 0)
- {
- *yyvalp = yyval_default;
- *yylocp = yyvsp[0].yystate.yyloc;
- }
+ *yyvalp = yyval_default;
else
- {
- *yyvalp = yyvsp[YYFILL (1-yyrhslen)].yystate.yysemantics.yysval;
- YYLLOC_DEFAULT (*yylocp, yyvsp - yyrhslen, yyrhslen);
- }
+ *yyvalp = yyvsp[YYFILL (1-yyrhslen)].yystate.yysemantics.yysval;
+ YYLLOC_DEFAULT (*yylocp, yyvsp - yyrhslen, yyrhslen);
]
switch (yyn)
Index: data/lalr1.cc
===================================================================
RCS file: /cvsroot/bison/bison/data/lalr1.cc,v
retrieving revision 1.60
diff -u -u -r1.60 lalr1.cc
--- data/lalr1.cc 29 Sep 2004 12:49:50 -0000 1.60
+++ data/lalr1.cc 5 Oct 2004 08:35:03 -0000
@@ -181,13 +181,22 @@
]/* Line __line__ of lalr1.cc. */
b4_syncline(address@hidden@], address@hidden@])[
-/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N]. */
+/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
+ If N is 0, then set CURRENT to the empty location which ends
+ the previous symbol: RHS[0] (always defined). */
#ifndef YYLLOC_DEFAULT
-# define YYLLOC_DEFAULT(Current, Rhs, N) \
-do { \
- ((Current).begin = (Rhs)[1].begin); \
- ((Current).end = (Rhs)[N].end); \
+# define YYLLOC_DEFAULT(Current, Rhs, N) \
+do { \
+ if (N) \
+ { \
+ (Current).begin = (Rhs)[1].begin; \
+ (Current).end = (Rhs)[N].end; \
+ } \
+ else \
+ { \
+ (Current).begin = (Current).end = (Rhs)[0].end; \
+ } \
} while (0)
#endif
@@ -583,21 +592,14 @@
This behavior is undocumented and Bison
users should not rely upon it. */
if (len_)
- {
- yyval = semantic_stack_[len_ - 1];
- yyloc = location_stack_[len_ - 1];
- }
+ yyval = semantic_stack_[len_ - 1];
else
- {
- yyval = semantic_stack_[0];
- yyloc = location_stack_[0];
- }
+ yyval = semantic_stack_[0];
- if (len_)
- {
- Slice<LocationType, LocationStack> slice (location_stack_, len_);
- YYLLOC_DEFAULT (yyloc, slice, len_);
- }
+ {
+ Slice<LocationType, LocationStack> slice (location_stack_, len_);
+ YYLLOC_DEFAULT (yyloc, slice, len_);
+ }
YY_REDUCE_PRINT (n_);
switch (n_)
{
Index: data/yacc.c
===================================================================
RCS file: /cvsroot/bison/bison/data/yacc.c,v
retrieving revision 1.73
diff -u -u -r1.73 yacc.c
--- data/yacc.c 27 Sep 2004 07:47:47 -0000 1.73
+++ data/yacc.c 5 Oct 2004 08:35:03 -0000
@@ -506,14 +506,26 @@
#define YYERRCODE 256
-/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N]. */
+/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
+ If N is 0, then set CURRENT to the empty location which ends
+ the previous symbol: RHS[0] (always defined). */
#ifndef YYLLOC_DEFAULT
-# define YYLLOC_DEFAULT(Current, Rhs, N) \
- ((Current).first_line = (Rhs)[1].first_line, \
- (Current).first_column = (Rhs)[1].first_column, \
- (Current).last_line = (Rhs)[N].last_line, \
- (Current).last_column = (Rhs)[N].last_column)
+# define YYLLOC_DEFAULT(Current, Rhs, N) \
+do { \
+ if (N) \
+ { \
+ (Current).first_line = (Rhs)[1].first_line; \
+ (Current).first_column = (Rhs)[1].first_column; \
+ (Current).last_line = (Rhs)[N].last_line; \
+ (Current).last_column = (Rhs)[N].last_column; \
+ } \
+ else \
+ { \
+ (Current).first_line = (Current).last_line = (Rhs)[0].last_line; \
+ (Current).first_column = (Current).last_column = (Rhs)[0].last_column; \
+ } \
+} while(0)
#endif
@@ -1021,10 +1033,7 @@
]b4_location_if(
[[ /* Default location. */
- if (yylen)
- YYLLOC_DEFAULT (yyloc, yylsp - yylen, yylen);
- else
- yyloc = yylsp[0];]])[
+ YYLLOC_DEFAULT (yyloc, yylsp - yylen, yylen);]])[
YY_REDUCE_PRINT (yyn);
switch (yyn)
]{
Index: tests/actions.at
===================================================================
RCS file: /cvsroot/bison/bison/tests/actions.at,v
retrieving revision 1.38
diff -u -u -r1.38 actions.at
--- tests/actions.at 20 Sep 2004 15:25:25 -0000 1.38
+++ tests/actions.at 5 Oct 2004 08:35:03 -0000
@@ -372,8 +372,8 @@
sending: ')' (address@hidden)
line (address@hidden): '(' (address@hidden) thing (address@hidden) ')'
(address@hidden)
sending: EOF (address@hidden)
-input (address@hidden): /* Nothing */
-input (address@hidden): line (address@hidden) input (address@hidden)
+input (address@hidden): /* Nothing */
+input (address@hidden): line (address@hidden) input (address@hidden)
Successful parse.
]])
@@ -390,8 +390,8 @@
sending: ')' (address@hidden)
line (address@hidden): '(' (address@hidden) error (@10-19) ')' (address@hidden)
sending: EOF (address@hidden)
-input (address@hidden): /* Nothing */
-input (address@hidden): line (address@hidden) input (address@hidden)
+input (address@hidden): /* Nothing */
+input (address@hidden): line (address@hidden) input (address@hidden)
Successful parse.
]])
@@ -438,8 +438,8 @@
sending: ')' (address@hidden)
line (address@hidden): '(' (address@hidden) thing (address@hidden) ')'
(address@hidden)
sending: 'y' (address@hidden)
-input (address@hidden): /* Nothing */
-input (address@hidden): line (address@hidden) input (address@hidden)
+input (address@hidden): /* Nothing */
+input (address@hidden): line (address@hidden) input (address@hidden)
input (address@hidden): line (address@hidden) input (address@hidden)
input (address@hidden): line (address@hidden) input (address@hidden)
130-139: syntax error, unexpected 'y', expecting $end
- FYI: Adjust @$ for empty reductions,
Akim Demaille <=