]> git.saurik.com Git - bison.git/blame - tests/cxx-type.at
Don't let -Wnone disable -Werror.
[bison.git] / tests / cxx-type.at
CommitLineData
1154cced 1# Checking GLR Parsing. -*- Autotest -*-
7d424de1 2
575619af 3# Copyright (C) 2002-2011 Free Software Foundation, Inc.
12bebc04 4
f16b0819 5# This program is free software: you can redistribute it and/or modify
12bebc04 6# it under the terms of the GNU General Public License as published by
f16b0819
PE
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
12bebc04
PH
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
f16b0819 14#
12bebc04 15# You should have received a copy of the GNU General Public License
f16b0819 16# along with this program. If not, see <http://www.gnu.org/licenses/>.
12bebc04
PH
17
18AT_BANNER([[C++ Type Syntax (GLR).]])
19
9501dc6e
AD
20# _AT_TEST_GLR_CXXTYPES(DECL, RESOLVE1, RESOLVE2)
21# -----------------------------------------------
12bebc04
PH
22# Store into types.y the calc program, with DECL inserted as a declaration,
23# and with RESOLVE1 and RESOLVE2 as annotations on the conflicted rule for
24# stmt. Then compile the result.
9501dc6e 25m4_define([_AT_TEST_GLR_CXXTYPES],
25005f6a
PH
26[
27AT_BISON_OPTION_PUSHDEFS([$1])
28
29AT_DATA_GRAMMAR([types.y],
1154cced 30[[/* Simplified C++ Type and Expression Grammar. */
12bebc04 31
1154cced 32$1
12bebc04
PH
33
34%{
35 #include <stdio.h>
2c3b392a
AD
36 union Node {
37 struct {
9ecafbbf 38 int isNterm;
2c3b392a 39 int parents;
9ecafbbf 40 } nodeInfo;
2c3b392a 41 struct {
9ecafbbf 42 int isNterm; /* 1 */
2c3b392a
AD
43 int parents;
44 char const *form;
45 union Node *children[3];
46 } nterm;
47 struct {
9ecafbbf 48 int isNterm; /* 0 */
2c3b392a
AD
49 int parents;
50 char *text;
51 } term;
52 };
53 typedef union Node Node;
54 static Node *new_nterm (char const *, Node *, Node *, Node *);
55 static Node *new_term (char *);
56 static void free_node (Node *);
57 static char *node_to_string (Node *);
58 #define YYSTYPE Node *
1154cced
AD
59]m4_bmatch([$2], [stmtMerge],
60[ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
12bebc04 61 #define YYINITDEPTH 10
e2688cd9 62 #define YYSTACKEXPANDABLE 1
b8a204c0
PE
63 struct YYLTYPE;
64#if YYPURE
65# if YYLSP_NEEDED
66# define LEX_PARAMETERS YYSTYPE *lvalp, struct YYLTYPE *llocp
67# define ERROR_PARAMETERS struct YYLTYPE *llocp, char const *s
68# else
69# define LEX_PARAMETERS YYSTYPE *lvalp
70# endif
71#endif
72#ifndef LEX_PARAMETERS
73# define LEX_PARAMETERS void
74#endif
75#ifndef ERROR_PARAMETERS
76# define ERROR_PARAMETERS char const *s
77#endif
78 int yylex (LEX_PARAMETERS);
ac8c5689 79 void yyerror (ERROR_PARAMETERS);
12bebc04
PH
80%}
81
82%token TYPENAME ID
83
84%right '='
85%left '+'
86
87%glr-parser
88
2c3b392a 89%destructor { free_node ($$); } stmt expr decl declarator TYPENAME ID
a22ff96f 90
12bebc04
PH
91%%
92
1154cced 93prog :
671881d1 94 | prog stmt {
9ecafbbf 95 char *output;]AT_LOCATION_IF([
671881d1 96 printf ("%d.%d-%d.%d: ",
25005f6a
PH
97 @2.first_line, @2.first_column,
98 @2.last_line, @2.last_column);])[
9ecafbbf 99 output = node_to_string (]$[2);
f0064700 100 printf ("%s\n", output);
9ecafbbf
AD
101 free (output);
102 free_node (]$[2);
25005f6a 103 }
12bebc04
PH
104 ;
105
25005f6a 106stmt : expr ';' $2 { $$ = ]$[1; }
1154cced 107 | decl $3
9ecafbbf 108 | error ';' { $$ = new_nterm ("<error>", 0, 0, 0); }
04098407 109 | '@' { YYACCEPT; }
12bebc04
PH
110 ;
111
25005f6a 112expr : ID
2c3b392a 113 | TYPENAME '(' expr ')'
9ecafbbf
AD
114 { $$ = new_nterm ("<cast>(%s,%s)", ]$[3, ]$[1, 0); }
115 | expr '+' expr { $$ = new_nterm ("+(%s,%s)", ]$[1, ]$[3, 0); }
2c3b392a 116 | expr '=' expr { $$ = new_nterm ("=(%s,%s)", ]$[1, ]$[3, 0); }
12bebc04
PH
117 ;
118
1154cced 119decl : TYPENAME declarator ';'
9ecafbbf 120 { $$ = new_nterm ("<declare>(%s,%s)", ]$[1, ]$[2, 0); }
12bebc04 121 | TYPENAME declarator '=' expr ';'
9ecafbbf
AD
122 { $$ = new_nterm ("<init-declare>(%s,%s,%s)", ]$[1,
123 ]$[2, ]$[4); }
12bebc04
PH
124 ;
125
25005f6a
PH
126declarator : ID
127 | '(' declarator ')' { $$ = ]$[2; }
12bebc04
PH
128 ;
129
130%%
131
132#include <ctype.h>
c469acce 133#include <stdlib.h>
1154cced 134#include <string.h>
25005f6a 135#include <stdarg.h>
12bebc04 136
1154cced 137int
671881d1 138main (int argc, char **argv)
12bebc04 139{
500bbfcd
PE
140 if (argc != 2)
141 abort ();
927f7817 142 if (!freopen (argv[1], "r", stdin))
6100a9aa
PE
143 return 3;
144 return yyparse ();
12bebc04
PH
145}
146
1154cced 147int
b8a204c0 148yylex (LEX_PARAMETERS)
12bebc04
PH
149{
150 char buffer[256];
151 int c;
c469acce 152 unsigned int i;
25005f6a 153 static int lineNum = 1;
e342c3be 154 static int colNum = 0;
1154cced
AD
155
156#if YYPURE
c66dfadd 157# undef yylloc
25005f6a 158# define yylloc (*llocp)
c66dfadd 159# undef yylval
1154cced 160# define yylval (*lvalp)
1154cced
AD
161#endif
162
c469acce
PE
163 while (1)
164 {
cf806753
PE
165 if (feof (stdin))
166 abort ();
c469acce
PE
167 c = getchar ();
168 switch (c)
169 {
170 case EOF:
171 return 0;
25005f6a 172 case '\t':
e342c3be 173 colNum = (colNum + 7) & ~7;
25005f6a
PH
174 break;
175 case ' ': case '\f':
176 colNum += 1;
04098407 177 break;
671881d1 178 case '\n':
25005f6a 179 lineNum += 1;
e342c3be 180 colNum = 0;
c469acce
PE
181 break;
182 default:
25005f6a
PH
183 {
184 int tok;
185#if YYLSP_NEEDED
671881d1 186 yylloc.first_line = yylloc.last_line = lineNum;
25005f6a
PH
187 yylloc.first_column = colNum;
188#endif
189 if (isalpha (c))
190 {
191 i = 0;
192
193 do
194 {
195 buffer[i++] = c;
196 colNum += 1;
197 if (i == sizeof buffer - 1)
198 abort ();
199 c = getchar ();
200 }
201 while (isalnum (c) || c == '_');
202
203 ungetc (c, stdin);
204 buffer[i++] = 0;
205 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
9ecafbbf 206 yylval = new_term (strcpy ((char *) malloc (i), buffer));
25005f6a 207 }
671881d1 208 else
25005f6a
PH
209 {
210 colNum += 1;
211 tok = c;
9ecafbbf 212 yylval = 0;
25005f6a
PH
213 }
214#if YYLSP_NEEDED
215 yylloc.last_column = colNum-1;
216#endif
217 return tok;
218 }
c469acce 219 }
12bebc04 220 }
12bebc04
PH
221}
222
ac8c5689 223void
b8a204c0
PE
224yyerror (ERROR_PARAMETERS)
225{
2a8d363a 226#if YYPURE && YYLSP_NEEDED
b8a204c0
PE
227 /* Pacify GCC by using llocp. */
228 if (! llocp)
229 abort ();
2a8d363a 230#endif
12bebc04 231 fprintf (stderr, "%s\n", s);
12bebc04
PH
232}
233
2c3b392a
AD
234static Node *
235new_nterm (char const *form, Node *child0, Node *child1, Node *child2)
236{
9d9b8b70 237 Node *node = (Node *) malloc (sizeof (Node));
9ecafbbf 238 node->nterm.isNterm = 1;
2c3b392a
AD
239 node->nterm.parents = 0;
240 node->nterm.form = form;
241 node->nterm.children[0] = child0;
242 if (child0)
9ecafbbf 243 child0->nodeInfo.parents += 1;
2c3b392a
AD
244 node->nterm.children[1] = child1;
245 if (child1)
9ecafbbf 246 child1->nodeInfo.parents += 1;
2c3b392a
AD
247 node->nterm.children[2] = child2;
248 if (child2)
9ecafbbf 249 child2->nodeInfo.parents += 1;
2c3b392a
AD
250 return node;
251}
252
253static Node *
254new_term (char *text)
255{
9d9b8b70 256 Node *node = (Node *) malloc (sizeof (Node));
9ecafbbf 257 node->term.isNterm = 0;
2c3b392a
AD
258 node->term.parents = 0;
259 node->term.text = text;
260 return node;
261}
262
263static void
264free_node (Node *node)
265{
266 if (!node)
267 return;
9ecafbbf 268 node->nodeInfo.parents -= 1;
2c3b392a 269 /* Free only if 0 (last parent) or -1 (no parents). */
9ecafbbf 270 if (node->nodeInfo.parents > 0)
2c3b392a 271 return;
9ecafbbf 272 if (node->nodeInfo.isNterm == 1)
2c3b392a
AD
273 {
274 free_node (node->nterm.children[0]);
275 free_node (node->nterm.children[1]);
276 free_node (node->nterm.children[2]);
277 }
278 else
279 free (node->term.text);
280 free (node);
281}
25005f6a 282
671881d1 283static char *
2c3b392a 284node_to_string (Node *node)
25005f6a 285{
2c3b392a
AD
286 char *child0;
287 char *child1;
288 char *child2;
289 char *buffer;
290 if (!node)
291 {
9d9b8b70 292 buffer = (char *) malloc (1);
2c3b392a
AD
293 buffer[0] = 0;
294 }
9ecafbbf 295 else if (node->nodeInfo.isNterm == 1)
2c3b392a
AD
296 {
297 child0 = node_to_string (node->nterm.children[0]);
298 child1 = node_to_string (node->nterm.children[1]);
299 child2 = node_to_string (node->nterm.children[2]);
9d9b8b70
PE
300 buffer = (char *) malloc (strlen (node->nterm.form) + strlen (child0)
301 + strlen (child1) + strlen (child2) + 1);
2c3b392a
AD
302 sprintf (buffer, node->nterm.form, child0, child1, child2);
303 free (child0);
304 free (child1);
305 free (child2);
306 }
307 else
308 buffer = strdup (node->term.text);
309 return buffer;
25005f6a
PH
310}
311
1154cced
AD
312]]
313m4_bmatch([$2], [stmtMerge],
314[[static YYSTYPE
315stmtMerge (YYSTYPE x0, YYSTYPE x1)
12bebc04 316{
2c3b392a 317 return new_nterm ("<OR>(%s,%s)", x0, x1, 0);
12bebc04
PH
318}
319]])
1154cced 320)
12bebc04
PH
321
322AT_DATA([test-input],
323[[
324
325z + q;
326
327T x;
328
329T x = y;
330
331x = y;
332
333T (x) + y;
334
335T (x);
336
337T (y) = z + q;
338
339T (y y) = z + q;
340
341z + q;
342
343@
344
345This is total garbage, but it should be ignored.
346]])
347
da730230 348AT_BISON_CHECK([-o types.c types.y], 0, [], ignore)
1154cced 349AT_COMPILE([types])
25005f6a 350AT_BISON_OPTION_POPDEFS
12bebc04
PH
351])
352
353m4_define([_AT_RESOLVED_GLR_OUTPUT],
215b40ac 354[[[+(z,q)
25005f6a
PH
355<declare>(T,x)
356<init-declare>(T,x,y)
357=(x,y)
358+(<cast>(x,T),y)
359<declare>(T,x)
360<init-declare>(T,y,+(z,q))
361<error>
362+(z,q)
215b40ac 363]]])
25005f6a
PH
364
365m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
215b40ac 366[[[3.0-3.5: +(z,q)
e342c3be
AD
3675.0-5.3: <declare>(T,x)
3687.0-7.7: <init-declare>(T,x,y)
3699.0-9.5: =(x,y)
37011.0-11.9: +(<cast>(x,T),y)
37113.0-13.5: <declare>(T,x)
37215.0-15.13: <init-declare>(T,y,+(z,q))
37317.0-17.15: <error>
37419.0-19.5: +(z,q)
215b40ac 375]]])
12bebc04
PH
376
377m4_define([_AT_AMBIG_GLR_OUTPUT],
215b40ac 378[[[+(z,q)
25005f6a
PH
379<declare>(T,x)
380<init-declare>(T,x,y)
381=(x,y)
382+(<cast>(x,T),y)
383<OR>(<declare>(T,x),<cast>(x,T))
384<OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
385<error>
386+(z,q)
215b40ac 387]]])
25005f6a
PH
388
389m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
215b40ac 390[[[3.0-3.5: +(z,q)
e342c3be
AD
3915.0-5.3: <declare>(T,x)
3927.0-7.7: <init-declare>(T,x,y)
3939.0-9.5: =(x,y)
39411.0-11.9: +(<cast>(x,T),y)
39513.0-13.5: <OR>(<declare>(T,x),<cast>(x,T))
39615.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
39717.0-17.15: <error>
39819.0-19.5: +(z,q)
215b40ac 399]]])
12bebc04 400
1154cced 401m4_define([_AT_GLR_STDERR],
215b40ac
EB
402[[[syntax error
403]]])
12bebc04 404
1154cced 405m4_define([_AT_VERBOSE_GLR_STDERR],
215b40ac
EB
406[[[syntax error, unexpected ID, expecting '=' or '+' or ')'
407]]])
12bebc04
PH
408
409## ---------------------------------------------------- ##
410## Compile the grammar described in the documentation. ##
411## ---------------------------------------------------- ##
412
413AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
9501dc6e 414_AT_TEST_GLR_CXXTYPES([],
9ecafbbf 415 [%dprec 1], [%dprec 2])
49b1cf79 416AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 417 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
418AT_CLEANUP
419
420AT_SETUP([GLR: Resolve ambiguity, impure, locations])
9501dc6e 421_AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
49b1cf79 422AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 423 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
12bebc04
PH
424AT_CLEANUP
425
426AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
d9df47b6 427_AT_TEST_GLR_CXXTYPES([%define api.pure],
9ecafbbf 428 [%dprec 1], [%dprec 2])
49b1cf79 429AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 430 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
431AT_CLEANUP
432
433AT_SETUP([GLR: Resolve ambiguity, pure, locations])
d9df47b6 434_AT_TEST_GLR_CXXTYPES([%define api.pure %locations],
9ecafbbf 435 [%dprec 1], [%dprec 2])
49b1cf79 436AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 437 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
12bebc04
PH
438AT_CLEANUP
439
440AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
9501dc6e 441_AT_TEST_GLR_CXXTYPES([],
9ecafbbf 442 [%merge <stmtMerge>], [%merge <stmtMerge>])
49b1cf79 443AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 444 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
445AT_CLEANUP
446
447AT_SETUP([GLR: Merge conflicting parses, impure, locations])
9501dc6e 448_AT_TEST_GLR_CXXTYPES([%locations],
9ecafbbf 449 [%merge <stmtMerge>], [%merge <stmtMerge>])
49b1cf79 450AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 451 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
12bebc04
PH
452AT_CLEANUP
453
454AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
d9df47b6 455_AT_TEST_GLR_CXXTYPES([%define api.pure],
9ecafbbf 456 [%merge <stmtMerge>], [%merge <stmtMerge>])
49b1cf79 457AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 458 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
459AT_CLEANUP
460AT_SETUP([GLR: Merge conflicting parses, pure, locations])
d9df47b6 461_AT_TEST_GLR_CXXTYPES([%define api.pure %locations],
9ecafbbf 462 [%merge <stmtMerge>],[%merge <stmtMerge>])
49b1cf79 463AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 464 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
12bebc04
PH
465AT_CLEANUP
466
467AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
9501dc6e 468_AT_TEST_GLR_CXXTYPES([%error-verbose],
9ecafbbf 469 [%merge <stmtMerge>], [%merge <stmtMerge>])
49b1cf79 470AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 471 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)
12bebc04 472AT_CLEANUP