Commit Graph

7072 Commits

Author SHA1 Message Date
Akim Demaille
44ac18d136 yacc.c: yypstate_expected_tokens
In push parsers, when asking for the list of expected tokens at some
point, it makes no sense to build a yyparse_context_t: the yypstate
alone suffices (the only difference being the lookahead).  Instead of
forcing the user to build a useless shell around yypstate, let's offer
yypstate_expected_tokens.

See https://lists.gnu.org/r/bison-patches/2020-03/msg00025.html.

* data/skeletons/yacc.c (yypstate): Declare earlier, so that we can
use it for...
(yypstate_expected_tokens): this new function, when in push parsers.
Adjust dependencies.
* examples/c/bistromathic/parse.y: Simplify: use
yypstate_expected_tokens.
Style fixes.
Reduce scopes (reported by Joel E. Denny).
2020-03-17 19:20:13 +01:00
Akim Demaille
0c3dd3a669 examples: bistromathic: simplify
* examples/c/bistromathic/parse.y (expected_tokens): Remove useless "break".
2020-03-09 07:24:33 +01:00
Akim Demaille
951da960e6 merge branch 'maint'
* upstream/maint:
  maint: post-release administrivia
  version 3.5.3
  news: update for 3.5.3
  yacc.c: make sure we properly propagated the user's number for error
  diagnostics: don't crash because of repeated definitions of error
  style: initialize some struct members
  diagnostics: beware of zero-width characters
  diagnostics: be sure to close the styling when lines are too short
  muscles: fix incorrect decoding of $
  code: be robust to reference with invalid tags
  build: fix typo
  doc: update recommandation for libtextstyle
  style: comment changes
  examples: use consistently the GFDL header for readmes
  style: remove useless declarations
  typo: succesful -> successful
  README: point to tests/bison, and document --trace
  gnulib: update
  maint: post-release administrivia
2020-03-08 10:13:16 +01:00
Akim Demaille
15ea35019f maint: post-release administrivia
* NEWS: Add header line for next release.
* .prev-version: Record previous version.
* cfg.mk (old_NEWS_hash): Auto-update.
2020-03-08 08:50:10 +01:00
Akim Demaille
f49684a577 version 3.5.3
* NEWS: Record release date.
v3.5.3
2020-03-08 08:30:41 +01:00
Akim Demaille
044ad1288c news: update for 3.5.3 2020-03-08 08:17:13 +01:00
Akim Demaille
e3812bb8c3 yacc.c: make sure we properly propagated the user's number for error
* data/skeletons/yacc.c (YYERRCODE): Be truthful.
* tests/input.at (Redefining the error token): Check that.
2020-03-08 08:10:11 +01:00
Akim Demaille
cfcd823e16 diagnostics: don't crash because of repeated definitions of error
According to https://www.unix.com/man-page/POSIX/1posix/yacc/, the
user is allowed to specify her user number for the error token:

    The token error shall be reserved for error handling. The name
    error can be used in grammar rules. It indicates places where the
    parser can recover from a syntax error. The default value of error
    shall be 256. Its value can be changed using a %token
    declaration. The lexical analyzer should not return the value of
    error.

I think this feature is useless, the user should not have to deal with
that.  The intend is probably to give the user a means to use 256 if
she wants to, but provided "error" cleared the path first by being
assigned another number.  In the case of Bison, 256 is assigned to
"error" at the end if the user did not use it for a token of hers.  So
this feature is useless.

Yet it is valid, and if the user assigns twice a token number to
"error", then the second time we want to complain about it and want to
show the original definition.  At this point, we try to display the
built-in definition of "error", whose location is NULL, and we crash.

Rather, the location of the first user definition of "error" should
become its defining location.

Reported byg Ahcheong Lee.
https://lists.gnu.org/r/bug-bison/2020-03/msg00007.html

