Skip to contentSkip to conversation

make yacc usage POSIX-compliant

PATCH In review Ethan Sommer 2 participants 2 messages Jan 30, 2020 – Feb 16, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 make yacc usage POSIX-compliant

use -b and -d to generate correctly named source and header files, instead of bison-specific --defines and non-POSIX -o
check that YACC command is found in path or is an executable file, instead of using bison-specific --version flag to display an error when it is missing
explicitly define yyltype in scripts/genksyms/lex.l, instead of relying on bison automatically defining it
replace bison-specific %destructor use in scripts/kconfig/parser.y dtc's yacc usage is not covered here, as its use of bison-specific features is much greater, and it is only built on certain architectures, unlike kconfig and genksyms

---
 scripts/Makefile.host     |  2 +-
 scripts/genksyms/Makefile |  6 ++++--
 scripts/genksyms/lex.l    |  2 ++
 scripts/kconfig/parser.y  | 14 +++++++-------
 4 files changed, 14 insertions(+), 10 deletions(-)
scripts/Makefile.host +1 -1
index 4c51c95d40f4..64e98e1d4825 100644
@@ -11,7 +11,7 @@ $(obj)/%.lex.c: $(src)/%.l FORCE
 # YACC
 # ---------------------------------------------------------------------------
 quiet_cmd_bison = YACC    $(basename $@).[ch]
-      cmd_bison = $(YACC) -o $(basename $@).c --defines=$(basename $@).h -t -l $<
+      cmd_bison = $(YACC) -b $(basename $(basename $@)) -d -t -l $<
 
 $(obj)/%.tab.c $(obj)/%.tab.h: $(src)/%.y FORCE
 	$(call if_changed,bison)
scripts/genksyms/Makefile +4 -2
index 78629f515e78..1e120328fa88 100644
@@ -14,9 +14,11 @@ genksyms-objs	:= genksyms.o parse.tab.o lex.lex.o
 # so that 'bison: not found' will be displayed if it is missing.
 ifeq ($(findstring 1,$(KBUILD_EXTRA_WARN)),)
 
+ifeq ($(shell command -v $(YACC) || [ -x $(YACC) ] && echo y),)
+  $(error command not found: $(YACC))
+endif
 quiet_cmd_bison_no_warn = $(quiet_cmd_bison)
-      cmd_bison_no_warn = $(YACC) --version >/dev/null; \
-			  $(cmd_bison) 2>/dev/null
+      cmd_bison_no_warn = $(cmd_bison) 2>/dev/null
 
 $(obj)/pars%.tab.c $(obj)/pars%.tab.h: $(src)/pars%.y FORCE
 	$(call if_changed,bison_no_warn)
scripts/genksyms/lex.l +2 -0
index e265c5d96861..0580c088527f 100644
@@ -19,6 +19,8 @@
 #include "genksyms.h"
 #include "parse.tab.h"
 
+extern YYSTYPE yylval;
+
 /* We've got a two-level lexer here.  We let flex do basic tokenization
    and then we categorize those basic tokens in the second stage.  */
 #define YY_DECL		static int yylex1(void)
scripts/kconfig/parser.y +7 -7
index b3eff9613cf8..9eb9a94a68e0 100644
@@ -20,6 +20,8 @@
 
 int cdebug = PRINTD;
 
+int yynerrs = 0;
+
 static void yyerror(const char *err);
 static void zconfprint(const char *err, ...);
 static void zconf_error(const char *err, ...);
@@ -101,13 +103,6 @@ static struct menu *current_menu, *current_entry;
 %type <string> word_opt assign_val
 %type <flavor> assign_op
 
-%destructor {
-	fprintf(stderr, "%s:%d: missing end statement for this entry\n",
-		$$->file->name, $$->lineno);
-	if (current_menu == $$)
-		menu_end_menu();
-} if_entry menu_entry choice_entry
-
 %%
 input: mainmenu_stmt stmt_list | stmt_list;
 
