Akim Demaille [Mon, 28 Jan 2013 13:56:16 +0000 (14:56 +0100)]
c++: remove now-useless operators
Now that symbols behaves properly, we can eliminate special routines
that are no longer needed.
* data/c++.m4, data/glr.cc, data/lalr1.cc, data/variant.hh:
Remove useless assignment operators and copy constructors.
As a consequence, remove useless includes for "abort".
Akim Demaille [Mon, 28 Jan 2013 13:29:43 +0000 (14:29 +0100)]
c++: revamp the support for variants
The current approach was too adhoc: the symbols were not sufficiently
self-contained, in particular wrt memory management. The "new"
guideline is the one that should have been followed from the start:
let the symbols handle themslves, instead of leaving their users to
it. It was justified by the will to avoid gratuitious moves and
copies, but the current approach does not seem to be slower, yet it
will probably be simpler to adjust to support move semantics from
C++11.
The documentation says that the %parse-param are available from the
%destructor. In retrospect, that was a silly design decision, which
we can break for variants, as its a new feature. It should be phased
out for non-variants too.
* data/variant.hh: A variant never knows if it stores something or
not, it is up to its users to store this information.
Yet, in parse.assert mode, make sure the empty/filled variants
are properly used.
(b4_symbol_constructor_define_): Don't call directly the symbol
constructor, to save a useless temporary.
* data/stack.hh (push): Steal the pushed value instead of duplicating
it.
This will simplify the callers of push, who handled this "move"
approach themselves.
* data/c++.m4 (basic_symbol): Let -1, as kind, denote the fact that
a symbol is empty.
This is needed for instance when shifting the lookahead: yyla
is given as argument to "push", and its value is then moved on
the stack. But then yyla must be declared "empty" so that its
destructor won't be called.
(basic_symbol::move): New.
Move the responsibility of calling the destructor from yy_destroy
to ~basic_symbol in the case of variants.
* data/lalr1.cc (stack_symbol_type): Now a derived class from its
previous value, so that we can add a constructor from a symbol_type.
(by_state): State -1 means empty.
(yypush_): Factor, by calling one overload from the other one, and
using the new semantics of stack::push.
No longer reclaim by hand the memory from rhs symbols, since now
that we store objects with proper destructors, they will be reclaimed
automatically.
Conversely, be sure to delete yylhs.
* tests/c++.at (C++ Variant-based Symbols): New "unit" test for
symbols.
Valentin Tolmer [Fri, 25 Jan 2013 10:12:47 +0000 (11:12 +0100)]
grammar: preserve token declaration order
In a declaration %token A B, the token A is declared before B, but in %left
A B (or with %precedence or %nonassoc or %right), the token B was declared
before A (tokens were declared in reverse order).
* src/symlist.h, src/symlist.c (symbol_list_append): New.
* src/parse-gram.y: Use it instead of symbol_list_prepend.
* tests/input.at: Adjust expectations.
Akim Demaille [Fri, 25 Jan 2013 10:06:32 +0000 (11:06 +0100)]
tests: improve test group titles
* tests/local.at (AT_SETUP_STRIP): AT_SETUP does not behave properly
with new-lines in its argument.
Remove them.
Fix the handling of %define with quotes.
Akim Demaille [Fri, 25 Jan 2013 12:51:33 +0000 (13:51 +0100)]
c: no longer require stdio.h when locations are enabled
Recent changes (in 2.7) introduced a dependency on both FILE and
fprintf, which are "available" only in %debug mode. This was to
define yy_location_print_, which is used only in %debug mode by the
parser, but massively used by the test suite to output the locations
in yyerror.
Break this dependency: the test suite should define its own routines
to display the locations. Eventually Bison will provide the user with
a means to display locations, but not yet.
* data/c.m4 (b4_yy_location_print_define): Use YYFPRINTF instead of
fprintf directly.
* data/yacc.c (b4_yy_location_print_define): Invoke it only in %debug
mode, so that stdio.h is included (needed for FILE*), and YYFPRINTF
is defined.
* tests/local.at (AT_YYERROR_DECLARE, AT_YYERROR_DEFINE): Declare
and define location_print and LOCATION_PRINT.
* tests/actions.at, tests/existing.at, tests/glr-regression.at,
* tests/input.at, tests/named-refs.at, tests/regression.at: Adjust
to use them.
Fix the expected line numbers (as the prologue's length has changed).
* data/location.cc (operator<<): Display location exactly as is
done in C skeletons.
* tests/local.at (AT_LOC_PUSHDEF, AT_LOC_POPDEF): Also define
AT_FIRST_LINE, AT_LAST_LINE, AT_FIRST_COLUMN, AT_LAST_COLUMN.
* tests/actions.at (Location Print): Also check C++ skeletons.
Akim Demaille [Mon, 21 Jan 2013 15:01:53 +0000 (16:01 +0100)]
tests: generalize default main for api.namespace
* tests/local.at (AT_NAME_PREFIX): Also match api.namespace.
(AT_MAIN_DEFINE): Take it into account.
* tests/c++.at, tests/headers.at: Use AT_NAME_PREFIX.
(AT_CHECK_NAMESPACE): Rename as...
(AT_TEST): this.
Akim Demaille [Mon, 21 Jan 2013 14:38:49 +0000 (15:38 +0100)]
tests: improve factoring of the main function
* tests/local.at (AT_MAIN_DEFINE): If %debug is used, check if
-d/--debug is passed to the generated parser, and enable the traces.
Return exactly the result of yyparse, so that we can check exit code
2 too.
* tests/actions.at, tests/glr-regression.at, tests/regression.at:
Use AT_MAIN_DEFINE, helping AT_BISON_OPTION_PUSHDEFS where needed,
preferably to option -t.
There used to be a bug in some skeletons, which caused the expansion of
'yylval' and 'yylloc', generating these errors:
input.cc:547:16: error: expected ',' or '...' before '(' token
#define yylval (yystackp->yyval)
^
input.yy:29:39: note: in expansion of macro 'yylval'
int yylex (yy::parser::semantic_type *yylval)
^
This bug is fixed by 'skel: better aliasing of identifiers', but a workaround
is useful when benchmarking against older versions of Bison, which are still
affected by the bug.
* etc/bench.pl.in: Rename yylval to yylvalp and yylloc to yyllocp in base
grammar 'list'.
* data/c++.m4 (basic_symbol): Keep 'inline' in the prototypes, but don't
duplicate it in the implementation.
* data/variant.hh (variant): 'inline' is not needed when the implementation is
provided in the class definition.
* src/getargs.c (feature_flag): Here.
* tests/local.at (AT_BISON_CHECK_, AT_BISON_CHECK_NO_XML): Deactivate carets
for the testsuite, by default.
* tests/input.at: Adjust the locations for command line definitions.
* data/variant.hh (variant, operator=): Make private.
* data/c++.m4 (operator=): New, to avoid needing a definition of that operator
for each class member (such as a possible variant).
* data/glr.cc, data/lalr.cc: Add the necessary include for the abort.
A "symbol" groups together the symbol type (INT, PLUS, etc.), its
possible semantic value, and its optional location. The type is
needed to access the value, as it is stored as a variant/union.
There are two kinds of symbols. "symbol_type" are "external symbols":
they have type, value and location, and are returned by yylex.
"stack_symbol_type" are "internal symbols", they group state number,
value and location, and are stored in the parser stack. The type of
the symbol is computed from the state number.
The class template symbol_base_type<Exact> factors the code common to
stack_symbol_type and symbol_type. It uses the Curiously Recurring
Template pattern so that we can always (static_) downcast to the exact
type. symbol_base_type features value and location, and delegates the
handling of the type to its parameter.
When trying to generalize the support for variant, a significant issue
was revealed: because stack_symbol_type and symbol_type _derive_ from
symbol_base_type, the type/state member is defined _after_ the value
and location. In C++ the order of the definition of the members
defines the order in which they are initialized, things go backward:
the value is initialized _before_ the type. This is wrong, since the
type is needed to access the value.
Therefore, we need another means to factor the common code, one that
ensures the order of the members.
The idea is simple: define two (base) classes that code the symbol
type ("by_type" codes it by its type, and "by_state" by the state
number). Define basic_symbol<Base> as the class template that
provides value and location support. Make it _derive_ from its
parameter, by_type or by_state. Then define stack_symbol_type and
symbol_type as basic_symbol<by_state>, basic_symbol<by_type>. The
name basic_symbol was chosen by similarity with basic_string and
basic_ostream.
* data/c++.m4 (symbol_base_type<Exact>): Remove, replace by...
(basic_symbol<Base>): which derives from its parameter, one of...
(by_state, by_type): which provide means to retrieve the actual type of
symbol.
(symbol_type): Is now basic_symbol<by_type>.
(stack_symbol_type): Is now basic_symbol<by_state>.
* data/lalr1.cc: Many adjustments.
Akim Demaille [Mon, 31 Dec 2012 15:21:34 +0000 (16:21 +0100)]
doc: use deffn to declare the list of %define variables
* doc/bison.texi (%define Summary): Use @deffn instead of @table, it
spares a lot of width, especially in PDF, and looks nicer in the other
formats too.
It is also more consistent with the rest of the document.
Akim Demaille [Fri, 28 Dec 2012 10:25:02 +0000 (11:25 +0100)]
syncline: one line is enough
So far we were issuing two lines for each syncline change:
/* Line 356 of yacc.c */
#line 1 "src/parse-gram.y"
This is a lot of clutter, especially when reading diffs, as these
lines change often. Fuse them into a single, shorter, line:
#line 1 "src/parse-gram.y" /* yacc.c:356 */
* data/bison.m4 (b4_syncline): Issue a single line.
Comment improvements.
(b4_sync_start, b4_sync_end): Issue a shorter comment.
* data/c++.m4 (b4_semantic_type_declare): b4_user_code must be
on its own line as it might start with a "#line" directive.
Akim Demaille [Fri, 28 Dec 2012 09:04:49 +0000 (10:04 +0100)]
graph: minor simplification
* src/gram.c (print_lhs): Use %*s to indent.
* src/print_graph.c (print_lhs): Use obstack_printf.
Became simple enough to be inlined in...
(print_core): here.
Use a "rule*" instead of an index in "rules[]".
carets: properly display when no line feed is present
* src/location.c (location_caret): finish the line with one whether or not it
is present in input. Rewrite code without getline.
(cleanup_caret): Reset the caret_info global.
* bootstrap.conf: No longer require getline.
Unput was no longer used since a POSIX-compatiblity issue with Flex 2.5.31,
which has been adressed in newer versions of Flex. See this discussion:
<http://lists.gnu.org/archive/html/bug-bison/2003-04/msg00029.html>
Akim Demaille [Thu, 27 Dec 2012 09:01:58 +0000 (10:01 +0100)]
skeletons: fix comments
The commit 38de4e570fdc7c8db9633c3b2054e565d8c1c6b9 underquoted the
content of the comments, which resulted in losing square brackets in
the comments. Besides, some other invocations were underquoting the
effective arguments.
* data/c.m4 (b4_comment_): Properly quote the comment.
(b4_comment_, b4_comment): Move to...
* data/c-like.m4: here, so that...
* data/java.m4: can use it instead of its own copy.
* data/bison.m4 (b4_integral_parser_tables_map): Fix some comments.
Akim Demaille [Wed, 26 Dec 2012 16:56:20 +0000 (17:56 +0100)]
tables: scope reduction
* src/tables.c (save_column, pack_vector): Reduce the scope to
emphasize the structure of the code.
Rename the returned value "res" to make understanding easier.
Akim Demaille [Wed, 26 Dec 2012 07:18:03 +0000 (08:18 +0100)]
skeletons: no longer call yylex via a CPP macro
The YYLEX existed only to support YYLEX_PARAM, which is now removed.
This macro was a nuisance, since incorrect yylex calls where pointed
the macro _use_, instead of its definition.
* data/c.m4 (b4_lex_formals, b4_lex): New.
* data/glr.c, data/yacc.c: Use it.
* data/lalr1.cc (b4_lex): New.
Use it.
squash! skeletons: no longer call yylex via a CPP macro
Akim Demaille [Sun, 23 Dec 2012 16:54:59 +0000 (17:54 +0100)]
c: improve the definition of public types
* data/c.m4 (b4_token_enum): Improve comments.
(b4_value_type_define, b4_location_type_define): New, extracted
from...
(b4_declare_yylstype): here.
Separate the typedefs from the union/struct definitions.
Akim Demaille [Sun, 23 Dec 2012 09:52:23 +0000 (10:52 +0100)]
tests: check the "%define variant" is deprecated.
* tests/input.at: Rename some AT_SETUP to avoid that
AT_SETUP_STRIP thinks they contain %define directives.
("%define" backward compatibility): Merge tests together
to speed up the test suite, and to make maintenance easier
(multiple AT_CHECK means multiple runs of the test suite to
be sure to have updated all the error messages).
Check the "%define variant" is properly obsoleted.
Akim Demaille [Sun, 16 Dec 2012 09:20:46 +0000 (10:20 +0100)]
%define variables: support value changes in deprecation
* src/muscle-tab.c (define_directive): Be robust to "assignment"
containing '='.
(muscle_percent_variable_update): Upgrade "variant" to "api.value.type".
Support such upgrade patterns.
Adjust callers.
* data/bison.m4: Use api.value.type for variants.
* tests/c++.at: Adjust tests.
warning: deprecated directive, use '%define api.namespace toto' [-Wdeprecated]
%define namespace toto
^^^^^^^^^
This is in preparation of cases where not only the variable is
renamed, but the values are too:
warning: deprecated directive: '%define variant', use '%define api.value.type variant' [-Wdeprecated]
* src/muscle-tab.c (define_directive): New.
(muscle_percent_variable_update): Take the value as argument, and use it
in the diagnostics.
Loop with a pointer instead of an index.
* tests/input.at (%define backward compatibility): Adjust.
Akim Demaille [Sat, 22 Dec 2012 09:16:49 +0000 (10:16 +0100)]
variant: produce stable results
Improve the output by ensuring a well defined order for type switches.
* src/uniqstr.h: Style changes for macro arguments.
(UNIQSTR_CMP): Replace by...
(uniqstr_cmp): this.
* src/uniqstr.c (uniqstr_cmp): New.
Produce well defined results.
* src/output.c: Use it.
Akim Demaille [Sat, 22 Dec 2012 12:39:54 +0000 (13:39 +0100)]
tests: improve titles
* tests/local.at (AT_SETUP_STRIP): New.
(AT_SETUP): Use it to shorten the test titles: remove %defines, %language
and %skeleton whose arguments suffice.
* tests/synclines.at: Use more precise AT_SETUP.
Akim Demaille [Sat, 22 Dec 2012 08:14:05 +0000 (09:14 +0100)]
c++: use // comments in the output
This is mostly used for the license header, the synclines, and the
generated tables:
- /* STOS_[STATE-NUM] -- The (internal number of the) accessing
- symbol of state STATE-NUM. */
+ // STOS_[STATE-NUM] -- The (internal number of the) accessing
+ // symbol of state STATE-NUM.
static const unsigned char yystos_[];
* data/c.m4: Comment changes.
(b4_comment_): Expand the text argument.
Before this change, we were actually formatting M4 code as a
C comment, and then expand it.
(b4_comment): Fix the closing of comments: there is no reason to
add the (line) prefix before the closing "*/".
* data/c++.m4 (b4_comment): New.
Akim Demaille [Wed, 19 Dec 2012 09:18:03 +0000 (10:18 +0100)]
variants: prohibit simple copies
The "variant" structure provides a means to store, in a typeless way,
C++ objects. Manipulating it without provide the type of the stored
content is doomed to failure. So provide a means to copy in a type
safe way, and prohibit typeless assignments.
* data/c++.m4 (symbol_type::move): New.
* data/lalr1.cc: Use it.
* data/variant.hh (b4_variant_define): Provide variant::copy.
Let variant::operator= abort.
We cannot undefine it, yet, as it is still uses by the implicit
assigment in symbols, which must also be disabled.
Akim Demaille [Wed, 19 Dec 2012 09:09:07 +0000 (10:09 +0100)]
variant: more assertions
Equip variants with more checking code. Provide a means to request
includes.
* data/variant.hh (b4_variant_includes): New.
* data/lalr1.cc: Use it.
* data/variant.hh (variant::built): Define at the end, as a private member.
(variant::tname): New.
Somewhat makes "built" useless, but let's keep both for a start, in
case using "typeinfo" is considered unacceptable in some environments.
Fix some formatting issues.
Akim Demaille [Wed, 19 Dec 2012 10:01:52 +0000 (11:01 +0100)]
glr.cc: simplifying the handling of parse/lex params
The fact that glr.cc uses glr.c makes the handling of parse params
more complex, as the parser object of glr.cc must be passed to the
parse function of glr.c. Yet not all the functions need access to
the parser object.
* data/glr.cc (b4_parse_param_wrap): New.
Use them.