* src/symtab.c (symbol_class_set): If this is a declaration and the
symbol was not declared yet, keep this as defining location.
* tests/input.at (Redefining the error token): New.
2020-03-08 08:10:11 +01:00
Akim Demaille
2f02d9beae style: initialize some struct members
* src/symtab.c (sym_content_new): Initialize all the location members.
Not needed by the code, but disturbing values when using a debugger.
2020-03-08 08:10:11 +01:00
Akim Demaille
b638603477 diagnostics: beware of zero-width characters
Currenly we rely on (visual) width of the characters to decide where
to open and close the styling of the quoted lines.  This breaks when
we deal with zero-width characters: we cannot just rely on (visual)
columns, we need to know whether we are before, inside, or after the
highlighted portion.

* src/location.c (location_caret): col_end: no longer add 1, "regular"
characters have a width of 1, only 0-width characters have 0-width.
opened: replace with 'state', a three-valued enum.
Don't reopen the style if we already did.
* tests/diagnostics.at (Zero-width characters): New.
2020-03-08 08:10:11 +01:00
Akim Demaille
e21ff47f5d diagnostics: be sure to close the styling when lines are too short
bar.y:4.12-17: <error>error:</error> redefining user token number of foo
    -    4 | %token foo <error>123
    +    4 | %token foo <error>123</error>
           |            <error>^~~~~~</error>

* src/location.c (location_caret): Be sure to close.
* tests/diagnostics.at (Line is too short, and then you die): New.
2020-03-07 10:01:52 +01:00
Akim Demaille
b82b387da9 muscles: fix incorrect decoding of $
Bug introduced in 458171e6df.
https://lists.gnu.org/archive/html/bison-patches/2013-11/msg00009.html

Reported by Ahcheong Lee.
https://lists.gnu.org/r/bug-bison/2020-03/msg00010.html

* src/muscle-tab.c (COMMON_DECODE): "$" is coded as "$][", not "$[][".
* tests/input.at ("%define" enum variables): Check that case.
2020-03-07 07:45:10 +01:00
Akim Demaille
641e326303 code: be robust to reference with invalid tags
Because we want to support $<a->b>$, we must accept -> in type tags,
and reject $<->$, as it is unfinished.
Reported by Ahcheong Lee.

* src/scan-code.l (yylex): Make sure "tag" does not end with -, since
-> does not close the tag.
* tests/input.at (Stray $ or @): Check this.
2020-03-06 17:29:26 +01:00
Akimn Demaille
192e9fdf77 build: fix typo
* build-aux/cross-options.pl: here.
2020-03-06 08:32:26 +01:00
Akim Demaille
a4a3f08c11 doc: update recommandation for libtextstyle
* README: here.
2020-03-06 08:32:18 +01:00
Akim Demaille
666df338a7 style: comment changes
* src/symtab.h, src/lr0.c: here.
2020-03-06 08:32:03 +01:00
Akim Demaille
b437b16603 examples: use consistently the GFDL header for readmes
* examples/c++/README.md, examples/c++/calc++/README.md,
* examples/c/calc/README.md, examples/c/lexcalc/README.md,
* examples/c/reccalc/README.md:
Prefer the GFDL banner to the GPL one.
2020-03-06 08:31:34 +01:00
Akim Demaille
b493c173c9 style: remove useless declarations
* src/reader.h: Don't duplicate what parse-gram.h already exposes.
* src/lr0.h: Remove useless include.
2020-03-06 08:30:21 +01:00
Adrian Vogelsgesang
aab3feb5a1 typo: succesful -> successful
* data/skeletons/lalr1.cc: here
* etc/bench.pl.in: here
* src/location.c: and here.
2020-03-06 08:29:58 +01:00
Akim Demaille
b7942f2661 README: point to tests/bison, and document --trace
Reported by Victor Morales Cayuela.

* README, README-hacking.md: here.
2020-03-06 08:28:23 +01:00
Akim Demaille
cefb538ab0 gnulib: update 2020-03-06 08:25:52 +01:00
Akim Demaille
ecd922024e README: point to tests/bison, and document --trace
Reported by Victor Morales Cayuela.

