+## ----------------- ##
+## YYSTYPE typedef. ##
+## ----------------- ##
+
+AT_SETUP([YYSTYPE typedef])
+
+AT_DATA_GRAMMAR([input.y],
+[[%{
+void yyerror (char const *);
+int yylex (void);
+typedef union { char const *val; } YYSTYPE;
+%}
+
+%type <val> program
+
+%%
+
+program: { $$ = ""; };
+]])
+
+AT_CHECK([bison -o input.c input.y])
+AT_COMPILE([input.o], [-c input.c])
+
+AT_CLEANUP
+
+
+
## ------------------------------------- ##
## Early token definitions with --yacc. ##
## ------------------------------------- ##
[[%{
void yyerror (const char *s);
int yylex (void);
-#ifndef MY_TOKEN
-# error "MY_TOKEN not defined."
-#endif
%}
%union
{
int val;
};
+%{
+#ifndef MY_TOKEN
+# error "MY_TOKEN not defined."
+#endif
+%}
%token MY_TOKEN
%%
exp: MY_TOKEN;
void yyerror (const char *s);
int yylex (void);
void print_my_token (void);
+%}
+
+%union
+{
+ int val;
+};
+%{
void
print_my_token (void)
{
printf ("%d\n", my_token);
}
%}
-
-%union
-{
- int val;
-};
%token MY_TOKEN
%%
exp: MY_TOKEN;
input.y:6.1: invalid character: `%'
input.y:6.2: invalid character: `-'
input.y:7.1-8.0: missing `%}' at end of file
+input.y:7.1-8.0: syntax error, unexpected %{...%}
]])
AT_CLEANUP
AT_CHECK_EXPECT2()
AT_CHECK_EXPECT2([%glr-parser])
AT_CHECK_EXPECT2([%skeleton "lalr1.cc"])
+
+
+
+## --------------------------------------------- ##
+## Braced code in declaration in rules section. ##
+## --------------------------------------------- ##
+
+AT_SETUP([Braced code in declaration in rules section])
+
+# Bison once mistook braced code in a declaration in the rules section to be a
+# rule action.
+
+AT_DATA_GRAMMAR([input.y],
+[[%{
+#include <stdio.h>
+static void yyerror (char const *msg);
+static int yylex (void);
+%}
+
+%error-verbose
+
+%%
+
+start:
+ {
+ printf ("Bison would once convert this action to a midrule because of the"
+ " subsequent braced code.\n");
+ }
+ ;
+
+%destructor { fprintf (stderr, "DESTRUCTOR\n"); } 'a';
+%printer { fprintf (yyoutput, "PRINTER"); } 'a';
+
+%%
+
+static void
+yyerror (char const *msg)
+{
+ fprintf (stderr, "%s\n", msg);
+}
+
+static int
+yylex (void)
+{
+ return 'a';
+}
+
+int
+main (void)
+{
+ yydebug = 1;
+ return !yyparse ();
+}
+]])
+
+AT_CHECK([bison -t -o input.c input.y])
+AT_COMPILE([input])
+AT_PARSER_CHECK([./input], 0,
+[[Bison would once convert this action to a midrule because of the subsequent braced code.
+]],
+[[Starting parse
+Entering state 0
+Reducing stack by rule 1 (line 22):
+-> $$ = nterm start ()
+Stack now 0
+Entering state 1
+Reading a token: Next token is token 'a' (PRINTER)
+syntax error, unexpected 'a', expecting $end
+Error: popping nterm start ()
+Stack now 0
+Cleanup: discarding lookahead token 'a' (PRINTER)
+DESTRUCTOR
+Stack now 0
+]])
+
+AT_CLEANUP