Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: simon.marchi@polymtl.ca
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH 05/17] gdb: let the parser and lexer generators prefix their symbols
Date: Fri,  4 Sep 2026 12:56:37 -0400	[thread overview]
Message-ID: <20260904170338.1643894-6-simon.marchi@polymtl.ca> (raw)
In-Reply-To: <20260904170338.1643894-1-simon.marchi@polymtl.ca>

From: Simon Marchi <simon.marchi@efficios.com>

In order for parsers to be able to co-exist and avoid symbol clashes, they
all include yy-remap.h to rename their symbols to something unique.

Bison and byacc both support doing this natively though, with the `-p`
flag.  flex also has a similar option `-P`.  I think we can get rid of
`yy-remap.h` in favor of using these options.

It's not totally clear to me if "byacc" is a single program or if there are
many variants of "byacc" maintained by various projects, but here are those
I found, which support `-p`:

 - https://invisible-island.net/byacc/manpage/yacc.html
 - https://man.openbsd.org/yacc
 - https://man.freebsd.org/cgi/man.cgi?query=byacc&apropos=0&sektion=1&manpath=FreeBSD+15.1-RELEASE&format=html
 - https://man.netbsd.org/yacc.1

AIX yacc supports it:

  https://www.ibm.com/docs/en/aix/7.3.0?topic=y-yacc-command

Solaris yacc supports it:

  https://docs.oracle.com/cd/E88353_01/html/E37839/yacc-1.html

And in any case, it's always possible to just use GNU bison.

For flex, it appears that the various BSDs just use plain flex, so they
support `-P`.  However, it looks like the AIX and Solaris lex don't
support it:

 - https://www.ibm.com/docs/en/aix/7.3.0?topic=l-lex-command
 - https://docs.oracle.com/cd/E88353_01/html/E37839/lex-1.html

It's not clear to me if we already strictly required flex or not.  We
don't use AC_PROG_LEX, and gdb/Makefile.in hardcodes:

  FLEX = flex

It's possible to override it with

  $ make FLEX=lex

but I don't know if that is really a supported use case.  I would like
feedback from the AIX and Solaris maintainers to know whether using flex
to build GDB is ok or not (you might already do it).  Remember that it's
only necessary when building from git, as generated parser/lexer files
are distributed in tarballs.

So, this patch removes yy-remap.h, and then updates gdb/Makefile.in to pass
a unique `-p` value to all processed .y files.  This is done by defining
variables based on the files base names:

    ...
    YY_PREFIX_c-exp = c_yy
    YY_PREFIX_cp-name-parser = cpnameyy
    YY_PREFIX_d-exp = d_yy
    ...

and then accessing them like this:

    -p $(YY_PREFIX_$*)

And likewise for flex.

yy-remap.h did one more thing besides the renaming: it defaulted YYDEBUG to
1, to enable the parsers' tracing support.  Pass `-t` to $(YACC), which
should have the same effect.  Note that the `-t` passed to lex means
something else (output to stdout).

I had Claude check the symbols in the generated objects before and after
the patch, for both bison and byacc (the Arch Linux package), and this is
the result:

    Verified by comparing the symbols exported by each generated parser's .o
    file before and after this patch, with nm --defined-only --extern-only,
    using both bison (3.8.2) and byacc (20260126):

     - For the seven pure parsers (c, cp-name, d, f, go, m2, p), the
       exported symbols are identical before and after, with both
       generators.  Replacing the yy-remap.h #defines with the generator's
       -p flag changes nothing in the emitted interface.

     - ada-exp is the only one that differs, and only in the lexer symbols:
       25 flex symbols that used to be exported as bare globals (yyin,
       yyout, yyleng, yylineno, yy_flex_debug, yyalloc, yyfree, yyxrealloc,
       yy_scan_*, yyget_*/yyset_*, yy_flush_buffer, yy{push,pop}_buffer_state,
       yylex_destroy) are now prefixed with ada_.  The old hand-written block
       only remapped seven flex symbols; -P covers them all.  Same result
       with both generators.

    No exported symbol with a bare "yy" prefix remains in any parser.  byacc
    additionally exports yyval and yyerrflag, but prefixed (e.g. c_yyval),
    so they still cannot collide.

