]> git.saurik.com Git - bison.git/blob - tests/cxx-type.at
ded69471ec14517a8e299ec1aeeabbef3e2366e8
[bison.git] / tests / cxx-type.at
1 # Checking GLR Parsing. -*- Autotest -*-
2 # Copyright (C) 2002, 2003 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_CXXTYPES(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_CXXTYPES],
27 [
28 AT_BISON_OPTION_PUSHDEFS([$1])
29
30 AT_DATA_GRAMMAR([types.y],
31 [[/* Simplified C++ Type and Expression Grammar. */
32
33 $1
34
35 %{
36 #include <stdio.h>
37 #define YYSTYPE const char*
38 ]m4_bmatch([$2], [stmtMerge],
39 [ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
40 #define YYINITDEPTH 10
41 static char* format (const char*, ...);
42
43 %}
44
45 %token TYPENAME ID
46
47 %right '='
48 %left '+'
49
50 %glr-parser
51
52 %%
53
54 prog :
55 | prog stmt {
56 ]AT_LOCATION_IF([
57 printf ("%d.%d-%d.%d: ",
58 @2.first_line, @2.first_column,
59 @2.last_line, @2.last_column);])[
60 printf ("%s\n", ]$[2);
61 }
62 ;
63
64 stmt : expr ';' $2 { $$ = ]$[1; }
65 | decl $3
66 | error ';' { $$ = "<error>"; }
67 | '@' { YYACCEPT; }
68 ;
69
70 expr : ID
71 | TYPENAME '(' expr ')' { $$ = format ("<cast>(%s,%s)", ]$[3, ]$[1); }
72 | expr '+' expr { $$ = format ("+(%s,%s)", ]$[1, ]$[3); }
73 | expr '=' expr { $$ = format ("=(%s,%s)", ]$[1, ]$[3); }
74 ;
75
76 decl : TYPENAME declarator ';'
77 { $$ = format ("<declare>(%s,%s)", ]$[1, ]$[2); }
78 | TYPENAME declarator '=' expr ';'
79 { $$ = format ("<init-declare>(%s,%s,%s)", ]$[1, ]$[2, ]$[4); }
80 ;
81
82 declarator : ID
83 | '(' declarator ')' { $$ = ]$[2; }
84 ;
85
86 %%
87
88 #include <ctype.h>
89 #include <stdlib.h>
90 #include <string.h>
91 #include <stdarg.h>
92
93 int
94 main (int argc, char** argv)
95 {
96 if (argc != 2)
97 abort ();
98 if (!freopen (argv[1], "r", stdin))
99 abort ();
100 exit (yyparse ());
101 }
102
103 int
104 #if YYPURE && YYLSP_NEEDED
105 yylex (YYSTYPE *lvalp, YYLTYPE *llocp)
106 #elif YYPURE
107 yylex (YYSTYPE *lvalp)
108 #else
109 yylex ()
110 #endif
111 {
112 char buffer[256];
113 int c;
114 unsigned int i;
115 static int lineNum = 1;
116 static int colNum = 1;
117
118
119 #if YYPURE
120 # define yylloc (*llocp)
121 # define yylval (*lvalp)
122 #endif
123
124 while (1)
125 {
126 c = getchar ();
127 switch (c)
128 {
129 case EOF:
130 return 0;
131 case '\t':
132 colNum = 1 + ((colNum + 7) & ~7);
133 break;
134 case ' ': case '\f':
135 colNum += 1;
136 break;
137 case '\n':
138 lineNum += 1;
139 colNum = 1;
140 break;
141 default:
142 {
143 int tok;
144 #if YYLSP_NEEDED
145 yylloc.first_line = yylloc.last_line = lineNum;
146 yylloc.first_column = colNum;
147 #endif
148 if (isalpha (c))
149 {
150 i = 0;
151
152 do
153 {
154 buffer[i++] = c;
155 colNum += 1;
156 if (i == sizeof buffer - 1)
157 abort ();
158 c = getchar ();
159 }
160 while (isalnum (c) || c == '_');
161
162 ungetc (c, stdin);
163 buffer[i++] = 0;
164 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
165 yylval = strcpy (malloc (i), buffer);
166 }
167 else
168 {
169 colNum += 1;
170 tok = c;
171 yylval = "";
172 }
173 #if YYLSP_NEEDED
174 yylloc.last_column = colNum-1;
175 #endif
176 return tok;
177 }
178 }
179 }
180 }
181
182 int
183 yyerror (
184 #if YYPURE && YYLSP_NEEDED
185 YYLTYPE *llocp,
186 #endif
187 const char *s
188 )
189 {
190 fprintf (stderr, "%s\n", s);
191 return 0;
192 }
193
194
195 static char* format (const char* form, ...)
196 {
197 char buffer[1024];
198 va_list args;
199 va_start (args, form);
200 vsprintf (buffer, form, args);
201 va_end (args);
202 return strcpy (malloc (strlen (buffer) + 1), buffer);
203 }
204
205 ]]
206 m4_bmatch([$2], [stmtMerge],
207 [[static YYSTYPE
208 stmtMerge (YYSTYPE x0, YYSTYPE x1)
209 {
210 return format ("<OR>(%s,%s)", x0, x1);
211 }
212 ]])
213 )
214
215 AT_DATA([test-input],
216 [[
217
218 z + q;
219
220 T x;
221
222 T x = y;
223
224 x = y;
225
226 T (x) + y;
227
228 T (x);
229
230 T (y) = z + q;
231
232 T (y y) = z + q;
233
234 z + q;
235
236 @
237
238 This is total garbage, but it should be ignored.
239 ]])
240
241 AT_CHECK([bison -o types.c types.y], 0, [], ignore)
242 AT_COMPILE([types])
243 AT_BISON_OPTION_POPDEFS
244 ])
245
246 m4_define([_AT_RESOLVED_GLR_OUTPUT],
247 [[+(z,q)
248 <declare>(T,x)
249 <init-declare>(T,x,y)
250 =(x,y)
251 +(<cast>(x,T),y)
252 <declare>(T,x)
253 <init-declare>(T,y,+(z,q))
254 <error>
255 +(z,q)
256 ]])
257
258 m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
259 [[3.1-3.6: +(z,q)
260 5.1-5.4: <declare>(T,x)
261 7.1-7.8: <init-declare>(T,x,y)
262 9.1-9.6: =(x,y)
263 11.1-11.10: +(<cast>(x,T),y)
264 13.1-13.6: <declare>(T,x)
265 15.1-15.14: <init-declare>(T,y,+(z,q))
266 17.6-17.16: <error>
267 19.1-19.6: +(z,q)
268 ]])
269
270 m4_define([_AT_AMBIG_GLR_OUTPUT],
271 [[+(z,q)
272 <declare>(T,x)
273 <init-declare>(T,x,y)
274 =(x,y)
275 +(<cast>(x,T),y)
276 <OR>(<declare>(T,x),<cast>(x,T))
277 <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
278 <error>
279 +(z,q)
280 ]])
281
282 m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
283 [[3.1-3.6: +(z,q)
284 5.1-5.4: <declare>(T,x)
285 7.1-7.8: <init-declare>(T,x,y)
286 9.1-9.6: =(x,y)
287 11.1-11.10: +(<cast>(x,T),y)
288 13.1-13.6: <OR>(<declare>(T,x),<cast>(x,T))
289 15.1-15.14: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
290 17.6-17.16: <error>
291 19.1-19.6: +(z,q)
292 ]])
293
294 m4_define([_AT_GLR_STDERR],
295 [[syntax error
296 ]])
297
298 m4_define([_AT_VERBOSE_GLR_STDERR],
299 [[syntax error, unexpected ID, expecting '=' or '+' or ')'
300 ]])
301
302 ## ---------------------------------------------------- ##
303 ## Compile the grammar described in the documentation. ##
304 ## ---------------------------------------------------- ##
305
306 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
307 _AT_TEST_GLR_CXXTYPES([],
308 [%dprec 1], [%dprec 2])
309 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
310 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
311 AT_CLEANUP
312
313 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
314 _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
315 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
316 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
317 AT_CLEANUP
318
319 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
320 _AT_TEST_GLR_CXXTYPES([%pure-parser],
321 [%dprec 1], [%dprec 2])
322 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
323 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
324 AT_CLEANUP
325
326 AT_SETUP([GLR: Resolve ambiguity, pure, locations])
327 _AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
328 [%dprec 1], [%dprec 2])
329 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
330 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
331 AT_CLEANUP
332
333 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
334 _AT_TEST_GLR_CXXTYPES([],
335 [%merge <stmtMerge>], [%merge <stmtMerge>])
336 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
337 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
338 AT_CLEANUP
339
340 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
341 _AT_TEST_GLR_CXXTYPES([%locations],
342 [%merge <stmtMerge>], [%merge <stmtMerge>])
343 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
344 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
345 AT_CLEANUP
346
347 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
348 _AT_TEST_GLR_CXXTYPES([%pure-parser],
349 [%merge <stmtMerge>], [%merge <stmtMerge>])
350 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
351 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
352 AT_CLEANUP
353 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
354 _AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
355 [%merge <stmtMerge>],[%merge <stmtMerge>])
356 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
357 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
358 AT_CLEANUP
359
360 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
361 _AT_TEST_GLR_CXXTYPES([%error-verbose],
362 [%merge <stmtMerge>], [%merge <stmtMerge>])
363 AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
364 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)
365 AT_CLEANUP