From ed9202a02878e0618c5ca6bc7abcac5edce81b7d Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 24 Sep 2020 19:14:47 +0200 Subject: Use Bison directives rather than CPP macros "%define parse.error verbose" is supported since 3.0, and support for YYERROR_VERBOSE (deprecated since Bison 2.6, 2012-07-19), was removed in 3.6. --- src/turtle_parser.y | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/turtle_parser.y b/src/turtle_parser.y index 5339c08e..fbf33314 100644 --- a/src/turtle_parser.y +++ b/src/turtle_parser.y @@ -63,9 +63,6 @@ #endif -/* Make verbose error messages for syntax errors */ -#define YYERROR_VERBOSE 1 - /* Fail with an debug error message if RAPTOR_DEBUG > 1 */ #if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1 #define YYERROR_MSG(msg) do { fputs("** YYERROR ", RAPTOR_DEBUG_FH); fputs(msg, RAPTOR_DEBUG_FH); fputc('\n', RAPTOR_DEBUG_FH); YYERROR; } while(0) @@ -125,6 +122,9 @@ static void raptor_turtle_handle_statement(raptor_parser *parser, raptor_stateme /* Write parser header file with macros (bison -d) */ %defines +/* Make verbose error messages for syntax errors */ +%define parse.error verbose + /* Write output file with verbose descriptions of parser states */ %verbose -- cgit v1.2.1 From bb1cac89573a61b1cb2830eaa1497b455f3597a2 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 24 Sep 2020 19:32:50 +0200 Subject: Use per-type Bison destructors rather than per symbols I can see two differences, and I believe this is an improvement: before we were not reclaiming memory associated to blankNodePropertyList and GRAPH_NAME_LEFT_CURLY. Now we do. --- src/turtle_parser.y | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/turtle_parser.y b/src/turtle_parser.y index fbf33314..fad59215 100644 --- a/src/turtle_parser.y +++ b/src/turtle_parser.y @@ -193,22 +193,22 @@ static void raptor_turtle_handle_statement(raptor_parser *parser, raptor_stateme %destructor { if($$) RAPTOR_FREE(char*, $$); -} STRING_LITERAL BLANK_LITERAL INTEGER_LITERAL FLOATING_LITERAL DECIMAL_LITERAL IDENTIFIER LANGTAG +} %destructor { if($$) raptor_free_uri($$); -} URI_LITERAL QNAME_LITERAL +} %destructor { if($$) raptor_free_term($$); -} subject predicate object verb literal resource blankNode collection +} %destructor { if($$) raptor_free_sequence($$); -} triples objectList itemList predicateObjectList predicateObjectListOpt +} %% -- cgit v1.2.1 From 458a9b055d4123cde090b726bf9784a96d0071be Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 24 Sep 2020 19:36:20 +0200 Subject: Stylistic change in the parser Propose a shorter, more readable (IMHO) approach to typing tokens. --- src/turtle_parser.y | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/turtle_parser.y b/src/turtle_parser.y index fad59215..d18e68e9 100644 --- a/src/turtle_parser.y +++ b/src/turtle_parser.y @@ -171,16 +171,19 @@ static void raptor_turtle_handle_statement(raptor_parser *parser, raptor_stateme %token SPARQL_BASE "BASE" /* literals */ -%token STRING_LITERAL "string literal" -%token URI_LITERAL "URI literal" -%token GRAPH_NAME_LEFT_CURLY "Graph URI literal {" -%token BLANK_LITERAL "blank node" -%token QNAME_LITERAL "QName" -%token IDENTIFIER "identifier" -%token LANGTAG "langtag" -%token INTEGER_LITERAL "integer literal" -%token FLOATING_LITERAL "floating point literal" -%token DECIMAL_LITERAL "decimal literal" +%token + + STRING_LITERAL "string literal" + IDENTIFIER "identifier" + LANGTAG "langtag" + INTEGER_LITERAL "integer literal" + FLOATING_LITERAL "floating point literal" + DECIMAL_LITERAL "decimal literal" + BLANK_LITERAL "blank node" + + URI_LITERAL "URI literal" + GRAPH_NAME_LEFT_CURLY "Graph URI literal {" + QNAME_LITERAL "QName" /* syntax error */ %token ERROR_TOKEN -- cgit v1.2.1 From 7738d46529dc60f2ec57ab0e5f200aba4a8560d6 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 24 Sep 2020 19:06:58 +0200 Subject: Call Bison with the real file names Give Bison the real output file names, so that the implementation file is not fooled when it wants to find the generated header. To this end, generate the real output files, but postprocess them, instead of creating temporary files with wrong names, and then having Perl fix the wrong names. --- appveyor.yml | 12 ++--- scripts/fix-bison.pl | 143 ++++++++++++++++++++++++++++----------------------- src/CMakeLists.txt | 10 ++-- src/Makefile.am | 12 ++--- 4 files changed, 90 insertions(+), 87 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index eeccb436..dbb01f97 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -51,14 +51,10 @@ before_build: #- cmd: '%CYG_ROOT%/bin/flex -oturtle_lexer.c turtle_lexer.l' #- cmd: '%CYG_ROOT%/bin/perl ../scripts/fix-flex.pl turtle_lexer.c > turtle_lexer.t' #- cmd: '%CYG_ROOT%/bin/mv -f turtle_lexer.t turtle_lexer.c' - #- cmd: '%CYG_ROOT%/bin/bison turtle_parser.y' - #- cmd: '%CYG_ROOT%/bin/perl ../scripts/fix-bison.pl turtle_parser.tab.c > turtle_parser.c' - #- cmd: '%CYG_ROOT%/bin/mv -f turtle_parser.tab.h turtle_parser.h' - #- cmd: '%CYG_ROOT%/bin/rm -f turtle_parser.tab.c' - #- cmd: '%CYG_ROOT%/bin/bison parsedate.y' - #- cmd: '%CYG_ROOT%/bin/perl ../scripts/fix-bison.pl parsedate.tab.c > parsedate.c' - #- cmd: '%CYG_ROOT%/bin/mv -f parsedate.tab.h parsedate.h' - #- cmd: '%CYG_ROOT%/bin/rm -f parsedate.tab.c' + #- cmd: '%CYG_ROOT%/bin/bison -o turtle_parser.c turtle_parser.y' + #- cmd: '%CYG_ROOT%/bin/perl ../scripts/fix-bison.pl turtle_parser.c' + #- cmd: '%CYG_ROOT%/bin/bison -o parsedate.c parsedate.y' + #- cmd: '%CYG_ROOT%/bin/perl ../scripts/fix-bison.pl parsedate.c' - cmd: 'md c:\projects\dest' - cmd: 'md c:\projects\raptor\bld' - cmd: 'md c:\projects\raptor\bldstatic' diff --git a/scripts/fix-bison.pl b/scripts/fix-bison.pl index b5b3e748..2327a7ae 100755 --- a/scripts/fix-bison.pl +++ b/scripts/fix-bison.pl @@ -4,8 +4,7 @@ # # Usage: # bison -b foo_parser -p foo_parser_ -d -v foo_parser.y -# perl fix-bison.pl foo_parser.tab.c > $tmp -# mv $tmp foo_parser.tab.c +# perl fix-bison.pl foo_parser.tab.c # # Copyright (C) 2004-2014, David Beckett http://www.dajobe.org/ # Copyright (C) 2004, University of Bristol, UK http://www.bristol.ac.uk/ @@ -14,81 +13,95 @@ my $seen_yyerrlab1=0; my $syntax_error_has_default=0; my $line_offset=1; # #line directives always refer to the NEXT line -while(<>) { - # Remove code that causes a warning - if(/Suppress GCC warning that yyerrlab1/) { - do { - $_=<>; - $line_offset--; # skipped a line - } while(!/^\#endif/); - $line_offset--; # skipped a line - next; - } - $seen_yyerrlab1=1 if /goto yyerrlab1/; +my $extension = '.bak'; - s/^yyerrlab1:// unless $seen_yyerrlab1; +sub fix($) +{ + my ($file) = @_; + my $backup = $file . $extension; + rename($file, $backup); + open(IN, "$backup"); + open(OUT, ">$file"); - # Do not use macro name for a temporary variable - s/unsigned int yylineno = /unsigned int yylineno_tmp = /; - s/yyrule - 1, yylineno\)/yyrule - 1, yylineno_tmp\)/; + while() { + # Remove code that causes a warning + if(/Suppress GCC warning that yyerrlab1/) { + do { + $_ = ; + $line_offset--; # skipped a line + } while (!/^\#endif/); + $line_offset--; # skipped a line + next; + } - # Do not (re)define prototypes that the system did better - if(m%^void \*malloc\s*\(%) { - $line_offset--; # skipped a line - next; - } - if(m%^void free\s*\(%) { - $line_offset--; # skipped a line - next; - } + $seen_yyerrlab1=1 if /goto yyerrlab1/; - # syntax error handler will have a default case already in Bison 3.0.5+ - $syntax_error_has_default=1 if /default: \/\* Avoid compiler warnings. \*\//; + s/^yyerrlab1:// unless $seen_yyerrlab1; - if(m%^\# undef YYCASE_$% and $syntax_error_has_default==0) { - # Add a default value for yyformat on Bison <3.0.5, for coverity CID 10838 - my $line=$_; - print qq{ default: yyformat = YY_("syntax error");\n}; - $line_offset++; # extra line - print $line; - next; - } + # Do not use macro name for a temporary variable + s/unsigned int yylineno = /unsigned int yylineno_tmp = /; + s/yyrule - 1, yylineno\)/yyrule - 1, yylineno_tmp\)/; - if(m%yysyntax_error_status = YYSYNTAX_ERROR%) { - # Set yytoken to non-negative value for coverity CID 29259 - my $line=$_; - print qq{if(yytoken < 0) yytoken = YYUNDEFTOK;\n}; - $line_offset++; # extra line - print $line; - next; - } + # Do not (re)define prototypes that the system did better + if(m%^void \*malloc\s*\(%) { + $line_offset--; # skipped a line + next; + } + if(m%^void free\s*\(%) { + $line_offset--; # skipped a line + next; + } - # Suppress warnings about empty declarations - s/(^static int .*_init_globals.*);$/$1/; + # syntax error handler will have a default case already in Bison 3.0.5+ + $syntax_error_has_default=1 if /default: \/\* Avoid compiler warnings. \*\//; - # Remove always false condition - if(m%if \(/\*CONSTCOND\*/ 0\)%) { - $line_offset--; # skipped a line - $_ = <>; - $line_offset--; # skipped a line - next; - } + if(m%^\# undef YYCASE_$% and $syntax_error_has_default==0) { + # Add a default value for yyformat on Bison <3.0.5, for coverity CID 10838 + my $line=$_; + print OUT qq{ default: yyformat = YY_("syntax error");\n}; + $line_offset++; # extra line + print OUT $line; + next; + } - # Remove always false condition; this macro is #defined to 0 - if(m%if \(yytable_value_is_error \(yyn\)\)%) { - $line_offset--; # skipped a line - $_ = <>; - $line_offset--; # skipped a line - next; - } + if(m%yysyntax_error_status = YYSYNTAX_ERROR%) { + # Set yytoken to non-negative value for coverity CID 29259 + my $line=$_; + print OUT qq{if(yytoken < 0) yytoken = YYUNDEFTOK;\n}; + $line_offset++; # extra line + print OUT $line; + next; + } + # Suppress warnings about empty declarations + s/(^static int .*_init_globals.*);$/$1/; + # Remove always false condition + if(m%if \(/\*CONSTCOND\*/ 0\)%) { + $line_offset--; # skipped a line + $_ = ; + $line_offset--; # skipped a line + next; + } - # Fixup pending filename renaming, see above. - # Fix line numbers. - my $line=$. +$line_offset; - s/^(\#line) \d+ (.*)\.tab\.c/$1 $line $2.c/; + # Remove always false condition; this macro is #defined to 0 + if(m%if \(yytable_value_is_error \(yyn\)\)%) { + $line_offset--; # skipped a line + $_ = ; + $line_offset--; # skipped a line + next; + } + + # Fixup pending filename renaming, see above. + # Fix line numbers. + my $line=$. +$line_offset; + s/^(\#line) \d+ (.*\.c)/$1 $line $2/; + + print OUT; + } +} - print; +for my $file (@ARGV) { + fix $file; } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a7939215..6435836c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -36,14 +36,13 @@ IF(RAPTOR_PARSER_TURTLE OR RAPTOR_PARSER_TRIG) # Generate the turtle parser ADD_CUSTOM_TARGET(turtle_tables_tgt DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/turtle_parser.tab.c) ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/turtle_parser.tab.c - COMMAND ${BISON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/turtle_parser.y + COMMAND ${BISON_EXECUTABLE} -o ${CMAKE_CURRENT_BINARY_DIR}/turtle_parser.c ${CMAKE_CURRENT_SOURCE_DIR}/turtle_parser.y DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/turtle_parser.y) ADD_CUSTOM_TARGET(turtle_parser_tgt DEPENDS turtle_tables_tgt ${CMAKE_CURRENT_BINARY_DIR}/turtle_parser.c ${CMAKE_CURRENT_BINARY_DIR}/turtle_parser.h) ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/turtle_parser.c - COMMAND ${PERL_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/fix-bison.pl ${CMAKE_CURRENT_BINARY_DIR}/turtle_parser.tab.c > ${CMAKE_CURRENT_BINARY_DIR}/turtle_parser.c + COMMAND ${PERL_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/fix-bison.pl ${CMAKE_CURRENT_BINARY_DIR}/turtle_parser.c DEPENDS turtle_tables_tgt) - ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/turtle_parser.h COMMAND ${CMAKE_COMMAND} -E rename ${CMAKE_CURRENT_BINARY_DIR}/turtle_parser.tab.h ${CMAKE_CURRENT_BINARY_DIR}/turtle_parser.h) # Generate the turtle lexer ADD_CUSTOM_TARGET(turtle_flex_tgt DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/turtle_lexer.t) @@ -165,14 +164,13 @@ IF(RAPTOR_PARSEDATE) # Generate the date/time parser ADD_CUSTOM_TARGET(parsedate_tables_tgt DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/parsedate.tab.c) ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/parsedate.tab.c - COMMAND ${BISON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/parsedate.y + COMMAND ${BISON_EXECUTABLE} -o ${CMAKE_CURRENT_BINARY_DIR}/parsedate.c ${CMAKE_CURRENT_SOURCE_DIR}/parsedate.y DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/parsedate.y) ADD_CUSTOM_TARGET(parsedate_tgt DEPENDS parsedate_tables_tgt ${CMAKE_CURRENT_BINARY_DIR}/parsedate.c ${CMAKE_CURRENT_BINARY_DIR}/parsedate.h) ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/parsedate.c - COMMAND ${PERL_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/fix-bison.pl ${CMAKE_CURRENT_BINARY_DIR}/parsedate.tab.c > ${CMAKE_CURRENT_BINARY_DIR}/parsedate.c + COMMAND ${PERL_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/fix-bison.pl ${CMAKE_CURRENT_BINARY_DIR}/parsedate.c DEPENDS parsedate_tables_tgt) - ADD_CUSTOM_COMMAND(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/parsedate.h COMMAND ${CMAKE_COMMAND} -E rename ${CMAKE_CURRENT_BINARY_DIR}/parsedate.tab.h ${CMAKE_CURRENT_BINARY_DIR}/parsedate.h) ENDIF(RAPTOR_PARSEDATE) IF(BUILD_SHARED_LIBS) diff --git a/src/Makefile.am b/src/Makefile.am index da4cce95..906c884a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -219,19 +219,15 @@ turtle_lexer.h: turtle_lexer.c ; @exit 0 turtle_parser.c: $(srcdir)/turtle_parser.y $(top_srcdir)/scripts/fix-bison.pl $(AM_V_GEN) \ - $(BISON) $(srcdir)/turtle_parser.y; \ - $(PERL) $(top_srcdir)/scripts/fix-bison.pl turtle_parser.tab.c > $@; \ - mv -f turtle_parser.tab.h turtle_parser.h; \ - rm -f turtle_parser.tab.c + $(BISON) -o $@ $(srcdir)/turtle_parser.y; \ + $(PERL) $(top_srcdir)/scripts/fix-bison.pl $@ turtle_parser.h: turtle_parser.c ; @exit 0 parsedate.c: $(srcdir)/parsedate.y $(top_srcdir)/scripts/fix-bison.pl $(AM_V_GEN) \ - $(BISON) $(srcdir)/parsedate.y; \ - $(PERL) $(top_srcdir)/scripts/fix-bison.pl parsedate.tab.c > $@; \ - mv -f parsedate.tab.h parsedate.h; \ - rm -f parsedate.tab.c + $(BISON) -o $@ $(srcdir)/parsedate.y; \ + $(PERL) $(top_srcdir)/scripts/fix-bison.pl $@ parsedate.h: parsedate.c ; @exit 0 -- cgit v1.2.1