X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/6100a9aa2e68e992396e3e1da954921dc1fee801..965537bc543191a048b6afb2404c878f06ab877f:/tests/actions.at diff --git a/tests/actions.at b/tests/actions.at index 55e51c6f..d84c7ee8 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. @@ -487,7 +532,7 @@ sending: '(' (18@180-189) sending: 'x' (19@190-199) thing (19@190-199): 'x' (19@190-199) sending: ')' (20@200-209) -200-209: parser stack overflow +200-209: memory exhausted Freeing nterm thing (19@190-199) Freeing nterm line (15@150-179) Freeing nterm line (12@120-149) @@ -530,3 +575,202 @@ 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); +} +%destructor { + fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column); +} + +%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 + + + + + +## ------------------------------------------------------------- ## +## Default %printer and %destructor for user-declared end token. ## +## ------------------------------------------------------------- ## + +AT_SETUP([Default %printer and %destructor for user-declared 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); +} +%destructor { + fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column); +} + +%% + +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