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.
This commit is contained in:
Akim Demaille
2021-01-06 19:22:18 +01:00
parent dc8b16424a
commit f859462658
6 changed files with 50 additions and 32 deletions

View File

@@ -555,7 +555,7 @@ m4_defn([b4_actions_])[]dnl
break; break;
}dnl }dnl
], ],
[YY_USE (m4_default([$2], [yykind]));])dnl [b4_use(m4_default([$2], [yykind]));])dnl
m4_popdef([b4_actions_])dnl m4_popdef([b4_actions_])dnl
]) ])

View File

@@ -163,13 +163,20 @@ m4_popdef([$2])dnl
m4_popdef([$1])dnl m4_popdef([$1])dnl
])]) ])])
# b4_use(EXPR)
# ------------
# Pacify the compiler about some maybe unused value.
m4_define([b4_use],
[YY_USE ($1)])
# b4_parse_param_use([VAL], [LOC]) # b4_parse_param_use([VAL], [LOC])
# -------------------------------- # --------------------------------
# 'YY_USE' VAL, LOC if locations are enabled, and all the parse-params. # 'YY_USE' VAL, LOC if locations are enabled, and all the parse-params.
m4_define([b4_parse_param_use], m4_define([b4_parse_param_use],
[m4_ifvaln([$1], [ YY_USE ([$1]);])dnl [m4_ifvaln([$1], [ b4_use([$1]);])dnl
b4_locations_if([m4_ifvaln([$2], [ YY_USE ([$2]);])])dnl b4_locations_if([m4_ifvaln([$2], [ b4_use([$2]);])])dnl
b4_parse_param_for([Decl], [Formal], [ YY_USE (Formal); b4_parse_param_for([Decl], [Formal], [ b4_use(Formal);
])dnl ])dnl
]) ])

View File

@@ -18,24 +18,33 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# _b4_comment(TEXT, OPEN, CONTINUE, END) m4_include(b4_skeletonsdir/[c-like.m4])
# --------------------------------------
# Put TEXT in comment. Avoid trailing spaces: don't indent empty lines.
# Avoid adding indentation to the first line, as the indentation comes
# from OPEN. That's why we don't patsubst([$1], [^\(.\)], [ \1]).
#
# Prefix all the output lines with PREFIX.
m4_define([_b4_comment],
[$2[]m4_bpatsubst(m4_expand([[$1]]), [
\(.\)], [
$3\1])$4])
# b4_comment(TEXT, [PREFIX]) # b4_symbol_action(SYMBOL-NUM, ACTION)
# -------------------------- # ------------------------------------
# Put TEXT in comment. Prefix all the output lines with PREFIX. # Run the action ACTION ("destructor" or "printer") for SYMBOL-NUM.
m4_define([b4_comment], m4_define([b4_symbol_action],
[_b4_comment([$1], [$2/* ], [$2 ], [ */])]) [b4_symbol_if([$1], [has_$2],
[b4_dollar_pushdef([yyval],
[$1],
[],
[yyloc])dnl
_b4_symbol_case([$1])[]dnl
b4_syncline([b4_symbol([$1], [$2_line])], [b4_symbol([$1], [$2_file])])dnl
b4_symbol([$1], [$2])
b4_syncline([@oline@], [@ofile@])dnl
break;
b4_dollar_popdef[]dnl
])])
# b4_use(EXPR)
# ------------
# Pacify the compiler about some maybe unused value.
m4_define([b4_use],
[])
# b4_sync_start(LINE, FILE) # b4_sync_start(LINE, FILE)

View File

@@ -399,20 +399,17 @@ b4_user_union_members
`--------------------------------*/ `--------------------------------*/
private final void yy_symbol_print (string s, SymbolKind yykind, private final void yy_symbol_print (string s, SymbolKind yykind,
ref Value yyvaluep]dnl ref Value yyval]b4_locations_if([, ref Location yyloc])[)
b4_locations_if([, ref Location yylocationp])[)
{ {
if (0 < yydebug) if (0 < yydebug)
{ {
string message = s ~ (yykind < yyntokens_ ? " token " : " nterm ") File yyo = yyDebugStream;
~ format("%s", yykind) ~ " ("]b4_locations_if([ yyo.write(s);
~ yylocationp.toString() ~ ": "])[; yyo.write(yykind < yyntokens_ ? " token " : " nterm ");
static if (__traits(compiles, message ~= yyvaluep.toString())) yyo.write(format("%s", yykind));
message ~= yyvaluep.toString(); yyo.write(" ("]b4_locations_if([ ~ yyloc.toString() ~ ": "])[);
else ]b4_symbol_actions([printer])[
message ~= format("%s", &yyvaluep); yyo.write(")\n");
message ~= ")";
yycdebugln (message);
} }
} }
]])[ ]])[

View File

@@ -21,6 +21,7 @@
%define api.parser.class {Calc} %define api.parser.class {Calc}
%define parse.error verbose %define parse.error verbose
%define parse.trace
%locations %locations
@@ -38,6 +39,7 @@
EOL "end of line" EOL "end of line"
%token <ival> NUM "number" %token <ival> NUM "number"
%type <ival> exp %type <ival> exp
%printer { yyo.write($$); } <ival>
%left "-" "+" %left "-" "+"
%left "*" "/" %left "*" "/"
@@ -168,6 +170,9 @@ int main()
{ {
auto l = calcLexer(stdin); auto l = calcLexer(stdin);
auto p = new Calc(l); auto p = new Calc(l);
import core.stdc.stdlib : getenv;
if (getenv("YYDEBUG"))
p.setDebugLevel(1);
p.parse(); p.parse();
return l.exit_status; return l.exit_status;
} }

View File

@@ -652,7 +652,7 @@ m4_define([_AT_DATA_CALC_Y(d)],
{ {
semantic_value ival; semantic_value ival;
}; };
%printer { fprintf (yyo, "%d", $$); } <ival>; %printer { yyo.write($$); } <ival>;
%code { %code {
]AT_TOKEN_TRANSLATE_IF([[ ]AT_TOKEN_TRANSLATE_IF([[