]> git.saurik.com Git - bison.git/blob - tests/cxx-type.at
* tests/cxx-type.at: Construct a tree, count the parents of shared
[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 union Node {
38 struct {
39 int type;
40 int parents;
41 } node_info;
42 struct {
43 int type; /* 1 */
44 int parents;
45 char const *form;
46 union Node *children[3];
47 } nterm;
48 struct {
49 int type; /* 0 */
50 int parents;
51 char *text;
52 } term;
53 };
54 typedef union Node Node;
55 static Node *new_nterm (char const *, Node *, Node *, Node *);
56 static Node *new_term (char *);
57 static void free_node (Node *);
58 static char *node_to_string (Node *);
59 #define YYSTYPE Node *
60 ]m4_bmatch([$2], [stmtMerge],
61 [ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
62 #define YYINITDEPTH 10
63 #define YYSTACKEXPANDABLE 1
64 struct YYLTYPE;
65 #if YYPURE
66 # if YYLSP_NEEDED
67 # define LEX_PARAMETERS YYSTYPE *lvalp, struct YYLTYPE *llocp
68 # define ERROR_PARAMETERS struct YYLTYPE *llocp, char const *s
69 # else
70 # define LEX_PARAMETERS YYSTYPE *lvalp
71 # endif
72 #endif
73 #ifndef LEX_PARAMETERS
74 # define LEX_PARAMETERS void
75 #endif
76 #ifndef ERROR_PARAMETERS
77 # define ERROR_PARAMETERS char const *s
78 #endif
79 int yylex (LEX_PARAMETERS);
80 void yyerror (ERROR_PARAMETERS);
81 %}
82
83 %token TYPENAME ID
84
85 %right '='
86 %left '+'
87
88 %glr-parser
89
90 %destructor { free_node ($$); } stmt expr decl declarator TYPENAME ID
91
92 %%
93
94 prog :
95 | prog stmt {
96 char *output;]AT_LOCATION_IF([
97 printf ("%d.%d-%d.%d: ",
98 @2.first_line, @2.first_column,
99 @2.last_line, @2.last_column);])[
100 output = node_to_string (]$[2);
101 printf ("%s\n", output);
102 free (output);
103 free_node (]$[2);
104 }
105 ;
106
107 stmt : expr ';' $2 { $$ = ]$[1; }
108 | decl $3
109 | error ';' { $$ = new_nterm ("<error>", 0, 0, 0); }
110 | '@' { YYACCEPT; }
111 ;
112
113 expr : ID
114 | TYPENAME '(' expr ')'
115 { $$ = new_nterm ("<cast>(%s,%s)", ]$[3, ]$[1, 0); }
116 | expr '+' expr { $$ = new_nterm ("+(%s,%s)", ]$[1, ]$[3, 0); }
117 | expr '=' expr { $$ = new_nterm ("=(%s,%s)", ]$[1, ]$[3, 0); }
118 ;
119
120 decl : TYPENAME declarator ';'
121 { $$ = new_nterm ("<declare>(%s,%s)", ]$[1, ]$[2, 0); }
122 | TYPENAME declarator '=' expr ';'
123 { $$ = new_nterm ("<init-declare>(%s,%s,%s)", ]$[1,
124 ]$[2, ]$[4); }
125 ;
126
127 declarator : ID
128 | '(' declarator ')' { $$ = ]$[2; }
129 ;
130
131 %%
132
133 #include <ctype.h>
134 #include <stdlib.h>
135 #include <string.h>
136 #include <stdarg.h>
137
138 int
139 main (int argc, char **argv)
140 {
141 if (argc != 2)
142 abort ();
143 if (!freopen (argv[1], "r", stdin))
144 return 3;
145 return yyparse ();
146 }
147
148 int
149 yylex (LEX_PARAMETERS)
150 {
151 char buffer[256];
152 int c;
153 unsigned int i;
154 static int lineNum = 1;
155 static int colNum = 0;
156
157 #if YYPURE
158 # define yylloc (*llocp)
159 # define yylval (*lvalp)
160 #endif
161
162 while (1)
163 {
164 c = getchar ();
165 switch (c)
166 {
167 case EOF:
168 return 0;
169 case '\t':
170 colNum = (colNum + 7) & ~7;
171 break;
172 case ' ': case '\f':
173 colNum += 1;
174 break;
175 case '\n':
176 lineNum += 1;
177 colNum = 0;
178 break;
179 default:
180 {
181 int tok;
182 #if YYLSP_NEEDED
183 yylloc.first_line = yylloc.last_line = lineNum;
184 yylloc.first_column = colNum;
185 #endif
186 if (isalpha (c))
187 {
188 i = 0;
189
190 do
191 {
192 buffer[i++] = c;
193 colNum += 1;
194 if (i == sizeof buffer - 1)
195 abort ();
196 c = getchar ();
197 }
198 while (isalnum (c) || c == '_');
199
200 ungetc (c, stdin);
201 buffer[i++] = 0;
202 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
203 yylval = new_term (strcpy ((char *) malloc (i), buffer));
204 }
205 else
206 {
207 colNum += 1;
208 tok = c;
209 yylval = 0;
210 }
211 #if YYLSP_NEEDED
212 yylloc.last_column = colNum-1;
213 #endif
214 return tok;
215 }
216 }
217 }
218 }
219
220 void
221 yyerror (ERROR_PARAMETERS)
222 {
223 #if YYPURE && YYLSP_NEEDED
224 /* Pacify GCC by using llocp. */
225 if (! llocp)
226 abort ();
227 #endif
228 fprintf (stderr, "%s\n", s);
229 }
230
231 static Node *
232 new_nterm (char const *form, Node *child0, Node *child1, Node *child2)
233 {
234 Node *node = malloc (sizeof (Node));
235 node->nterm.type = 1;
236 node->nterm.parents = 0;
237 node->nterm.form = form;
238 node->nterm.children[0] = child0;
239 if (child0)
240 child0->node_info.parents += 1;
241 node->nterm.children[1] = child1;
242 if (child1)
243 child1->node_info.parents += 1;
244 node->nterm.children[2] = child2;
245 if (child2)
246 child2->node_info.parents += 1;
247 return node;
248 }
249
250 static Node *
251 new_term (char *text)
252 {
253 Node *node = malloc (sizeof (Node));
254 node->term.type = 0;
255 node->term.parents = 0;
256 node->term.text = text;
257 return node;
258 }
259
260 static void
261 free_node (Node *node)
262 {
263 if (!node)
264 return;
265 node->node_info.parents -= 1;
266 /* Free only if 0 (last parent) or -1 (no parents). */
267 if (node->node_info.parents > 0)
268 return;
269 if (node->node_info.type == 1)
270 {
271 free_node (node->nterm.children[0]);
272 free_node (node->nterm.children[1]);
273 free_node (node->nterm.children[2]);
274 }
275 else
276 free (node->term.text);
277 free (node);
278 }
279
280 static char *
281 node_to_string (Node *node)
282 {
283 char *child0;
284 char *child1;
285 char *child2;
286 char *buffer;
287 if (!node)
288 {
289 buffer = malloc (1);
290 buffer[0] = 0;
291 }
292 else if (node->node_info.type == 1)
293 {
294 child0 = node_to_string (node->nterm.children[0]);
295 child1 = node_to_string (node->nterm.children[1]);
296 child2 = node_to_string (node->nterm.children[2]);
297 buffer = malloc (strlen (node->nterm.form) + strlen (child0)
298 + strlen (child1) + strlen (child2) + 1);
299 sprintf (buffer, node->nterm.form, child0, child1, child2);
300 free (child0);
301 free (child1);
302 free (child2);
303 }
304 else
305 buffer = strdup (node->term.text);
306 return buffer;
307 }
308
309 ]]
310 m4_bmatch([$2], [stmtMerge],
311 [[static YYSTYPE
312 stmtMerge (YYSTYPE x0, YYSTYPE x1)
313 {
314 return new_nterm ("<OR>(%s,%s)", x0, x1, 0);
315 }
316 ]])
317 )
318
319 AT_DATA([test-input],
320 [[
321
322 z + q;
323
324 T x;
325
326 T x = y;
327
328 x = y;
329
330 T (x) + y;
331
332 T (x);
333
334 T (y) = z + q;
335
336 T (y y) = z + q;
337
338 z + q;
339
340 @
341
342 This is total garbage, but it should be ignored.
343 ]])
344
345 AT_CHECK([bison -o types.c types.y], 0, [], ignore)
346 AT_COMPILE([types])
347 AT_BISON_OPTION_POPDEFS
348 ])
349
350 m4_define([_AT_RESOLVED_GLR_OUTPUT],
351 [[+(z,q)
352 <declare>(T,x)
353 <init-declare>(T,x,y)
354 =(x,y)
355 +(<cast>(x,T),y)
356 <declare>(T,x)
357 <init-declare>(T,y,+(z,q))
358 <error>
359 +(z,q)
360 ]])
361
362 m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
363 [[3.0-3.5: +(z,q)
364 5.0-5.3: <declare>(T,x)
365 7.0-7.7: <init-declare>(T,x,y)
366 9.0-9.5: =(x,y)
367 11.0-11.9: +(<cast>(x,T),y)
368 13.0-13.5: <declare>(T,x)
369 15.0-15.13: <init-declare>(T,y,+(z,q))
370 17.0-17.15: <error>
371 19.0-19.5: +(z,q)
372 ]])
373
374 m4_define([_AT_AMBIG_GLR_OUTPUT],
375 [[+(z,q)
376 <declare>(T,x)
377 <init-declare>(T,x,y)
378 =(x,y)
379 +(<cast>(x,T),y)
380 <OR>(<declare>(T,x),<cast>(x,T))
381 <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
382 <error>
383 +(z,q)
384 ]])
385
386 m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
387 [[3.0-3.5: +(z,q)
388 5.0-5.3: <declare>(T,x)
389 7.0-7.7: <init-declare>(T,x,y)
390 9.0-9.5: =(x,y)
391 11.0-11.9: +(<cast>(x,T),y)
392 13.0-13.5: <OR>(<declare>(T,x),<cast>(x,T))
393 15.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
394 17.0-17.15: <error>
395 19.0-19.5: +(z,q)
396 ]])
397
398 m4_define([_AT_GLR_STDERR],
399 [[syntax error
400 ]])
401
402 m4_define([_AT_VERBOSE_GLR_STDERR],
403 [[syntax error, unexpected ID, expecting '=' or '+' or ')'
404 ]])
405
406 ## ---------------------------------------------------- ##
407 ## Compile the grammar described in the documentation. ##
408 ## ---------------------------------------------------- ##
409
410 AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
411 _AT_TEST_GLR_CXXTYPES([],
412 [%dprec 1], [%dprec 2])
413 AT_PARSER_CHECK([[./types test-input]], 0,
414 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
415 AT_CLEANUP
416
417 AT_SETUP([GLR: Resolve ambiguity, impure, locations])
418 _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
419 AT_PARSER_CHECK([[./types test-input]], 0,
420 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
421 AT_CLEANUP
422
423 AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
424 _AT_TEST_GLR_CXXTYPES([%pure-parser],
425 [%dprec 1], [%dprec 2])
426 AT_PARSER_CHECK([[./types test-input]], 0,
427 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
428 AT_CLEANUP
429
430 AT_SETUP([GLR: Resolve ambiguity, pure, locations])
431 _AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
432 [%dprec 1], [%dprec 2])
433 AT_PARSER_CHECK([[./types test-input]], 0,
434 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
435 AT_CLEANUP
436
437 AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
438 _AT_TEST_GLR_CXXTYPES([],
439 [%merge <stmtMerge>], [%merge <stmtMerge>])
440 AT_PARSER_CHECK([[./types test-input]], 0,
441 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
442 AT_CLEANUP
443
444 AT_SETUP([GLR: Merge conflicting parses, impure, locations])
445 _AT_TEST_GLR_CXXTYPES([%locations],
446 [%merge <stmtMerge>], [%merge <stmtMerge>])
447 AT_PARSER_CHECK([[./types test-input]], 0,
448 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
449 AT_CLEANUP
450
451 AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
452 _AT_TEST_GLR_CXXTYPES([%pure-parser],
453 [%merge <stmtMerge>], [%merge <stmtMerge>])
454 AT_PARSER_CHECK([[./types test-input]], 0,
455 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
456 AT_CLEANUP
457 AT_SETUP([GLR: Merge conflicting parses, pure, locations])
458 _AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
459 [%merge <stmtMerge>],[%merge <stmtMerge>])
460 AT_PARSER_CHECK([[./types test-input]], 0,
461 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
462 AT_CLEANUP
463
464 AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
465 _AT_TEST_GLR_CXXTYPES([%error-verbose],
466 [%merge <stmtMerge>], [%merge <stmtMerge>])
467 AT_PARSER_CHECK([[./types test-input]], 0,
468 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)
469 AT_CLEANUP