X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/1a05945136faf21ac5e3a07cfcdee8ee2e8ab6ae..b2a0b7ca70f9490693d517d10c9c4a64c9fc1af0:/tests/actions.at diff --git a/tests/actions.at b/tests/actions.at index 8ca79669..e1b2cb99 100644 --- a/tests/actions.at +++ b/tests/actions.at @@ -1,5 +1,5 @@ # Executing Actions. -*- Autotest -*- -# Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. +# Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. # 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 @@ -30,13 +30,13 @@ AT_SETUP([Mid-rule actions]) # action. AT_DATA_GRAMMAR([[input.y]], -[[%{ +[[%error-verbose +%debug +%{ # include # include static void yyerror (const char *msg); static int yylex (void); -# define YYDEBUG 1 -# define YYERROR_VERBOSE 1 %} %% exp: { putchar ('0'); } @@ -91,13 +91,14 @@ AT_CLEANUP AT_SETUP([Exotic Dollars]) AT_DATA_GRAMMAR([[input.y]], -[[%{ +[[%error-verbose +%debug +%{ # include # include static void yyerror (const char *msg); static int yylex (void); -# define YYDEBUG 1 -# define YYERROR_VERBOSE 1 +# define USE(Var) %} %union @@ -112,6 +113,7 @@ AT_DATA_GRAMMAR([[input.y]], exp: a_1 a_2 { $$ = 3; } { $$ = $3 + 1; } a_5 sum_of_the_five_previous_values { + USE (($1, $2, $3, $4, $5)); printf ("%d\n", $6); } ; @@ -145,7 +147,7 @@ main (void) } ]]) -AT_CHECK([bison -d -v -o input.c input.y]) +AT_CHECK([bison -d -v -o input.c input.y], 0) AT_COMPILE([input]) AT_PARSER_CHECK([./input], 0, [[15 @@ -170,7 +172,7 @@ m4_if([$1$2$3], $[1]$[2]$[3], [], # helping macros. So don't put any directly in the Bison file. AT_BISON_OPTION_PUSHDEFS([$5]) AT_DATA_GRAMMAR([[input.y]], -[[%{ +[[%start-header { #include #include #include @@ -180,23 +182,24 @@ AT_DATA_GRAMMAR([[input.y]], ]AT_LALR1_CC_IF( [#define RANGE(Location) (Location).begin.line, (Location).end.line], [#define RANGE(Location) (Location).first_line, (Location).last_line]) -[%} +[} $5] m4_ifval([$6], [%union { int ival; }]) -[ -%{ -]AT_LALR1_CC_IF([typedef yy::location YYLTYPE; +AT_LALR1_CC_IF([%define "global_tokens_and_yystype"]) +m4_ifval([$6], [[%end-header {]], [[%start-header {]]) +AT_LALR1_CC_IF([typedef yy::location YYLTYPE; m4_ifval([$6], , [#define YYSTYPE int])]) [static int yylex (]AT_LEX_FORMALS[); ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);]) -[%} +[} -]m4_ifval([$6], [%type '(' 'x' 'y' ')' ';' thing line input])[ +]m4_ifval([$6], [%type '(' 'x' 'y' ')' ';' thing line input END])[ +/* FIXME: This %printer isn't actually tested. */ %printer { ]AT_LALR1_CC_IF([debug_stream () << $$;], @@ -224,6 +227,11 @@ m4_ifval([$6], [%union { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); } 'y' +%token END 0 +%destructor + { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); } + END + %% /* This grammar is made to exercise error recovery. @@ -239,7 +247,7 @@ input: printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$)); } | line input /* Right recursive to load the stack so that popping at - EOF can be exercised. */ + END can be exercised. */ { $$ = 2; printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n", @@ -286,7 +294,7 @@ thing: ; %% /* Alias to ARGV[1]. */ -const char *yysource = 0; +const char *source = 0; static int yylex (]AT_LEX_FORMALS[) @@ -303,12 +311,12 @@ yylex (]AT_LEX_FORMALS[) AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9; ])[ - if (yysource[c]) - printf ("sending: '%c'", yysource[c]); + if (source[c]) + printf ("sending: '%c'", source[c]); else - printf ("sending: EOF"); + printf ("sending: END"); printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[)); - return yysource[c]; + return source[c]; } ]AT_LALR1_CC_IF( @@ -340,7 +348,7 @@ main (int argc, const char *argv[]) int status; yydebug = !!getenv ("YYDEBUG"); assert (argc == 2); - yysource = argv[1]; + source = argv[1]; status = yyparse (); switch (status) { @@ -370,9 +378,11 @@ sending: 'x' (1@10-19) thing (1@10-19): 'x' (1@10-19) sending: ')' (2@20-29) line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) -sending: EOF (3@30-39) +sending: END (3@30-39) input (0@29-29): /* Nothing */ input (2@0-29): line (0@0-29) input (0@29-29) +Freeing token END (3@30-39) +Freeing nterm input (2@0-29) Successful parse. ]]) @@ -388,9 +398,11 @@ sending: 'y' (1@10-19) Freeing token 'y' (1@10-19) sending: ')' (2@20-29) line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29) -sending: EOF (3@30-39) +sending: END (3@30-39) input (0@29-29): /* Nothing */ input (2@0-29): line (-1@0-29) input (0@29-29) +Freeing token END (3@30-39) +Freeing nterm input (2@0-29) Successful parse. ]]) @@ -441,12 +453,45 @@ input (0@129-129): /* Nothing */ input (2@100-129): line (10@100-129) input (0@129-129) input (2@70-129): line (7@70-99) input (2@100-129) input (2@0-129): line (-1@0-69) input (2@70-129) -130-139: syntax error, unexpected 'y', expecting $end +130-139: syntax error, unexpected 'y', expecting END Freeing nterm input (2@0-129) Freeing token 'y' (13@130-139) Parsing FAILED. ]]) + +# Syntax error caught by the parser where lookahead = END +# -------------------------------------------------------- +# Load the stack and provoke an error that cannot be caught by the +# grammar, to check that the stack is cleared. And make sure the +# lookahead is freed. +# +# '(', 'x', ')', +# '(', 'x', ')', +# 'x' +AT_PARSER_CHECK([./input '(x)(x)x'], 1, +[[sending: '(' (0@0-9) +sending: 'x' (1@10-19) +thing (1@10-19): 'x' (1@10-19) +sending: ')' (2@20-29) +line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) +sending: '(' (3@30-39) +sending: 'x' (4@40-49) +thing (4@40-49): 'x' (4@40-49) +sending: ')' (5@50-59) +line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59) +sending: 'x' (6@60-69) +thing (6@60-69): 'x' (6@60-69) +sending: END (7@70-79) +70-79: syntax error, unexpected END, expecting 'x' +Freeing nterm thing (6@60-69) +Freeing nterm line (3@30-59) +Freeing nterm line (0@0-29) +Freeing token END (7@70-79) +Parsing FAILED. +]]) + + # Check destruction upon stack overflow # ------------------------------------- # Upon stack overflow, all symbols on the stack should be destroyed. @@ -530,3 +575,503 @@ AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union]) AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser]) AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union]) + + + +## --------------------------------- ## +## Default %printer and %destructor. ## +## --------------------------------- ## + +# Check that the right %printer and %destructor are called, that they're not +# called for $end, and that $$ and @$ work correctly. + +AT_SETUP([Default %printer and %destructor]) + +AT_DATA_GRAMMAR([[input.y]], +[[%error-verbose +%debug +%locations +%initial-action { + @$.first_line = @$.last_line = 1; + @$.first_column = @$.last_column = 1; +} + +%{ +# include +# include + static void yyerror (const char *msg); + static int yylex (void); +# define USE(SYM) +%} + +%printer { + fprintf (yyoutput, "Default printer for '%c' @ %d", $$, @$.first_column); +} %symbol-default +%destructor { + fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column); +} %symbol-default + +%printer { + fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column); +} 'b' 'c' +%destructor { + fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column); +} 'b' 'c' + +%% + +start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ; + +%% + +static int +yylex (void) +{ + static const char *input = "abcd"; + static int column = 1; + yylval = *input++; + yylloc.first_line = yylloc.last_line = 1; + yylloc.first_column = yylloc.last_column = column++; + return yylval; +} + +static void +yyerror (const char *msg) +{ + fprintf (stderr, "%s\n", msg); +} + +int +main (void) +{ + yydebug = 1; + return yyparse (); +} +]]) + +AT_CHECK([bison -o input.c input.y]) +AT_COMPILE([input]) +AT_PARSER_CHECK([./input], 1, +[[Default destructor for 'd' @ 4. +'b'/'c' destructor for 'c' @ 3. +'b'/'c' destructor for 'b' @ 2. +Default destructor for 'a' @ 1. +]], +[[Starting parse +Entering state 0 +Reading a token: Next token is token 'a' (1.1-1.1: Default printer for 'a' @ 1) +Shifting token 'a' (1.1-1.1: Default printer for 'a' @ 1) +Entering state 1 +Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) +Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) +Entering state 3 +Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) +Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) +Entering state 5 +Reading a token: Next token is token 'd' (1.4-1.4: Default printer for 'd' @ 4) +Shifting token 'd' (1.4-1.4: Default printer for 'd' @ 4) +Entering state 6 +Reading a token: Now at end of input. +syntax error, unexpected $end, expecting 'e' +Error: popping token 'd' (1.4-1.4: Default printer for 'd' @ 4) +Stack now 0 1 3 5 +Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) +Stack now 0 1 3 +Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) +Stack now 0 1 +Error: popping token 'a' (1.1-1.1: Default printer for 'a' @ 1) +Stack now 0 +Cleanup: discarding lookahead token $end (1.5-1.5: ) +Stack now 0 +]]) + +AT_CLEANUP + + + +## ----------------------------------- ## +## Per-type %printer and %destructor. ## +## ----------------------------------- ## + +AT_SETUP([Per-type %printer and %destructor]) + +AT_DATA_GRAMMAR([[input.y]], +[[%error-verbose +%debug + +%{ +# include +# include + static void yyerror (const char *msg); + static int yylex (void); +# define USE(SYM) +%} + +%union { int field0; int field1; int field2; } +%type start 'a' 'g' +%type 'e' +%type 'f' +%printer { + fprintf (yyoutput, "%%symbol-default//e printer"); +} %symbol-default 'e' +%destructor { + fprintf (stdout, "%%symbol-default//e destructor.\n"); +} %symbol-default 'e' + +%type 'b' +%printer { fprintf (yyoutput, " printer"); } +%destructor { fprintf (stdout, " destructor.\n"); } + +%type 'c' +%printer { fprintf (yyoutput, "'c' printer"); } 'c' +%destructor { fprintf (stdout, "'c' destructor.\n"); } 'c' + +%type 'd' +%printer { fprintf (yyoutput, "'d' printer"); } 'd' +%destructor { fprintf (stdout, "'d' destructor.\n"); } 'd' + +%% + +start: + 'a' 'b' 'c' 'd' 'e' 'f' 'g' + { + USE(($1, $2, $3, $4, $5, $6, $7)); + $$ = 'S'; + } + ; + +%% + +static int +yylex (void) +{ + static const char *input = "abcdef"; + return *input++; +} + +static void +yyerror (const char *msg) +{ + fprintf (stderr, "%s\n", msg); +} + +int +main (void) +{ + yydebug = 1; + return yyparse (); +} +]]) + +AT_CHECK([bison -o input.c input.y]) +AT_COMPILE([input]) +AT_PARSER_CHECK([./input], 1, +[[%symbol-default//e destructor. +%symbol-default//e destructor. +'d' destructor. +'c' destructor. + destructor. +%symbol-default//e destructor. +]], +[[Starting parse +Entering state 0 +Reading a token: Next token is token 'a' (%symbol-default//e printer) +Shifting token 'a' (%symbol-default//e printer) +Entering state 1 +Reading a token: Next token is token 'b' ( printer) +Shifting token 'b' ( printer) +Entering state 3 +Reading a token: Next token is token 'c' ('c' printer) +Shifting token 'c' ('c' printer) +Entering state 5 +Reading a token: Next token is token 'd' ('d' printer) +Shifting token 'd' ('d' printer) +Entering state 6 +Reading a token: Next token is token 'e' (%symbol-default//e printer) +Shifting token 'e' (%symbol-default//e printer) +Entering state 7 +Reading a token: Next token is token 'f' (%symbol-default//e printer) +Shifting token 'f' (%symbol-default//e printer) +Entering state 8 +Reading a token: Now at end of input. +syntax error, unexpected $end, expecting 'g' +Error: popping token 'f' (%symbol-default//e printer) +Stack now 0 1 3 5 6 7 +Error: popping token 'e' (%symbol-default//e printer) +Stack now 0 1 3 5 6 +Error: popping token 'd' ('d' printer) +Stack now 0 1 3 5 +Error: popping token 'c' ('c' printer) +Stack now 0 1 3 +Error: popping token 'b' ( printer) +Stack now 0 1 +Error: popping token 'a' (%symbol-default//e printer) +Stack now 0 +Cleanup: discarding lookahead token $end () +Stack now 0 +]]) + +AT_CLEANUP + + + +## ------------------------------------------------------------- ## +## Default %printer and %destructor for user-defined end token. ## +## ------------------------------------------------------------- ## + +AT_SETUP([Default %printer and %destructor for user-defined end token]) + +AT_DATA_GRAMMAR([[input.y]], +[[%error-verbose +%debug +%locations +%initial-action { + @$.first_line = @$.last_line = 1; + @$.first_column = @$.last_column = 1; +} + +%{ +# include +# include + static void yyerror (const char *msg); + static int yylex (void); +# define USE(SYM) +%} + +%token END 0 +%printer { + fprintf (yyoutput, "Default printer for '%c' @ %d", $$, @$.first_column); +} %symbol-default +%destructor { + fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column); +} %symbol-default + +%% + +start: { $$ = 'S'; } ; + +%% + +static int +yylex (void) +{ + yylval = 'E'; + yylloc.first_line = yylloc.last_line = 1; + yylloc.first_column = yylloc.last_column = 1; + return 0; +} + +static void +yyerror (const char *msg) +{ + fprintf (stderr, "%s\n", msg); +} + +int +main (void) +{ + yydebug = 1; + return yyparse (); +} +]]) + +AT_CHECK([bison -o input.c input.y]) +AT_COMPILE([input]) +AT_PARSER_CHECK([./input], 0, +[[Default destructor for 'E' @ 1. +Default destructor for 'S' @ 1. +]], +[[Starting parse +Entering state 0 +Reducing stack by rule 1 (line 37): +-> $$ = nterm start (1.1-1.1: Default printer for 'S' @ 1) +Stack now 0 +Entering state 1 +Reading a token: Now at end of input. +Shifting token END (1.1-1.1: Default printer for 'E' @ 1) +Entering state 2 +Stack now 0 1 2 +Cleanup: popping token END (1.1-1.1: Default printer for 'E' @ 1) +Cleanup: popping nterm start (1.1-1.1: Default printer for 'S' @ 1) +]]) + +AT_CLEANUP + + + +## ------------------------------------------------------------------ ## +## Default %printer and %destructor are not for error or $undefined. ## +## ------------------------------------------------------------------ ## + +AT_SETUP([Default %printer and %destructor are not for error or \$undefined]) + +# If Bison were to apply the default %printer and %destructor to the error +# token or to $undefined: +# - For the error token: +# - It would generate warnings for unused $n. +# - It would invoke the %printer and %destructor on the error token's +# semantic value, which would be initialized from the lookahead, which +# would be destroyed separately. +# - For $undefined, who knows what the semantic value would be. + +AT_DATA_GRAMMAR([[input.y]], +[[%debug + +%{ +# include + static void yyerror (const char *msg); + static int yylex (void); +# define USE(SYM) +%} + +%printer { + fprintf (yyoutput, "'%c'", $$); +} %symbol-default +%destructor { + fprintf (stderr, "DESTROY '%c'\n", $$); +} %symbol-default + +%% + +start: + { $$ = 'S'; } + /* In order to reveal the problems that this bug caused during parsing, add + * $2 to USE. */ + | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; } + ; + +%% + +static int +yylex (void) +{ + static const char *input = "abd"; + yylval = *input++; + return yylval; +} + +static void +yyerror (const char *msg) +{ + fprintf (stderr, "%s\n", msg); +} + +int +main (void) +{ + yydebug = 1; + return yyparse (); +} +]]) + +AT_CHECK([bison -o input.c input.y]) +AT_COMPILE([input]) +AT_PARSER_CHECK([./input], [1], [], +[[Starting parse +Entering state 0 +Reading a token: Next token is token 'a' ('a') +Shifting token 'a' ('a') +Entering state 1 +Reading a token: Next token is token 'b' ('b') +syntax error +Shifting token error () +Entering state 3 +Next token is token 'b' ('b') +Shifting token 'b' ('b') +Entering state 5 +Reading a token: Next token is token $undefined () +Error: popping token 'b' ('b') +DESTROY 'b' +Stack now 0 1 3 +Error: popping token error () +Stack now 0 1 +Shifting token error () +Entering state 3 +Next token is token $undefined () +Error: discarding token $undefined () +Error: popping token error () +Stack now 0 1 +Shifting token error () +Entering state 3 +Reading a token: Now at end of input. +Cleanup: discarding lookahead token $end () +Stack now 0 1 3 +Cleanup: popping token error () +Cleanup: popping token 'a' ('a') +DESTROY 'a' +]]) + +AT_CLEANUP + + + +## ------------------------------------------------------ ## +## Default %printer and %destructor are not for $accept. ## +## ------------------------------------------------------ ## + +AT_SETUP([Default %printer and %destructor are not for \$accept]) + +# If YYSTYPE is a union and Bison were to apply the default %printer and +# %destructor to $accept: +# - The %printer and %destructor code generated for $accept would always be +# dead code because $accept is currently never shifted onto the stack. +# - $$ for $accept would always be of type YYSTYPE because it's not possible +# to declare `%type $accept'. (Also true for $undefined.) +# - Thus, the compiler might complain that the user code assumes the wrong +# type for $$ since the code might assume the type associated with a +# specific union field, which is especially reasonable in C++ since that +# type may be a base type. This test case checks for this problem. (Also +# true for $undefined and the error token, so there are three warnings for +# %printer and three for %destructor.) + +AT_DATA_GRAMMAR([[input.y]], +[[%debug /* So that %printer is actually compiled. */ + +%{ +# include + static void yyerror (const char *msg); + static int yylex (void); +# define USE(SYM) +%} + +%printer { + char chr = $$; + fprintf (yyoutput, "'%c'", chr); +} %symbol-default +%destructor { + char chr = $$; + fprintf (stderr, "DESTROY '%c'\n", chr); +} %symbol-default + +%union { char chr; } +%type start + +%% + +start: { USE($$); } ; + +%% + +static int +yylex (void) +{ + return 0; +} + +static void +yyerror (const char *msg) +{ + fprintf (stderr, "%s\n", msg); +} + +int +main (void) +{ + return yyparse (); +} +]]) + +AT_CHECK([bison -o input.c input.y]) +AT_COMPILE([input]) + +AT_CLEANUP