Change-Id: Ib46ff232722c686072ece4fdbf4f70737e10d8c1
---
 gdb/Makefile.in      | 18 +++++++--
 gdb/ada-exp.y        | 19 ---------
 gdb/c-exp.y          |  5 ---
 gdb/cp-name-parser.y |  3 --
 gdb/d-exp.y          |  5 ---
 gdb/f-exp.y          |  5 ---
 gdb/go-exp.y         |  5 ---
 gdb/m2-exp.y         |  5 ---
 gdb/p-exp.y          |  5 ---
 gdb/yy-remap.h       | 96 --------------------------------------------
 10 files changed, 15 insertions(+), 151 deletions(-)
 delete mode 100644 gdb/yy-remap.h

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 80b2f8573294..fdfefbff1f6c 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -1741,7 +1741,6 @@ HFILES_NO_SRCDIR = \
 	xml-syscall.h \
 	xml-tdesc.h \
 	xtensa-tdep.h \
-	yy-remap.h \
 	z80-tdep.h
 
 # Header files that already have srcdir in them, or which are in objdir.
@@ -2698,14 +2697,27 @@ po/$(PACKAGE).pot: force
 POST_PROCESS_PARSER_OUTPUT_SH = $(srcdir)/post-process-parser-output.sh
 POST_PROCESS_PARSER_OUTPUT = $(SHELL) $(POST_PROCESS_PARSER_OUTPUT_SH)
 
+# The prefix to give to the symbols each parser or lexer generator
+# produces, so that they can coexist in the same program.  There is no
+# rule tying it to the file name, so spell it out for each one.
+YY_PREFIX_ada-exp = ada_yy
+YY_PREFIX_ada-lex = ada_yy
+YY_PREFIX_c-exp = c_yy
+YY_PREFIX_cp-name-parser = cpnameyy
+YY_PREFIX_d-exp = d_yy
+YY_PREFIX_f-exp = f_yy
+YY_PREFIX_go-exp = go_yy
+YY_PREFIX_m2-exp = m2_yy
+YY_PREFIX_p-exp = pascal_yy
+
 %-gen.c: %.y $(POST_PROCESS_PARSER_OUTPUT_SH)
 	$(ECHO_YACC) $(SHELL) $(YLWRAP) $< y.tab.c $@.tmp -- \
-		$(YACC) $(YFLAGS) || (rm -f $@.tmp; false)
+		$(YACC) $(YFLAGS) -p $(YY_PREFIX_$*) -t || (rm -f $@.tmp; false)
 	@$(POST_PROCESS_PARSER_OUTPUT) bison $* < $@.tmp > $@.new && \
 	  rm -f $@.tmp && \
 	  mv $@.new $@
 %-gen.c: %.l $(POST_PROCESS_PARSER_OUTPUT_SH)
-	$(ECHO_LEX) $(FLEX) -t $< > $@.tmp || (rm -f $@.tmp; false)
+	$(ECHO_LEX) $(FLEX) -t -P $(YY_PREFIX_$*) $< > $@.tmp || (rm -f $@.tmp; false)
 	@$(POST_PROCESS_PARSER_OUTPUT) flex $* < $@.tmp > $@.new && \
 	  rm -f $@.tmp && \
 	  mv $@.new $@
diff --git a/gdb/ada-exp.y b/gdb/ada-exp.y
index bffe94a8de9b..433293d22ad9 100644
--- a/gdb/ada-exp.y
+++ b/gdb/ada-exp.y
@@ -46,11 +46,6 @@
 #include "ada-exp.h"
 #include "cli/cli-style.h"
 
-/* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
-   etc).  */
-#define GDB_YY_REMAP_PREFIX ada_
-#include "yy-remap.h"
-
 /* The state of the parser, used internally when we are parsing the
    expression.  */
 
@@ -1237,20 +1232,6 @@ primary	:	'*' primary		%prec '.'
 /* yylex defined in ada-lex-gen.c: Reads one token, getting characters */
 /* through lexptr.  */
 
