ylwrap: rename header inclusion in generated parsers
authorAkim Demaille <akim@lrde.epita.fr>
Fri, 13 Jul 2012 12:32:22 +0000 (14:32 +0200)
committerStefano Lattarini <stefano.lattarini@gmail.com>
Sat, 14 Jul 2012 10:10:22 +0000 (12:10 +0200)
Some types of Bison parsers, such as the GLR ones, generate a header
file that they include.  ylwrap, which renames the generated files,
does not rename the included file.  Fix this shortcoming, reported
for instance here:
<http://lists.gnu.org/archive/html/bug-bison/2012-06/msg00033.html>.
Fixes t/yacc-bison-skeleton.sh, see Automake bug#7648 and PR automake/491.

* lib/ylwrap (quote_for_sed): Accept arguments.
Catch more special characters.
(rename_sed): New.
Improve the previous renaming sed commands using quote_for_sed.
Suggested by Stefano Lattarini here:
<http://lists.gnu.org/archive/html/automake-patches/2012-07/msg00095.html>.
(main loop): Use rename_sed to rename the dependencies to other files.
* t/yacc-d-basic.sh: Exercise this case, even if bison/yacc was
not issuing such an include.
* t/list-of-tests.mk (XFAIL_TESTS): Adjust.

lib/ylwrap
t/list-of-tests.mk
t/yacc-d-basic.sh

index 8a9f2b0..8e02e9a 100755 (executable)
@@ -1,7 +1,7 @@
 #! /bin/sh
 # ylwrap - wrapper for lex/yacc invocations.
 
-scriptversion=2012-07-13.10; # UTC
+scriptversion=2012-07-13.14; # UTC
 
 # Copyright (C) 1996-2012 Free Software Foundation, Inc.
 #
@@ -48,10 +48,16 @@ guard()
         -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'
 }
 
+# quote_for_sed [STRING]
+# ----------------------
+# Return STRING (or stdin) quoted to be used as a sed pattern.
 quote_for_sed ()
 {
-  # FIXME: really we should care about more than '.' and '\'.
-  sed -e 's,[\\.],\\&,g'
+  case $# in
+    0) cat;;
+    1) printf '%s\n' "$1";;
+  esac \
+    | sed -e 's|[][\\.*]|\\&|g'
 }
 
 case "$1" in
@@ -113,6 +119,10 @@ fi
 
 # The list of file to rename: FROM TO...
 pairlist=
+# A sed program to s/FROM/TO/g for all the FROM/TO so that, for
+# instance, we rename #include "y.tab.h" into #include "parse.h"
+# during the conversion from y.tab.c to parse.c.
+rename_sed=
 while test "$#" -ne 0; do
   if test "$1" = "--"; then
     shift
@@ -130,6 +140,7 @@ while test "$#" -ne 0; do
   to=$1
   shift
   pairlist="$pairlist $from $to"
+  rename_sed="${rename_sed}s|"`quote_for_sed "$from"`"|$to|g;"
 done
 
 # The program to run.
@@ -196,7 +207,7 @@ if test $ret -eq 0; then
       FROM=`guard "$from"`
       TARGET=`guard "$to"`
 
-      sed -e "/^#/!b" -e "s,$input_rx,$input_sub_rx," -e "s,$from,$to," \
+      sed -e "/^#/!b" -e "s,$input_rx,$input_sub_rx," -e "$rename_sed" \
           -e "s,$FROM,$TARGET," "$from" >"$target" || ret=$?
 
       # Check whether header files must be updated.
index 78d7d9b..9f0e123 100644 (file)
@@ -31,7 +31,6 @@ t/pm/Version3.pl
 XFAIL_TESTS = \
 t/all.sh \
 t/yacc-bison-skeleton-cxx.sh \
-t/yacc-bison-skeleton.sh \
 t/cond17.sh \
 t/gcj6.sh \
 t/override-conditional-2.sh \
index 91fbc62..72872f2 100755 (executable)
@@ -54,7 +54,14 @@ void yyerror (char *s) {}
 x : 'x' {};
 %%
 END
-cp foo/parse.y bar/parse.y
+# Using ylwrap, we actually generate y.tab.[ch].  Unfortunately, we
+# forgot to rename #include "y.tab.h" into #include "parse.h" during
+# the conversion from y.tab.c to parse.c.  This was OK when Bison was
+# not issuing such an #include (up to 2.6).
+#
+# To make sure that we perform this conversion, in bar/parse.y, use
+# y.tab.h instead of parse.c.
+sed -e 's/parse\.h/y.tab.h/' <foo/parse.y >bar/parse.y
 
 cat > foo/main.c << 'END'
 #include "parse.h"