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