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