* README, README-hacking.md: here.
2020-03-05 17:56:55 +01:00
Akim Demaille
2353ce7216 yacc.c: simplify yyparse_context_t member names
* data/skeletons/yacc.c (yyparse_context_t): Rename yyes_p and
yyes_capacity_p as...
(yyes, yyes_capacity): These.
2020-03-05 07:26:50 +01:00
Akim Demaille
9cc76ee62c yacc.c: yyerror_range does not need to be preserved accross calls
* data/skeletons/yacc.c (b4_parse_state_variable_macros): Don't define
yyerror_range.
(yyparse): Add yyerror_range as local variable.
2020-03-05 07:26:49 +01:00
Akim Demaille
2f83ef57f3 yacc.c: push: undefine the pstate macros for the epilogue
* data/skeletons/yacc.c (b4_macro_define, b4_macro_undef)
(b4_pstate_macro_define, b4_parse_state_variable_macros):
New.
Use them.
* examples/c/bistromathic/parse.y: Remove now useless undefs.
2020-03-05 07:26:49 +01:00
Akim Demaille
744171ddbf yacc.c: push: initialize the pstate variables in pstate_new
Currently pstate_new does not set up its variables, this task is left
to yypush_parse.  This was probably to share more code with usual pull
parsers, where these (local) variables are indeed initialized by
yyparse.

But as a consequence yyexpected_tokens crashes at the very beginning
of the parse, since, for instance, the stacks are not even set up.
See https://lists.gnu.org/r/bison-patches/2020-03/msg00001.html.

The fix could have very simple, but the documentation actually makes
it very clear that we can reuse a pstate for several parses:

    After yypush_parse returns a status other than YYPUSH_MORE, the
    parser instance yyps may be reused for a new parse.

so we need to restore the parser to its pristine state so that (i) it
is ready to run the next parse, (ii) it properly supports
yyexpected_tokens for the next run.

* data/skeletons/yacc.c (b4_initialize_parser_state_variables): New,
extracted from the top of yyparse/yypush_parse.
(yypstate_clear): New.
(yypstate_new): Use it when push parsers are enabled.
Define after the yyps macros so that we can use the same code as the
regular pull parsers.
(yyparse): Use it when push parsers are _not_ enabled.

* examples/c/bistromathic/bistromathic.test: Check the completion on
the beginning of the line.
2020-03-05 07:13:23 +01:00
Akim Demaille
4fd3282dd7 style: formatting changes
* data/skeletons/yacc.c, tests/torture.at: here.
2020-03-04 08:24:36 +01:00
Akim Demaille
67793793e8 bistromathic: properly compute the lcp, as expected by readline
Currently completion on "at" proposes only "atan", but does not
actually complete "at" into "atan".

* examples/c/bistromathic/parse.y (completion): Install the lcp in
matches[0].
* examples/c/bistromathic/bistromathic.test: Check that case.
2020-03-04 08:24:36 +01:00
Akim Demaille
f334775dbf bistromathic: don't require spaces after operators for completion
Currently "(1+<TAB>" does not work as expected, because "+" is not a
word breaking character.

* examples/c/bistromathic/parse.y (init_readline): Specify our word
breaking characters.
* examples/c/bistromathic/bistromathic.test: Avoid trailing spaces.
2020-03-04 08:24:35 +01:00
Akim Demaille
feb1011c8b bistromathic: check completion
* examples/c/bistromathic/bistromathic.test: here.
* examples/c/bistromathic/parse.y (expected_tokens): Fix a memory
leak.
2020-03-02 06:58:25 +01:00
Akim Demaille
99bedadf23 m4: remove b4_function_define and b4_function_declare
* data/skeletons/c.m4: here.
2020-03-02 06:58:20 +01:00
Akim Demaille
79c3f2b8fd m4: decommission b4_function_declare
* data/skeletons/glr.c, data/skeletons/glr.cc, data/skeletons/yacc.c:
Stop using b4_function_declare.
2020-03-02 06:58:14 +01:00
Akim Demaille
4cca30d2e6 m4: decommission function generating macro
These macros have been extremely useful when we had to support K&R C,
which we dropped long ago.  Now, they merely make the code uselessly
hard to read.