-/* Remap normal flex interface names (yylex) as well as gratuitously */
-/* global symbol names, so we can have multiple flex-generated parsers */
-/* in gdb.  */
-
-/* (See note above on previous definitions for YACC.) */
-
-#define yy_create_buffer ada_yy_create_buffer
-#define yy_delete_buffer ada_yy_delete_buffer
-#define yy_init_buffer ada_yy_init_buffer
-#define yy_load_buffer_state ada_yy_load_buffer_state
-#define yy_switch_to_buffer ada_yy_switch_to_buffer
-#define yyrestart ada_yyrestart
-#define yytext ada_yytext
-
 /* The following kludge was found necessary to prevent conflicts between */
 /* defs.h and non-standard stdlib.h files.  */
 #define qsort __qsort__dummy
diff --git a/gdb/c-exp.y b/gdb/c-exp.y
index d58bce833551..9a1ecb3e6d3a 100644
--- a/gdb/c-exp.y
+++ b/gdb/c-exp.y
@@ -54,11 +54,6 @@
 #include "macroexp.h"
 #include "cli/cli-style.h"
 
-/* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
-   etc).  */
-#define GDB_YY_REMAP_PREFIX c_
-#include "yy-remap.h"
-
 /* The state of the parser, used internally when we are parsing the
    expression.  */
 
diff --git a/gdb/cp-name-parser.y b/gdb/cp-name-parser.y
index 1fd4a17db06a..44ef1a41eb39 100644
--- a/gdb/cp-name-parser.y
+++ b/gdb/cp-name-parser.y
@@ -45,9 +45,6 @@
 #include "parser-defs.h"
 #include "gdbsupport/selftest.h"
 
-#define GDB_YY_REMAP_PREFIX cpname
-#include "yy-remap.h"
-
 %}
 
 %union
diff --git a/gdb/d-exp.y b/gdb/d-exp.y
index d3263ba4fd72..3b3a212cd741 100644
--- a/gdb/d-exp.y
+++ b/gdb/d-exp.y
@@ -50,11 +50,6 @@
 #include "expop.h"
 #include "cli/cli-style.h"
 
-/* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
-   etc).  */
-#define GDB_YY_REMAP_PREFIX d_
-#include "yy-remap.h"
-
 /* The state of the parser, used internally when we are parsing the
    expression.  */
 
diff --git a/gdb/f-exp.y b/gdb/f-exp.y
index 4cdfcc973923..ad1a9253d7f9 100644
--- a/gdb/f-exp.y
+++ b/gdb/f-exp.y
@@ -52,11 +52,6 @@
 #include "type-stack.h"
 #include "f-exp.h"
 
-/* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
-   etc).  */
-#define GDB_YY_REMAP_PREFIX f_
-#include "yy-remap.h"
-
 /* The state of the parser, used internally when we are parsing the
    expression.  */
 
diff --git a/gdb/go-exp.y b/gdb/go-exp.y
index 199da6dc5c37..1384c3ebeeec 100644
--- a/gdb/go-exp.y
+++ b/gdb/go-exp.y
@@ -61,11 +61,6 @@
 #include "block.h"
 #include "expop.h"
 
-/* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
-   etc).  */
-#define GDB_YY_REMAP_PREFIX go_
-#include "yy-remap.h"
-
 /* The state of the parser, used internally when we are parsing the
    expression.  */
 
diff --git a/gdb/m2-exp.y b/gdb/m2-exp.y
index 0889054f1f37..fae0f86ffdae 100644
--- a/gdb/m2-exp.y
+++ b/gdb/m2-exp.y
@@ -45,11 +45,6 @@
 #include "block.h"
 #include "m2-exp.h"
 
-/* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
-   etc).  */
-#define GDB_YY_REMAP_PREFIX m2_
-#include "yy-remap.h"
-
 /* The state of the parser, used internally when we are parsing the
    expression.  */
 
diff --git a/gdb/p-exp.y b/gdb/p-exp.y
index 70c74beae438..dd4ae2cb209b 100644
--- a/gdb/p-exp.y
+++ b/gdb/p-exp.y
@@ -51,11 +51,6 @@
 #include "block.h"
 #include "expop.h"
 
