bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 0/7] Deprecate yysyntax_error_arguments


From: Akim Demaille
Subject: [PATCH 0/7] Deprecate yysyntax_error_arguments
Date: Sat, 21 Mar 2020 12:53:08 +0100

This series of commits explores the removal of
yysyntax_error_arguments, as Adrian suggested
(https://lists.gnu.org/archive/html/bison-patches/2020-02/msg00069.html).
I wanted to see in practice what impact it has on the user code, and
here are a few examples.

examples/c/bistromathic goes from

  int
  yyreport_syntax_error (const yyparse_context_t *ctx)
  {
    enum { ARGMAX = 10 };
    int res = 0;
    int arg[ARGMAX];
    int n = yysyntax_error_arguments (ctx, arg, ARGMAX);
    if (n < 0)
      // Forward errors to yyparse.
      res = n;
    YY_LOCATION_PRINT (stderr, *yyparse_context_location (ctx));
    fprintf (stderr, ": syntax error");
    if (n >= 0)
      {
        for (int i = 1; i < n; ++i)
          fprintf (stderr, "%s %s",
                   i == 1 ? ": expected" : " or", yysymbol_name (arg[i]));
        if (n)
          fprintf (stderr, " before %s", yysymbol_name (arg[0]));
      }
    fprintf (stderr, "\n");
    return res;
  }

to (with a slight change of semantics: 10 unexpected vs 10 in total)

  int
  yyreport_syntax_error (const yyparse_context_t *ctx)
  {
    int res = 0;
    YY_LOCATION_PRINT (stderr, *yyparse_context_location (ctx));
    fprintf (stderr, ": syntax error");
    {
      enum { TOKENMAX = 10 };
      int expected[TOKENMAX];
      int n = yyexpected_tokens (ctx, expected, TOKENMAX);
      if (n < 0)
        // Forward errors to yyparse.
        res = n;
      else
        for (int i = 0; i < n; ++i)
          fprintf (stderr, "%s %s",
                   i == 0 ? ": expected" : " or", yysymbol_name (expected[i]));
    }
    {
      int lookahead = yyparse_context_token (ctx);
      if (lookahead != YYEMPTY)
        fprintf (stderr, " before %s", yysymbol_name (lookahead));
    }
    fprintf (stderr, "\n");
    return res;
  }

Bison's own reporting function goes from

  int
  yyreport_syntax_error (const yyparse_context_t *ctx)
  {
    enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
    /* Arguments of format: reported tokens (one for the "unexpected",
       one per "expected"). */
    int arg[YYERROR_VERBOSE_ARGS_MAXIMUM];
    int n = yysyntax_error_arguments (ctx, arg, YYERROR_VERBOSE_ARGS_MAXIMUM);
    if (n == -2)
      return -2;
    const char *argv[YYERROR_VERBOSE_ARGS_MAXIMUM];
    for (int i = 0; i < n; ++i)
      argv[i] = yysymbol_name (arg[i]);
    syntax_error (*yyparse_context_location (ctx), n, argv);
    return 0;
  }

to

  int
  yyreport_syntax_error (const yyparse_context_t *ctx)
  {
    int res = 0;
    /* Arguments of format: reported tokens (one for the "unexpected",
       one per "expected"). */
    enum { ARGS_MAX = 5 };
    const char *argv[ARGS_MAX];
    int argc = 0;
    int unexpected = yyparse_context_token (ctx);
    if (unexpected != YYEMPTY)
      {
        argv[argc++] = yysymbol_name (unexpected);
        int expected[ARGS_MAX - 1];
        int nexpected = yyexpected_tokens (ctx, expected, ARGS_MAX - 1);
        if (nexpected < 0)
          res = nexpected;
        else
          for (int i = 0; i < nexpected; ++i)
            argv[argc++] = yysymbol_name (expected[i]);
      }
    syntax_error (*yyparse_context_location (ctx), argc, argv);
    return res;
  }

So, overall there is more complexity pushed onto the user.  And we
have to expose her to YYEMPTY, which was not needed before.

These routines are also complex because of the handling of errors.  It
might not be worth it: if memory is exhausted, then we will report
that anyway, and quite properly.  The only difference being that we
display "syntax error" before "memory exhausted".  Is this really
worth the extra complexity?

So I am still hesitating, and would welcome opinions.

Akim Demaille (7):
  doc: c++: promote api.token.raw
  style: reduce length of private constant
  yacc.c: use negative numbers for errors in auxiliary functions
  examples: don't use yysyntax_error_arguments
  tests: yacc.c: avoid yysyntax_error_arguments
  bison: avoid using yysyntax_error_arguments
  lalr1.cc: avoid using yysyntax_error_arguments

 NEWS                            | 26 ++++++-----
 THANKS                          |  1 +
 data/skeletons/glr.c            | 24 +++++++++--
 data/skeletons/lalr1.cc         |  9 ++--
 data/skeletons/lalr1.java       | 22 +++++++++-
 data/skeletons/yacc.c           | 59 +++++++++++++++----------
 doc/bison.texi                  | 19 +++++++--
 examples/c/bistromathic/parse.y | 30 ++++++++-----
 examples/java/calc/Calc.y       | 21 +++++----
 src/parse-gram.c                | 62 ++++++++++++++++++---------
 src/parse-gram.y                | 28 +++++++-----
 tests/local.at                  | 76 ++++++++++++++++++---------------
 12 files changed, 249 insertions(+), 128 deletions(-)

-- 
2.25.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]