static int
yylex (void)
{
- static const char *input = "123456789";
- return *input++;
+ static char const input[] = "123456789";
+ static size_t toknum;
+ if (! (toknum < sizeof input))
+ abort ();
+ return input[toknum++];
}
static void
static int
yylex (void)
{
+ static int called;
+ if (called++)
+ abort ();
return EOF;
}
[[%start-header {
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <assert.h>
#define YYINITDEPTH 10
int ival;
}])
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])])
+m4_ifval([$6], [[%end-header {]], [[%after-header {]])
+AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])
[static int yylex (]AT_LEX_FORMALS[);
]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
[}
AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
])[
+ if (! (0 <= c && c <= strlen (source)))
+ abort ();
if (source[c])
printf ("sending: '%c'", source[c]);
else
AT_CHECK_PRINTER_AND_DESTRUCTOR([])
AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
-# These tests currently fail on a Debian GNU/Linux 3.0r2 x86 host,
-# but the 2nd test succeeds on a Solaris 9 sparc hosts (Forte 7 cc).
-# Skip them until we figure out what the problem is.
AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
%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);
static int
yylex (void)
{
- static const char *input = "abcd";
- static int column = 1;
- yylval = *input++;
+ static char const input[] = "abcd";
+ static size_t toknum;
+ if (! (toknum < sizeof input))
+ abort ();
+ yylval = input[toknum++];
yylloc.first_line = yylloc.last_line = 1;
- yylloc.first_column = yylloc.last_column = column++;
+ yylloc.first_column = yylloc.last_column = toknum;
return yylval;
}
+## ----------------------------------- ##
+## Per-type %printer and %destructor. ##
+## ----------------------------------- ##
+
+AT_SETUP([Per-type %printer and %destructor])
+
+AT_DATA_GRAMMAR([[input.y]],
+[[%error-verbose
+%debug
+
+%{
+# include <stdio.h>
+# include <stdlib.h>
+ static void yyerror (const char *msg);
+ static int yylex (void);
+# define USE(SYM)
+%}
+
+%union { int field0; int field1; int field2; }
+%type <field0> start 'a' 'g'
+%type <field1> 'e'
+%type <field2> 'f'
+%printer {
+ fprintf (yyoutput, "%%symbol-default/<field2>/e printer");
+} %symbol-default 'e' <field2>
+%destructor {
+ fprintf (stdout, "%%symbol-default/<field2>/e destructor.\n");
+} %symbol-default 'e' <field2>
+
+%type <field1> 'b'
+%printer { fprintf (yyoutput, "<field1> printer"); } <field1>
+%destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
+
+%type <field0> 'c'
+%printer { fprintf (yyoutput, "'c' printer"); } 'c'
+%destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
+
+%type <field1> '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 char const input[] = "abcdef";
+ static size_t toknum;
+ if (! (toknum < sizeof input))
+ abort ();
+ return input[toknum++];
+}
+
+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/<field2>/e destructor.
+%symbol-default/<field2>/e destructor.
+'d' destructor.
+'c' destructor.
+<field1> destructor.
+%symbol-default/<field2>/e destructor.
+]],
+[[Starting parse
+Entering state 0
+Reading a token: Next token is token 'a' (%symbol-default/<field2>/e printer)
+Shifting token 'a' (%symbol-default/<field2>/e printer)
+Entering state 1
+Reading a token: Next token is token 'b' (<field1> printer)
+Shifting token 'b' (<field1> 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/<field2>/e printer)
+Shifting token 'e' (%symbol-default/<field2>/e printer)
+Entering state 7
+Reading a token: Next token is token 'f' (%symbol-default/<field2>/e printer)
+Shifting token 'f' (%symbol-default/<field2>/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/<field2>/e printer)
+Stack now 0 1 3 5 6 7
+Error: popping token 'e' (%symbol-default/<field2>/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' (<field1> printer)
+Stack now 0 1
+Error: popping token 'a' (%symbol-default/<field2>/e printer)
+Stack now 0
+Cleanup: discarding lookahead token $end ()
+Stack now 0
+]])
+
+AT_CLEANUP
+
+
+
## ------------------------------------------------------------- ##
-## Default %printer and %destructor for user-declared end token. ##
+## Default %printer and %destructor for user-defined end token. ##
## ------------------------------------------------------------- ##
-AT_SETUP([Default %printer and %destructor for user-declared end token])
+AT_SETUP([Default %printer and %destructor for user-defined end token])
AT_DATA_GRAMMAR([[input.y]],
[[%error-verbose
%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
%%
static int
yylex (void)
{
+ static int called;
+ if (called++)
+ abort ();
yylval = 'E';
yylloc.first_line = yylloc.last_line = 1;
yylloc.first_column = yylloc.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, "'%c'", $$);
-}
+} %symbol-default
%destructor {
fprintf (stderr, "DESTROY '%c'\n", $$);
-}
+} %symbol-default
%%
static int
yylex (void)
{
- static const char *input = "abd";
- yylval = *input++;
+ static char const input[] = "abd";
+ static size_t toknum;
+ if (! (toknum < sizeof input))
+ abort ();
+ yylval = input[toknum++];
return yylval;
}
%{
# include <stdio.h>
+# include <stdlib.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
static int
yylex (void)
{
+ static int called;
+ if (called++)
+ abort ();
return 0;
}