-/* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
-   etc).  */
-#define GDB_YY_REMAP_PREFIX pascal_
-#include "yy-remap.h"
-
 /* The state of the parser, used internally when we are parsing the
    expression.  */
 
diff --git a/gdb/yy-remap.h b/gdb/yy-remap.h
deleted file mode 100644
index 0cadf6082fd5..000000000000
--- a/gdb/yy-remap.h
+++ /dev/null
@@ -1,96 +0,0 @@
-/* Copyright (C) 1986-2026 Free Software Foundation, Inc.
-
-   This file is part of GDB.
-
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 3 of the License, or
-   (at your option) any later version.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-   GNU General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
-
-#ifndef GDB_YY_REMAP_H
-#define GDB_YY_REMAP_H
-
-/* Remap normal yacc parser interface names (yyparse, yylex, yyerror,
-   etc), as well as gratuitously global symbol names, so we can have
-   multiple yacc generated parsers in gdb.  Note that these are only
-   the variables produced by yacc.  If other parser generators (bison,
-   byacc, etc) produce additional global names that conflict at link
-   time, then those parser generators need to be fixed instead of
-   adding those names to this list.  */
-
-/* NOTE: This is clumsy since BISON and FLEX provide --prefix options.
-   We are maintaining it to accommodate systems without BISON.  */
-
-/* Define GDB_YY_REMAP_PREFIX to the desired remapping prefix before
-   including this file.  */
-#ifndef GDB_YY_REMAP_PREFIX
-# error "GDB_YY_REMAP_PREFIX not defined"
-#endif
-
-#define GDB_YY_REMAP_2(PREFIX, YYSYM) PREFIX ## YYSYM
-#define GDB_YY_REMAP_1(PREFIX, YYSYM) GDB_YY_REMAP_2 (PREFIX, YYSYM)
-#define GDB_YY_REMAP(YYSYM) GDB_YY_REMAP_1 (GDB_YY_REMAP_PREFIX, YYSYM)
-
-#define yymaxdepth	GDB_YY_REMAP (yymaxdepth)
-#define yyparse		GDB_YY_REMAP (yyparse)
-#define yylex		GDB_YY_REMAP (yylex)
-#define yyerror		GDB_YY_REMAP (yyerror)
-#define yylval		GDB_YY_REMAP (yylval)
-#define yychar		GDB_YY_REMAP (yychar)
-#define yydebug		GDB_YY_REMAP (yydebug)
-#define yypact		GDB_YY_REMAP (yypact)
-#define yyr1		GDB_YY_REMAP (yyr1)
-#define yyr2		GDB_YY_REMAP (yyr2)
-#define yydef		GDB_YY_REMAP (yydef)
-#define yychk		GDB_YY_REMAP (yychk)
-#define yypgo		GDB_YY_REMAP (yypgo)
-#define yyact		GDB_YY_REMAP (yyact)
-#define yyexca		GDB_YY_REMAP (yyexca)
-#define yyerrflag	GDB_YY_REMAP (yyerrflag)
-#define yynerrs		GDB_YY_REMAP (yynerrs)
-#define yyps		GDB_YY_REMAP (yyps)
-#define yypv		GDB_YY_REMAP (yypv)
-#define yys		GDB_YY_REMAP (yys)
-#define yy_yys		GDB_YY_REMAP (yy_yys)
-#define yystate		GDB_YY_REMAP (yystate)
-#define yytmp		GDB_YY_REMAP (yytmp)
-#define yyv		GDB_YY_REMAP (yyv)
-#define yy_yyv		GDB_YY_REMAP (yy_yyv)
-#define yyval		GDB_YY_REMAP (yyval)
-#define yylloc		GDB_YY_REMAP (yylloc)
-#define yyreds		GDB_YY_REMAP (yyreds)  /* With YYDEBUG defined */
-#define yytoks		GDB_YY_REMAP (yytoks)  /* With YYDEBUG defined */
-#define yyname		GDB_YY_REMAP (yyname)  /* With YYDEBUG defined */
-#define yyrule		GDB_YY_REMAP (yyrule)  /* With YYDEBUG defined */
-#define yylhs		GDB_YY_REMAP (yylhs)
-#define yylen		GDB_YY_REMAP (yylen)
-#define yydefred	GDB_YY_REMAP (yydefred)
-#define yydgoto		GDB_YY_REMAP (yydgoto)
-#define yysindex	GDB_YY_REMAP (yysindex)
-#define yyrindex	GDB_YY_REMAP (yyrindex)
-#define yygindex	GDB_YY_REMAP (yygindex)
-#define yytable		GDB_YY_REMAP (yytable)
-#define yycheck		GDB_YY_REMAP (yycheck)
-#define yyss		GDB_YY_REMAP (yyss)
-#define yysslim		GDB_YY_REMAP (yysslim)
-#define yyssp		GDB_YY_REMAP (yyssp)
-#define yystacksize	GDB_YY_REMAP (yystacksize)
-#define yyvs		GDB_YY_REMAP (yyvs)
-#define yyvsp		GDB_YY_REMAP (yyvsp)
-#define YYSTACKDATA	GDB_YY_REMAP (YYSTACKDATA)
-
-/* The following are common to all parsers.  */
-
-#ifndef YYDEBUG
-# define YYDEBUG 1  /* Default to yydebug support */
-#endif
-
-#endif /* GDB_YY_REMAP_H */
-- 
2.55.0


  parent reply	other threads:[~2026-09-04 17:10 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-04 16:56 [PATCH 00/17] Move C++ support code out of .y files simon.marchi
2026-09-04 16:56 ` [PATCH 01/17] gdb/ada-exp-parser: remove name_info struct simon.marchi
2026-09-04 16:56 ` [PATCH 02/17] gdb: replace parse_type macros with functions simon.marchi
2026-09-04 16:56 ` [PATCH 03/17] gdb: suffix flex/bison output files with -gen.c simon.marchi
2026-09-04 16:56 ` [PATCH 04/17] gdb: move parser output post-processing to a script simon.marchi
2026-09-05  0:38   ` Kevin Buettner
2026-09-05  3:59     ` Simon Marchi
2026-09-04 16:56 ` simon.marchi [this message]
2026-09-04 16:56 ` [PATCH 06/17] gdb: separate cp-name-parser's symbol prefix with an underscore simon.marchi
2026-09-04 16:56 ` [PATCH 07/17] gdb: make $(YACC) and $(FLEX) generate headers simon.marchi
2026-09-04 16:56 ` [PATCH 08/17] gdb: add check for stale build generated files simon.marchi
2026-09-04 16:56 ` [PATCH 09/17] gdb: move cp-name-parser.y's support code to cp-name-parser.c simon.marchi
2026-09-04 16:56 ` [PATCH 10/17] gdb: rename LANG-exp.y to LANG-exp-parser.y simon.marchi
2026-09-04 16:56 ` [PATCH 11/17] gdb: move c-exp-parser.y's support code to c-exp-parser.c simon.marchi
2026-09-04 16:56 ` [PATCH 12/17] gdb: move ada-exp-parser.y's support code to ada-exp-parser.c simon.marchi
2026-09-05  0:16   ` Kevin Buettner
2026-09-04 16:56 ` [PATCH 13/17] gdb: move d-exp-parser.y's support code to d-exp-parser.c simon.marchi
2026-09-04 16:56 ` [PATCH 14/17] gdb: move f-exp-parser.y's support code to f-exp-parser.c simon.marchi
2026-09-04 16:56 ` [PATCH 15/17] gdb: move go-exp-parser.y's support code to go-exp-parser.c simon.marchi
2026-09-04 16:56 ` [PATCH 16/17] gdb: move m2-exp-parser.y's support code to m2-exp-parser.c simon.marchi
2026-09-05  0:30   ` Kevin Buettner
2026-09-05  4:04     ` Simon Marchi
2026-09-04 16:56 ` [PATCH 17/17] gdb: move p-exp-parser.y's support code to p-exp-parser.c simon.marchi
2026-09-05  0:29   ` Kevin Buettner
2026-09-05  0:50 ` [PATCH 00/17] Move C++ support code out of .y files Kevin Buettner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260904170338.1643894-6-simon.marchi@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=simon.marchi@efficios.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox