From: simon.marchi@polymtl.ca
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH 04/17] gdb: move parser output post-processing to a script
Date: Fri, 4 Sep 2026 12:56:36 -0400 [thread overview]
Message-ID: <20260904170338.1643894-5-simon.marchi@polymtl.ca> (raw)
In-Reply-To: <20260904170338.1643894-1-simon.marchi@polymtl.ca>
From: Simon Marchi <simon.marchi@efficios.com>
The output of bison and flex needs some post-processing before being
compiled. It is currently done by a sed invocation inlined in each of the
two pattern rules, which is kind of hard to read and modify. Additionally,
a following patch adds the generation of header files for parsers, and
those will need the same post processing. Moving the code to a dedicated
script makes re-use easier.
Move the code to post-process-parser-output.sh, which takes the name of the
parser generator (flex or bison), just to know which substitutions to do,
as well as the name of the parser file.
Make the Makefile rules depend on the script, so that the parsers are
regenerated when it changes.
Change-Id: I15f43184034aff33d242052db79586980ec2dac0
---
gdb/Makefile.in | 39 +++++-------------
gdb/post-process-parser-output.sh | 67 +++++++++++++++++++++++++++++++
2 files changed, 78 insertions(+), 28 deletions(-)
create mode 100755 gdb/post-process-parser-output.sh
diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index aeafb9394d7b..80b2f8573294 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -2693,39 +2693,22 @@ po/$(PACKAGE).pot: force
%.c: %.y
%.c: %.l
-%-gen.c: %.y
+# Post-process a file produced by a parser generator, see the script
+# for details.
+POST_PROCESS_PARSER_OUTPUT_SH = $(srcdir)/post-process-parser-output.sh
+POST_PROCESS_PARSER_OUTPUT = $(SHELL) $(POST_PROCESS_PARSER_OUTPUT_SH)
+
+%-gen.c: %.y $(POST_PROCESS_PARSER_OUTPUT_SH)
$(ECHO_YACC) $(SHELL) $(YLWRAP) $< y.tab.c $@.tmp -- \
$(YACC) $(YFLAGS) || (rm -f $@.tmp; false)
- @sed -e '/extern.*malloc/d' \
- -e '/extern.*realloc/d' \
- -e '/extern.*free/d' \
- -e '/include.*malloc.h/d' \
- -e 's/\([^x]\)malloc/\1xmalloc/g' \
- -e 's/\([^x]\)realloc/\1xrealloc/g' \
- -e 's/\([ \t;,(]\)free\([ \t]*[&(),]\)/\1xfree\2/g' \
- -e 's/\([ \t;,(]\)free$$/\1xfree/g' \
- -e '/^#line.*y.tab.c/d' \
- -e 's/YY_NULL/YY_NULLPTR/g' \
- -e "s/YYSTYPE/$(subst -,_,$*)_YYSTYPE/g" \
- -e "s/yyalloc/$(subst -,_,$*)_yyalloc/g" \
- -e "s/yysymbol_kind_t/$(subst -,_,$*)_yysymbol_kind_t/g" \
- < $@.tmp > $@.new && \
+ @$(POST_PROCESS_PARSER_OUTPUT) bison $* < $@.tmp > $@.new && \
rm -f $@.tmp && \
mv $@.new $@
-%-gen.c: %.l
+%-gen.c: %.l $(POST_PROCESS_PARSER_OUTPUT_SH)
$(ECHO_LEX) $(FLEX) -t $< > $@.tmp || (rm -f $@.tmp; false)
- @sed -e '/extern.*malloc/d' \
- -e '/extern.*realloc/d' \
- -e '/extern.*free/d' \
- -e '/include.*malloc.h/d' \
- -e 's/\([^x]\)malloc/\1xmalloc/g' \
- -e 's/\([^x]\)realloc/\1xrealloc/g' \
- -e 's/\([ \t;,(]\)free\([ \t]*[&(),]\)/\1xfree\2/g' \
- -e 's/\([ \t;,(]\)free$$/\1xfree/g' \
- -e 's/yy_flex_xrealloc/yyxrealloc/g' \
- < $@.tmp > $@.new && \
- rm -f $@.tmp && \
- mv $@.new $@
+ @$(POST_PROCESS_PARSER_OUTPUT) flex $* < $@.tmp > $@.new && \
+ rm -f $@.tmp && \
+ mv $@.new $@
# XML rules
diff --git a/gdb/post-process-parser-output.sh b/gdb/post-process-parser-output.sh
new file mode 100755
index 000000000000..b246e63ddff1
--- /dev/null
+++ b/gdb/post-process-parser-output.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+# Copyright (C) 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/>.
+
+# Post-process a file produced by flex or bison/byacc, reading it from
+# standard input and writing the result to standard output.
+#
+# Usage:
+#
+# ./post-process-parser-output.sh GENERATOR NAME < INPUT > OUTPUT
+#
+# Where GENERATOR is "bison" (regardless of whether we're using the actual
+# bison or byacc) or "flex", and NAME is the name of the parser (e.g.
+# "cp-name-parser"), used to make the global symbols names bison produces
+# unique.
+
+set -e
+
+generator="$1"
+
+# Convert e.g. "cp-name-parser" to "cp_name_parser".
+name=$(echo "$2" | sed -e 's/-/_/g')
+
+common='
+/extern.*malloc/d
+/extern.*realloc/d
+/extern.*free/d
+/include.*malloc.h/d
+s/\([^x]\)malloc/\1xmalloc/g
+s/\([^x]\)realloc/\1xrealloc/g
+s/\([ \t;,(]\)free\([ \t]*[&(),]\)/\1xfree\2/g
+s/\([ \t;,(]\)free$/\1xfree/g
+'
+
+case "$generator" in
+ bison)
+ sed -e "$common" \
+ -e '/^#line.*y.tab.c/d' \
+ -e 's/YY_NULL/YY_NULLPTR/g' \
+ -e "s/YYSTYPE/${name}_YYSTYPE/g" \
+ -e "s/yyalloc/${name}_yyalloc/g" \
+ -e "s/yysymbol_kind_t/${name}_yysymbol_kind_t/g"
+ ;;
+ flex)
+ sed -e "$common" \
+ -e 's/yy_flex_xrealloc/yyxrealloc/g'
+ ;;
+ *)
+ echo "$0: unknown parser generator \"$generator\"" >&2
+ exit 1
+ ;;
+esac
--
2.55.0
next prev 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 ` simon.marchi [this message]
2026-09-05 0:38 ` [PATCH 04/17] gdb: move parser output post-processing to a script Kevin Buettner
2026-09-05 3:59 ` Simon Marchi
2026-09-04 16:56 ` [PATCH 05/17] gdb: let the parser and lexer generators prefix their symbols simon.marchi
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-5-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