]> git.saurik.com Git - bison.git/blame_incremental - tests/cxx-type.at
Let yyerror always receive the msg as last argument, so that
[bison.git] / tests / cxx-type.at
... / ...
CommitLineData
1# Checking GLR Parsing. -*- Autotest -*-
2# Copyright (C) 2002 Free Software Foundation, Inc.
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2, or (at your option)
7# any later version.
8
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17# 02111-1307, USA.
18
19AT_BANNER([[C++ Type Syntax (GLR).]])
20
21# _AT_TEST_GLR_CALC(DECL, RESOLVE1, RESOLVE2)
22# -------------------------------------------
23# Store into types.y the calc program, with DECL inserted as a declaration,
24# and with RESOLVE1 and RESOLVE2 as annotations on the conflicted rule for
25# stmt. Then compile the result.
26m4_define([_AT_TEST_GLR_CALC],
27[AT_DATA([types.y],
28[[/* Simplified C++ Type and Expression Grammar. */
29
30$1
31
32%{
33 #include <stdio.h>
34 #define YYSTYPE const char*
35 #define YYLTYPE int
36]m4_bmatch([$2], [stmtMerge],
37[ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
38 #define YYINITDEPTH 10
39 int yyerror (
40#if YYPURE && YYLSP_NEEDED
41 YYLTYPE *yylocation,
42#endif
43 const char *s
44 );
45
46 #if YYPURE
47]m4_bmatch([$1], [location],
48[ int yylex (YYSTYPE *lvalp, YYLTYPE *llocp);],
49[ int yylex (YYSTYPE *lvalp);])[
50 #else
51 int yylex (void);
52 #endif
53
54%}
55
56%token TYPENAME ID
57
58%right '='
59%left '+'
60
61%glr-parser
62
63%%
64
65prog :
66 | prog stmt { printf ("\n"); }
67 ;
68
69stmt : expr ';' $2
70 | decl $3
71 | error ';'
72 | '@' { YYACCEPT; }
73 ;
74
75expr : ID { printf ("%s ", $$); }
76 | TYPENAME '(' expr ')'
77 { printf ("%s <cast> ", ]$[1); }
78 | expr '+' expr { printf ("+ "); }
79 | expr '=' expr { printf ("= "); }
80 ;
81
82decl : TYPENAME declarator ';'
83 { printf ("%s <declare> ", ]$[1); }
84 | TYPENAME declarator '=' expr ';'
85 { printf ("%s <init-declare> ", ]$[1); }
86 ;
87
88declarator : ID { printf ("\"%s\" ", ]$[1); }
89 | '(' declarator ')'
90 ;
91
92%%
93
94#include <assert.h>
95#include <ctype.h>
96#include <stdlib.h>
97#include <string.h>
98
99int
100main (int argc, char** argv)
101{
102 assert (argc == 2);
103 if (!freopen (argv[1], "r", stdin))
104 abort ();
105 exit (yyparse ());
106}
107
108#if YYPURE
109int
110]m4_bmatch([$1], [location],
111[yylex (YYSTYPE *lvalp, YYLTYPE *llocp)],
112[yylex (YYSTYPE *lvalp)])[
113#else
114int
115yylex ()
116#endif
117{
118 char buffer[256];
119 int c;
120 unsigned int i;
121
122#if YYPURE
123# define yylval (*lvalp)
124]m4_bmatch([$1], [location],[ (void) llocp;])[
125#endif
126
127 while (1)
128 {
129 c = getchar ();
130 switch (c)
131 {
132 case EOF:
133 return 0;
134 case ' ': case '\t': case '\n': case '\f':
135 break;
136 default:
137 if (isalpha (c))
138 {
139 i = 0;
140
141 do
142 {
143 buffer[i++] = c;
144 if (i == sizeof buffer - 1)
145 abort ();
146 c = getchar ();
147 }
148 while (isalnum (c) || c == '_');
149
150 ungetc (c, stdin);
151 buffer[i++] = 0;
152 yylval = strcpy (malloc (i), buffer);
153 return isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
154 }
155 return c;
156 }
157 }
158}
159
160int
161yyerror (
162#if YYPURE && YYLSP_NEEDED
163 YYLTYPE *yylocation,
164#endif
165 const char *s
166 )
167{
168#if YYPURE && YYLSP_NEEDED
169 (void) *yylocation;
170#endif
171 fprintf (stderr, "%s\n", s);
172 return 0;
173}
174
175]]
176m4_bmatch([$2], [stmtMerge],
177[[static YYSTYPE
178stmtMerge (YYSTYPE x0, YYSTYPE x1)
179{
180 /* Use the arguments. */
181 (void) x0;
182 (void) x1;
183 printf ("<OR> ");
184 return "";
185}
186]])
187)
188
189AT_DATA([test-input],
190[[
191
192z + q;
193
194T x;
195
196T x = y;
197
198x = y;
199
200T (x) + y;
201
202T (x);
203
204T (y) = z + q;
205
206T (y y) = z + q;
207
208z + q;
209
210@
211
212This is total garbage, but it should be ignored.
213]])
214
215AT_CHECK([bison -o types.c types.y], 0, [], ignore)
216AT_COMPILE([types])
217])
218
219m4_define([_AT_RESOLVED_GLR_OUTPUT],
220[[z q +
221"x" T <declare>
222"x" y T <init-declare>
223x y =
224x T <cast> y +
225"x" T <declare>
226"y" z q + T <init-declare>
227y
228z q +
229]])
230
231m4_define([_AT_AMBIG_GLR_OUTPUT],
232[[z q +
233"x" T <declare>
234"x" y T <init-declare>
235x y =
236x T <cast> y +
237"x" T <declare> x T <cast> <OR>
238"y" z q + T <init-declare> y T <cast> z q + = <OR>
239y
240z q +
241]])
242
243m4_define([_AT_GLR_STDERR],
244[[parse error
245]])
246
247m4_define([_AT_VERBOSE_GLR_STDERR],
248[[parse error, unexpected ID, expecting '=' or '+' or ')'
249]])
250
251## ---------------------------------------------------- ##
252## Compile the grammar described in the documentation. ##
253## ---------------------------------------------------- ##
254
255AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
256_AT_TEST_GLR_CALC([],[%dprec 1],[%dprec 2])
257AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
258 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
259AT_CLEANUP
260
261AT_SETUP([GLR: Resolve ambiguity, impure, locations])
262_AT_TEST_GLR_CALC([%locations],[%dprec 1],[%dprec 2])
263AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
264 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
265AT_CLEANUP
266
267AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
268_AT_TEST_GLR_CALC([%pure-parser],[%dprec 1],[%dprec 2])
269AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
270 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
271AT_CLEANUP
272
273AT_SETUP([GLR: Resolve ambiguity, pure, locations])
274_AT_TEST_GLR_CALC([%pure-parser
275%locations],[%dprec 1],[%dprec 2])
276AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
277 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
278AT_CLEANUP
279
280AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
281_AT_TEST_GLR_CALC([],[%merge <stmtMerge>],[%merge <stmtMerge>])
282AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
283 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
284AT_CLEANUP
285
286AT_SETUP([GLR: Merge conflicting parses, impure, locations])
287_AT_TEST_GLR_CALC([%locations],[%merge <stmtMerge>],[%merge <stmtMerge>])
288AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
289 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
290AT_CLEANUP
291
292AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
293_AT_TEST_GLR_CALC([%pure-parser],[%merge <stmtMerge>],[%merge <stmtMerge>])
294AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
295 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
296AT_CLEANUP
297AT_SETUP([GLR: Merge conflicting parses, pure, locations])
298_AT_TEST_GLR_CALC([%pure-parser
299%locations],[%merge <stmtMerge>],[%merge <stmtMerge>])
300AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
301 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
302AT_CLEANUP
303
304AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
305_AT_TEST_GLR_CALC([%error-verbose],
306[%merge <stmtMerge>],[%merge <stmtMerge>])
307AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
308 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)
309AT_CLEANUP