* data/skeletons/c.m4, data/skeletons/glr.c, data/skeletons/glr.cc,
* data/skeletons/yacc.c:
Stop using b4_function_define.
2020-03-02 06:57:50 +01:00
Akim Demaille
5789f9d91e examples: bistromathic: demonstrate the use of yyexpected_tokens
Let's use GNU readline and its TAB autocompletion to demonstrate the
use of yyexpected_tokens.

This shows a number of weaknesses in our current approach:

- some macros (yyssp, etc.) from push parsers "leak" in user code, we
  need to undefine them

- the context needed by yyexpected_tokens does not need the token,
  yypstate actually suffices

- yypstate is not properly setup when first allocated, which results
  in a crash of yyexpected_tokens if fired before a first token was
  read.  We should move initialization from yypush_parse into
  yypstate_new.

* examples/c/bistromathic/parse.y (yylex): Take input as a string, not
a file.
(EXIT): New token.
(input): Adjust to work only on a line.
(line): Remove.
(symbol_count, process_line, expected_tokens, completion)
(init_readline): New.
* examples/c/bistromathic/bistromathic.test: Adjust expectations.
2020-03-01 12:31:39 +01:00
Akim Demaille
b269a45fa4 examples: use consistently the GFDL header for readmes
* examples/c++/README.md, examples/c++/calc++/README.md,
* examples/c/calc/README.md, examples/c/lexcalc/README.md,
* examples/c/pushcalc/README.md, examples/c/reccalc/README.md:
Prefer the GFDL banner to the GPL one.
2020-03-01 07:56:02 +01:00
Akim Demaille
1478fccd23 gnulib: use readline 2020-03-01 06:23:49 +01:00
Akim Demaille
535281f0ff examples: bistromathic: don't use Flex
This example will soon use GNU readline, so its scanner should be easy
to use (concurrently) on strings, not streams.  This is not a place
where Flex shines, and anyway, these are examples of Bison, not Flex.
There's already lexcalc and reccalc that demonstrate the use of Flex.

* examples/c/bistromathic/scan.l: Remove.
* examples/c/bistromathic/parse.y (yylex): New.
Adjust dependencies.
2020-02-29 17:52:08 +01:00
Akim Demaille
c4a7e7a1ab examples: bistromathic: strengthen tests
* examples/c/bistromathic/bistromathic.test: here.
* examples/test: Be clearer on failing tests.
2020-02-29 12:22:31 +01:00
Akim Demaille
388e12ac0f examples: lexcalc: demonstrate location tracking
The bistromathic example should not use Flex, it makes it too complex.
But it was the only example to show location tracking with Flex.

* examples/c/lexcalc/lexcalc.test, examples/c/lexcalc/parse.y,
* examples/c/lexcalc/scan.l: Demonstrate location tracking as is done
in bistromathic.
2020-02-29 12:15:19 +01:00
Akim Demaille
b870d5fee4 c++: don't copy the lookahead
The current implementation of parser::context keeps a copy of the
lookahead.  This is troublesome since we support move-only types.
Besides, while GCC is happy with the current implementation, Clang
complains that the ctor it needs to build the copy of the lookahead is
not yet available.

    461. calc.at:1120: testing Calculator C++ %defines %locations parse.error=verbose %name-prefix "calc" %verbose  ...
    calc.at:1120: COLUMNS=1000; export COLUMNS;  bison --color=no -fno-caret -Wno-deprecated -o calc.cc calc.y
    calc.at:1120: $CXX $CXXFLAGS $CPPFLAGS  $LDFLAGS -o calc calc.cc calc-lex.cc calc-main.cc $LIBS
    stderr:
    In file included from calc-lex.cc:7:
    calc.hh:351:12: error: instantiation of function 'calc::parser::basic_symbol<calc::parser::by_type>::basic_symbol' required here, but no definition is available [-Werror,-Wundefined-func-template]
        struct symbol_type : basic_symbol<by_type>
               ^
    calc.hh:273:7: note: forward declaration of template entity is here
          basic_symbol (const basic_symbol& that);
          ^
    calc.hh:351:12: note: add an explicit instantiation declaration to suppress this warning if 'calc::parser::basic_symbol<calc::parser::by_type>::basic_symbol' is explicitly instantiated in another translation unit
        struct symbol_type : basic_symbol<by_type>
               ^
    1 error generated.
    In file included from calc-main.cc:7:
    calc.hh:351:12: error: instantiation of function 'calc::parser::basic_symbol<calc::parser::by_type>::basic_symbol' required here, but no definition is available [-Werror,-Wundefined-func-template]
        struct symbol_type : basic_symbol<by_type>
               ^
    calc.hh:273:7: note: forward declaration of template entity is here
          basic_symbol (const basic_symbol& that);
          ^
    calc.hh:351:12: note: add an explicit instantiation declaration to suppress this warning if 'calc::parser::basic_symbol<calc::parser::by_type>::basic_symbol' is explicitly instantiated in another translation unit
        struct symbol_type : basic_symbol<by_type>
               ^
    1 error generated.
    stdout:
    calc.at:1120: exit code was 1, expected 0
    461. calc.at:1120: 461. Calculator C++ %defines %locations parse.error=verbose %name-prefix "calc" %verbose  (calc.at:1120): FAILED (calc.at:1120)