@@ -529,6 +524,11 @@ static bool zconf_endtoken(const char *tokenname,
 	if (strcmp(tokenname, expected_tokenname)) {
 		zconf_error("unexpected '%s' within %s block",
 			    tokenname, expected_tokenname);
+		if (!strcmp(tokenname, "if") || !strcmp(tokenname, "menu") ||
+			!strcmp(tokenname, "choice"))
+			fprintf(stderr, "%s:%d: missing end statement for this entry\n",
+				current_menu->file->name, current_menu->lineno);
+		menu_end_menu();
 		yynerrs++;
 		return false;
 	}
Signature · 1 line
2.25.0
#1 lore raw

Hi Ethan,

Quoted 12 lines · Ethan Sommer

use -b and -d to generate correctly named source and header files, instead of bison-specific --defines and non-POSIX -o
check that YACC command is found in path or is an executable file, instead of using bison-specific --version flag to display an error when it is missing
explicitly define yyltype in scripts/genksyms/lex.l, instead of relying on bison automatically defining it
replace bison-specific %destructor use in scripts/kconfig/parser.y dtc's yacc usage is not covered here, as its use of bison-specific features is much greater, and it is only built on certain architectures, unlike kconfig and genksyms

Please start each sentence with a capital letter,
and end with a period.

Quoted 27 lines
---
 scripts/Makefile.host     |  2 +-
 scripts/genksyms/Makefile |  6 ++++--
 scripts/genksyms/lex.l    |  2 ++
 scripts/kconfig/parser.y  | 14 +++++++-------
 4 files changed, 14 insertions(+), 10 deletions(-)
scripts/Makefile.host +1 -1
index 4c51c95d40f4..64e98e1d4825 100644
@@ -11,7 +11,7 @@ $(obj)/%.lex.c: $(src)/%.l FORCE
 # YACC
 # ---------------------------------------------------------------------------
 quiet_cmd_bison = YACC    $(basename $@).[ch]
-      cmd_bison = $(YACC) -o $(basename $@).c --defines=$(basename $@).h -t -l $<
+      cmd_bison = $(YACC) -b $(basename $(basename $@)) -d -t -l $<
 
 $(obj)/%.tab.c $(obj)/%.tab.h: $(src)/%.y FORCE
        $(call if_changed,bison)
scripts/genksyms/Makefile +0 -0
index 78629f515e78..1e120328fa88 100644

scripts/genksyms/Makefile genksyms-objs := genksyms.o parse.tab.o lex.lex.o · line 19

Quoted 6 lines
scripts/genksyms/Makefile +3 -0
@@ -14,9 +14,11 @@ genksyms-objs        := genksyms.o parse.tab.o lex.lex.o
 # so that 'bison: not found' will be displayed if it is missing.
 ifeq ($(findstring 1,$(KBUILD_EXTRA_WARN)),)
 
+ifeq ($(shell command -v $(YACC) || [ -x $(YACC) ] && echo y),)
+  $(error command not found: $(YACC))
+endif

Please do not check the presence of $(YACC)
in the parse stage of Makefile.

You would not be able to run 'make mrproper' or 'make distclean' without $(YACC), which is odd.

Quoted 25 lines
scripts/genksyms/Makefile +1 -2
 quiet_cmd_bison_no_warn = $(quiet_cmd_bison)
-      cmd_bison_no_warn = $(YACC) --version >/dev/null; \
-                         $(cmd_bison) 2>/dev/null
+      cmd_bison_no_warn = $(cmd_bison) 2>/dev/null
 
 $(obj)/pars%.tab.c $(obj)/pars%.tab.h: $(src)/pars%.y FORCE
        $(call if_changed,bison_no_warn)
scripts/genksyms/lex.l +2 -0
index e265c5d96861..0580c088527f 100644
@@ -19,6 +19,8 @@
 #include "genksyms.h"
 #include "parse.tab.h"
 
+extern YYSTYPE yylval;
+
 /* We've got a two-level lexer here.  We let flex do basic tokenization
    and then we categorize those basic tokens in the second stage.  */
 #define YY_DECL                static int yylex1(void)
scripts/kconfig/parser.y +0 -0
index b3eff9613cf8..9eb9a94a68e0 100644

scripts/kconfig/parser.y line 24

Quoted 5 lines
scripts/kconfig/parser.y +2 -0
@@ -20,6 +20,8 @@
 
 int cdebug = PRINTD;
 
+int yynerrs = 0;
+

Isn't yynerrs POSIX-compiliant?

Both bison and byacc worked for me without this change.

This patch is stricter than
your previous 'make it work for byacc as well'.

If this is a desired change,
how can I test if the code is POSIX-compliant?

But, I am still not convinced with this part:

"dtc's yacc usage is not covered here, as its use of bison-specific features is much greater, and it is only built on certain architectures, unlike kconfig and genksyms"

If DTC remains as an exceptional case that is bison-specific code, I do not get the point of merging this change.

Quoted 30 lines
scripts/kconfig/parser.y +5 -8
 static void yyerror(const char *err);
 static void zconfprint(const char *err, ...);
 static void zconf_error(const char *err, ...);
@@ -101,13 +103,6 @@ static struct menu *current_menu, *current_entry;
 %type <string> word_opt assign_val
 %type <flavor> assign_op
 
-%destructor {
-       fprintf(stderr, "%s:%d: missing end statement for this entry\n",
-               $$->file->name, $$->lineno);
-       if (current_menu == $$)
-               menu_end_menu();
-} if_entry menu_entry choice_entry
-
 %%
 input: mainmenu_stmt stmt_list | stmt_list;
 
@@ -529,6 +524,11 @@ static bool zconf_endtoken(const char *tokenname,
        if (strcmp(tokenname, expected_tokenname)) {
                zconf_error("unexpected '%s' within %s block",
                            tokenname, expected_tokenname);
+               if (!strcmp(tokenname, "if") || !strcmp(tokenname, "menu") ||
+                       !strcmp(tokenname, "choice"))
+                       fprintf(stderr, "%s:%d: missing end statement for this entry\n",
+                               current_menu->file->name, current_menu->lineno);
+               menu_end_menu();
                yynerrs++;
                return false;
        }
--
Quoted 2 lines

2.25.0

--
Best Regards
Masahiro Yamada