# 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 <stdio.h>
#include <stdlib.h>
#include <assert.h>
]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([%define "global_tokens_and_yystype"])
-[
-%{
-]AT_LALR1_CC_IF([typedef yy::location YYLTYPE;
+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 <ival> '(' 'x' 'y' ')' ';' thing line input])[
+]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
+/* FIXME: This %printer isn't actually tested. */
%printer
{
]AT_LALR1_CC_IF([debug_stream () << $$;],
{ 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.
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",
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 source[c];
}
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.
]])
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.
]])
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.
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 <stdio.h>
+# include <stdlib.h>
+ 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
+
+
+
+## ------------------------------------------------------------- ##
+## 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 <stdio.h>
+# include <stdlib.h>
+ 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 <stdio.h>
+ 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 <field> $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 <stdio.h>
+ 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 <chr> 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