* data/skeletons/lalr1.cc (context::yyla_): Make it a const-ref.
Move the implementation out of the declaration.
2020-02-27 21:49:56 +01:00
Akim Demaille
30d01b21e7 c++: minor fixes
Address compiler warnings such as

    warning: declaration of 'yyla' shadows a member of 'yy::parser::context' [-Wshadow]

* data/skeletons/lalr1.cc (context): Don't use the same names for
variables and members.
Use foo_ for private members, as in parser.
Also, use the + trick in array accesses to please ICC and provide it
with an int.
2020-02-27 21:31:09 +01:00
Adrian Vogelsgesang
c2cca46795 c++: add support for parse.error=custom
* data/skeletons/lalr1.cc: added support here
* tests/calc.at: added test cases
* tests/local.at: added yyreport_syntax_error implementation
   for C++ test cases
2020-02-27 18:13:44 +01:00
Adrian Vogelsgesang
879530cb95 c++: add parser::context for syntax error handling
* data/skeletons/lalr1.cc: here
2020-02-27 18:13:44 +01:00
Adrian Vogelsgesang
72acecb30c c++: add support for parse.error=detailed
* data/skeletons/lalr1.cc: added support here
* tests/calc.at: added a test case
2020-02-27 18:13:43 +01:00
Adrian Vogelsgesang
d4fcd5c3d0 skeletons: prefer b4_parse_error_{case,bmatch} over manual solution
Prefer b4_parse_error_case over the adhoc solution
`m4_case + b4_percent_define_get`. Same for b4_parse_error_bmatch.

* data/skeletons/glr.c: here
* data/skeletons/yacc.c: here
2020-02-27 18:13:43 +01:00
Adrian Vogelsgesang
368fcf0af5 typo: succesful -> successful
* data/skeletons/lalr1.cc: here
* etc/bench.pl.in: here
* src/location.c: here
* tests/calc.at: and here
2020-02-27 18:10:39 +01:00
Akim Demaille
6fe063da8f bench.pl: clean up the dust
* etc/bench.pl.in: Adjust to the current use of %define's values.
Don't use %error-verbose.
Prefer Bison to CPP (e.g., api.value.type).
Avoid returning characters directly, so that %define api.token.raw works.
2020-02-24 08:42:16 +01:00
Akim Demaille
296660304c style: comment changes
* src/symtab.h, src/lr0.c: here.
2020-02-23 08:25:53 +01:00
Akim Demaille
323731ff74 style: avoid using 'this' as an identifier
LLDB insists on parsing 'this' as a C++ keyword, even when debugging a
C program.

* src/symtab.c: Please the dictator.
2020-02-23 08:25:53 +01:00
Akim Demaille
6f7f949708 style: remove useless declarations
* src/reader.h: Don't duplicate what parse-gram.h already exposes.
* src/lr0.h: Remove useless include.
2020-02-23 08:25:53 +01:00