]> git.saurik.com Git - bison.git/blob - tests/cxx-type.at
* data/c.m4 (b4_identification, b4_user_args, b4_parse_param):
[bison.git] / tests / cxx-type.at
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
19 AT_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.
26 m4_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 (const char *s
40 #if YYPURE && YYLSP_NEEDED
41 , YYLTYPE *yylocation
42 #endif
43 );
44
45 #if YYPURE
46 ]m4_bmatch([$1], [location],
47 [ int yylex (YYSTYPE *lvalp, YYLTYPE *llocp);],
48 [ int yylex (YYSTYPE *lvalp);])[
49 #else
50 int yylex (void);
51 #endif
52
53 %}
54
55 %token TYPENAME ID
56
57 %right '='
58 %left '+'
59
60 %glr-parser
61
62 %%
63
64 prog :
65 | prog stmt { printf ("\n"); }
66 ;
67
68 stmt : expr ';' $2
69 | decl $3
70 | error ';'
71 | '@' { YYACCEPT; }
72 ;
73
74 expr : ID { printf ("%s ", $$); }
75 | TYPENAME '(' expr ')'
76 { printf ("%s <cast> ", ]$[1); }
77 | expr '+' expr { printf ("+ "); }
78 | expr '=' expr { printf ("= "); }
79 ;
80
81 decl : TYPENAME declarator ';'
82 { printf ("%s <declare> ", ]$[1); }
83 | TYPENAME declarator '=' expr ';'
84 { printf ("%s <init-declare> ", ]$[1); }
85 ;
86
87 declarator : ID { printf ("\"%s\" ", ]$[1); }
88 | '(' declarator ')'
89 ;
90
91 %%
92
93 #include <assert.h>
94 #include <ctype.h>
95 #include <stdlib.h>
96 #include <string.h>
97
98 int
99 main (int argc, char** argv)
100 {
101 assert (argc == 2);
102 if (!freopen (argv[1], "r", stdin))
103 abort ();
104 exit (yyparse ());
105 }
106
107 #if YYPURE
108 int
109 ]m4_bmatch([$1], [location],
110 [yylex (YYSTYPE *lvalp, YYLTYPE *llocp)],
111 [yylex (YYSTYPE *lvalp)])[
112 #else
113 int
114 yylex ()
115 #endif
116 {
117 char buffer[256];
118 int c;
119 unsigned int i;
120
121 #if YYPURE
122 # define yylval (*lvalp)
123 ]m4_bmatch([$1], [location],[ (void) llocp;])[
124 #endif
125
126 while (1)
127 {
128 c = getchar ();
129 switch (c)
130 {
131 case EOF:
132 return 0;
133 case ' ': case '\t': case '\n': case '\f':
134 break;
135 default:
136 if (isalpha (c))
137 {
138 i = 0;
139
140 do
141 {
142 buffer[i++] = c;
143 if (i == sizeof buffer - 1)
144 abort ();
145 c = getchar ();
146 }
147 while (isalnum (c) || c == '_');
148
149 ungetc (c, stdin);
150 buffer[i++] = 0;
151 yylval = strcpy (malloc (i), buffer);
152 return isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
153 }
154 return c;
155 }
156 }
157 }
158
159 int
160 yyerror (const char *s
161 #if YYPURE && YYLSP_NEEDED
162 , YYLTYPE *yylocation
163 #endif
164 )
165 {
166 #if YYPURE && YYLSP_NEEDED
167 (void) *yylocation;
168 #endif
169 fprintf (stderr, "%s\n", s);
170 return 0;
171 }
172
173 ]]
174 m4_bmatch([$2], [stmtMerge],
175 [[static YYSTYPE
176 stmtMerge (YYSTYPE x0, YYSTYPE x1)
177 {
178 /* Use the arguments. */
179 (void) x0;
180 (void) x1;
181 printf ("<OR> ");
182 return "";
183 }
184 ]])
185 )
186
187 AT_DATA([test-input],
188 [[
189
190 z + q;
191
192 T x;
193
194 T x = y;
195
196 x = y;
197
198 T (x) + y;
199
200 T (x);
201
202 T (y) = z + q;
203
204 T (y y) = z + q;
205
206 z + q;
207
208 @
209
210 This is total garbage, but it should be ignored.
211 ]])
212
213 AT_CHECK([bison -o types.c types.y], 0, [], ignore)
214 AT_COMPILE([types])
215 ])
216
217 m4_define([_AT_RESOLVED_GLR_OUTPUT],
218 [[z q +
219 "x" T <declare>
220 "x" y T <init-declare>
221 x y =
222 x T <cast> y +
223 "x" T <declare>
224 "y" z q + T <init-declare>
225 y
226 z q +
227 ]])
228
229 m4_define([_AT_AMBIG_GLR_OUTPUT],
230 [[z q +
231 "x" T <declare>
232 "x" y T <init-declare>
233 x y =
234 x T <cast> y +
235 "x" T <declare> x T <cast> <OR>
236 "y" z q + T <init-declare> y T <cast> z q + = <OR>
237 y
238 z q +
239 ]])
240
241 m4_define([_AT_GLR_STDERR],
242 [[parse error
243 ]])
244
245 m4_define([_AT_VERBOSE_GLR_STDERR],
246 [[parse error, unexpected ID, expecting '=' or '+' or ')'
247 ]])
248
249 ## ---------------------------------------------------- ##
250 ## Compile the grammar described in the documentation. ##
251 ## ---------------------------------------------------- ##
252
253 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
254 _AT_TEST_GLR_CALC([],[%dprec 1],[%dprec 2])
255 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
256 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
257 AT_CLEANUP
258
259 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
260 _AT_TEST_GLR_CALC([%locations],[%dprec 1],[%dprec 2])
261 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
262 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
263 AT_CLEANUP
264
265 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
266 _AT_TEST_GLR_CALC([%pure-parser],[%dprec 1],[%dprec 2])
267 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
268 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
269 AT_CLEANUP
270
271 AT_SETUP([GLR: Resolve ambiguity, pure, locations])
272 _AT_TEST_GLR_CALC([%pure-parser
273 %locations],[%dprec 1],[%dprec 2])
274 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
275 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
276 AT_CLEANUP
277
278 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
279 _AT_TEST_GLR_CALC([],[%merge <stmtMerge>],[%merge <stmtMerge>])
280 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
281 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
282 AT_CLEANUP
283
284 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
285 _AT_TEST_GLR_CALC([%locations],[%merge <stmtMerge>],[%merge <stmtMerge>])
286 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
287 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
288 AT_CLEANUP
289
290 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
291 _AT_TEST_GLR_CALC([%pure-parser],[%merge <stmtMerge>],[%merge <stmtMerge>])
292 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
293 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
294 AT_CLEANUP
295 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
296 _AT_TEST_GLR_CALC([%pure-parser
297 %locations],[%merge <stmtMerge>],[%merge <stmtMerge>])
298 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
299 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
300 AT_CLEANUP
301
302 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
303 _AT_TEST_GLR_CALC([%error-verbose],
304 [%merge <stmtMerge>],[%merge <stmtMerge>])
305 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
306 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)
307 AT_CLEANUP