]> git.saurik.com Git - bison.git/blame - tests/cxx-type.at
Fix test failure reported by Tom Lane in
[bison.git] / tests / cxx-type.at
CommitLineData
1154cced 1# Checking GLR Parsing. -*- Autotest -*-
e2a21b6f 2# Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
12bebc04
PH
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
0fb669f9
PE
16# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17# 02110-1301, USA.
12bebc04
PH
18
19AT_BANNER([[C++ Type Syntax (GLR).]])
20
9501dc6e
AD
21# _AT_TEST_GLR_CXXTYPES(DECL, RESOLVE1, RESOLVE2)
22# -----------------------------------------------
12bebc04
PH
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.
9501dc6e 26m4_define([_AT_TEST_GLR_CXXTYPES],
25005f6a
PH
27[
28AT_BISON_OPTION_PUSHDEFS([$1])
29
30AT_DATA_GRAMMAR([types.y],
1154cced 31[[/* Simplified C++ Type and Expression Grammar. */
12bebc04 32
1154cced 33$1
12bebc04
PH
34
35%{
36 #include <stdio.h>
2c3b392a
AD
37 union Node {
38 struct {
9ecafbbf 39 int isNterm;
2c3b392a 40 int parents;
9ecafbbf 41 } nodeInfo;
2c3b392a 42 struct {
9ecafbbf 43 int isNterm; /* 1 */
2c3b392a
AD
44 int parents;
45 char const *form;
46 union Node *children[3];
47 } nterm;
48 struct {
9ecafbbf 49 int isNterm; /* 0 */
2c3b392a
AD
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 *
1154cced
AD
60]m4_bmatch([$2], [stmtMerge],
61[ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
12bebc04 62 #define YYINITDEPTH 10
e2688cd9 63 #define YYSTACKEXPANDABLE 1
b8a204c0
PE
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);
ac8c5689 80 void yyerror (ERROR_PARAMETERS);
12bebc04
PH
81%}
82
83%token TYPENAME ID
84
85%right '='
86%left '+'
87
88%glr-parser
89
2c3b392a 90%destructor { free_node ($$); } stmt expr decl declarator TYPENAME ID
a22ff96f 91
12bebc04
PH
92%%
93
1154cced 94prog :
671881d1 95 | prog stmt {
9ecafbbf 96 char *output;]AT_LOCATION_IF([
671881d1 97 printf ("%d.%d-%d.%d: ",
25005f6a
PH
98 @2.first_line, @2.first_column,
99 @2.last_line, @2.last_column);])[
9ecafbbf 100 output = node_to_string (]$[2);
f0064700 101 printf ("%s\n", output);
9ecafbbf
AD
102 free (output);
103 free_node (]$[2);
25005f6a 104 }
12bebc04
PH
105 ;
106
25005f6a 107stmt : expr ';' $2 { $$ = ]$[1; }
1154cced 108 | decl $3
9ecafbbf 109 | error ';' { $$ = new_nterm ("<error>", 0, 0, 0); }
04098407 110 | '@' { YYACCEPT; }
12bebc04
PH
111 ;
112
25005f6a 113expr : ID
2c3b392a 114 | TYPENAME '(' expr ')'
9ecafbbf
AD
115 { $$ = new_nterm ("<cast>(%s,%s)", ]$[3, ]$[1, 0); }
116 | expr '+' expr { $$ = new_nterm ("+(%s,%s)", ]$[1, ]$[3, 0); }
2c3b392a 117 | expr '=' expr { $$ = new_nterm ("=(%s,%s)", ]$[1, ]$[3, 0); }
12bebc04
PH
118 ;
119
1154cced 120decl : TYPENAME declarator ';'
9ecafbbf 121 { $$ = new_nterm ("<declare>(%s,%s)", ]$[1, ]$[2, 0); }
12bebc04 122 | TYPENAME declarator '=' expr ';'
9ecafbbf
AD
123 { $$ = new_nterm ("<init-declare>(%s,%s,%s)", ]$[1,
124 ]$[2, ]$[4); }
12bebc04
PH
125 ;
126
25005f6a
PH
127declarator : ID
128 | '(' declarator ')' { $$ = ]$[2; }
12bebc04
PH
129 ;
130
131%%
132
133#include <ctype.h>
c469acce 134#include <stdlib.h>
1154cced 135#include <string.h>
25005f6a 136#include <stdarg.h>
12bebc04 137
1154cced 138int
671881d1 139main (int argc, char **argv)
12bebc04 140{
500bbfcd
PE
141 if (argc != 2)
142 abort ();
927f7817 143 if (!freopen (argv[1], "r", stdin))
6100a9aa
PE
144 return 3;
145 return yyparse ();
12bebc04
PH
146}
147
1154cced 148int
b8a204c0 149yylex (LEX_PARAMETERS)
12bebc04
PH
150{
151 char buffer[256];
152 int c;
c469acce 153 unsigned int i;
25005f6a 154 static int lineNum = 1;
e342c3be 155 static int colNum = 0;
1154cced
AD
156
157#if YYPURE
c66dfadd 158# undef yylloc
25005f6a 159# define yylloc (*llocp)
c66dfadd 160# undef yylval
1154cced 161# define yylval (*lvalp)
1154cced
AD
162#endif
163
c469acce
PE
164 while (1)
165 {
cf806753
PE
166 if (feof (stdin))
167 abort ();
c469acce
PE
168 c = getchar ();
169 switch (c)
170 {
171 case EOF:
172 return 0;
25005f6a 173 case '\t':
e342c3be 174 colNum = (colNum + 7) & ~7;
25005f6a
PH
175 break;
176 case ' ': case '\f':
177 colNum += 1;
04098407 178 break;
671881d1 179 case '\n':
25005f6a 180 lineNum += 1;
e342c3be 181 colNum = 0;
c469acce
PE
182 break;
183 default:
25005f6a
PH
184 {
185 int tok;
186#if YYLSP_NEEDED
671881d1 187 yylloc.first_line = yylloc.last_line = lineNum;
25005f6a
PH
188 yylloc.first_column = colNum;
189#endif
190 if (isalpha (c))
191 {
192 i = 0;
193
194 do
195 {
196 buffer[i++] = c;
197 colNum += 1;
198 if (i == sizeof buffer - 1)
199 abort ();
200 c = getchar ();
201 }
202 while (isalnum (c) || c == '_');
203
204 ungetc (c, stdin);
205 buffer[i++] = 0;
206 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
9ecafbbf 207 yylval = new_term (strcpy ((char *) malloc (i), buffer));
25005f6a 208 }
671881d1 209 else
25005f6a
PH
210 {
211 colNum += 1;
212 tok = c;
9ecafbbf 213 yylval = 0;
25005f6a
PH
214 }
215#if YYLSP_NEEDED
216 yylloc.last_column = colNum-1;
217#endif
218 return tok;
219 }
c469acce 220 }
12bebc04 221 }
12bebc04
PH
222}
223
ac8c5689 224void
b8a204c0
PE
225yyerror (ERROR_PARAMETERS)
226{
2a8d363a 227#if YYPURE && YYLSP_NEEDED
b8a204c0
PE
228 /* Pacify GCC by using llocp. */
229 if (! llocp)
230 abort ();
2a8d363a 231#endif
12bebc04 232 fprintf (stderr, "%s\n", s);
12bebc04
PH
233}
234
2c3b392a
AD
235static Node *
236new_nterm (char const *form, Node *child0, Node *child1, Node *child2)
237{
9d9b8b70 238 Node *node = (Node *) malloc (sizeof (Node));
9ecafbbf 239 node->nterm.isNterm = 1;
2c3b392a
AD
240 node->nterm.parents = 0;
241 node->nterm.form = form;
242 node->nterm.children[0] = child0;
243 if (child0)
9ecafbbf 244 child0->nodeInfo.parents += 1;
2c3b392a
AD
245 node->nterm.children[1] = child1;
246 if (child1)
9ecafbbf 247 child1->nodeInfo.parents += 1;
2c3b392a
AD
248 node->nterm.children[2] = child2;
249 if (child2)
9ecafbbf 250 child2->nodeInfo.parents += 1;
2c3b392a
AD
251 return node;
252}
253
254static Node *
255new_term (char *text)
256{
9d9b8b70 257 Node *node = (Node *) malloc (sizeof (Node));
9ecafbbf 258 node->term.isNterm = 0;
2c3b392a
AD
259 node->term.parents = 0;
260 node->term.text = text;
261 return node;
262}
263
264static void
265free_node (Node *node)
266{
267 if (!node)
268 return;
9ecafbbf 269 node->nodeInfo.parents -= 1;
2c3b392a 270 /* Free only if 0 (last parent) or -1 (no parents). */
9ecafbbf 271 if (node->nodeInfo.parents > 0)
2c3b392a 272 return;
9ecafbbf 273 if (node->nodeInfo.isNterm == 1)
2c3b392a
AD
274 {
275 free_node (node->nterm.children[0]);
276 free_node (node->nterm.children[1]);
277 free_node (node->nterm.children[2]);
278 }
279 else
280 free (node->term.text);
281 free (node);
282}
25005f6a 283
671881d1 284static char *
2c3b392a 285node_to_string (Node *node)
25005f6a 286{
2c3b392a
AD
287 char *child0;
288 char *child1;
289 char *child2;
290 char *buffer;
291 if (!node)
292 {
9d9b8b70 293 buffer = (char *) malloc (1);
2c3b392a
AD
294 buffer[0] = 0;
295 }
9ecafbbf 296 else if (node->nodeInfo.isNterm == 1)
2c3b392a
AD
297 {
298 child0 = node_to_string (node->nterm.children[0]);
299 child1 = node_to_string (node->nterm.children[1]);
300 child2 = node_to_string (node->nterm.children[2]);
9d9b8b70
PE
301 buffer = (char *) malloc (strlen (node->nterm.form) + strlen (child0)
302 + strlen (child1) + strlen (child2) + 1);
2c3b392a
AD
303 sprintf (buffer, node->nterm.form, child0, child1, child2);
304 free (child0);
305 free (child1);
306 free (child2);
307 }
308 else
309 buffer = strdup (node->term.text);
310 return buffer;
25005f6a
PH
311}
312
1154cced
AD
313]]
314m4_bmatch([$2], [stmtMerge],
315[[static YYSTYPE
316stmtMerge (YYSTYPE x0, YYSTYPE x1)
12bebc04 317{
2c3b392a 318 return new_nterm ("<OR>(%s,%s)", x0, x1, 0);
12bebc04
PH
319}
320]])
1154cced 321)
12bebc04
PH
322
323AT_DATA([test-input],
324[[
325
326z + q;
327
328T x;
329
330T x = y;
331
332x = y;
333
334T (x) + y;
335
336T (x);
337
338T (y) = z + q;
339
340T (y y) = z + q;
341
342z + q;
343
344@
345
346This is total garbage, but it should be ignored.
347]])
348
b56471a6 349AT_CHECK([bison -o types.c types.y], 0, [], ignore)
1154cced 350AT_COMPILE([types])
25005f6a 351AT_BISON_OPTION_POPDEFS
12bebc04
PH
352])
353
354m4_define([_AT_RESOLVED_GLR_OUTPUT],
25005f6a
PH
355[[+(z,q)
356<declare>(T,x)
357<init-declare>(T,x,y)
358=(x,y)
359+(<cast>(x,T),y)
360<declare>(T,x)
361<init-declare>(T,y,+(z,q))
362<error>
363+(z,q)
364]])
365
366m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
e342c3be
AD
367[[3.0-3.5: +(z,q)
3685.0-5.3: <declare>(T,x)
3697.0-7.7: <init-declare>(T,x,y)
3709.0-9.5: =(x,y)
37111.0-11.9: +(<cast>(x,T),y)
37213.0-13.5: <declare>(T,x)
37315.0-15.13: <init-declare>(T,y,+(z,q))
37417.0-17.15: <error>
37519.0-19.5: +(z,q)
12bebc04
PH
376]])
377
378m4_define([_AT_AMBIG_GLR_OUTPUT],
25005f6a
PH
379[[+(z,q)
380<declare>(T,x)
381<init-declare>(T,x,y)
382=(x,y)
383+(<cast>(x,T),y)
384<OR>(<declare>(T,x),<cast>(x,T))
385<OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
386<error>
387+(z,q)
388]])
389
390m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
e342c3be
AD
391[[3.0-3.5: +(z,q)
3925.0-5.3: <declare>(T,x)
3937.0-7.7: <init-declare>(T,x,y)
3949.0-9.5: =(x,y)
39511.0-11.9: +(<cast>(x,T),y)
39613.0-13.5: <OR>(<declare>(T,x),<cast>(x,T))
39715.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
39817.0-17.15: <error>
39919.0-19.5: +(z,q)
12bebc04
PH
400]])
401
1154cced 402m4_define([_AT_GLR_STDERR],
6e649e65 403[[syntax error
12bebc04
PH
404]])
405
1154cced 406m4_define([_AT_VERBOSE_GLR_STDERR],
6e649e65 407[[syntax error, unexpected ID, expecting '=' or '+' or ')'
12bebc04
PH
408]])
409
410## ---------------------------------------------------- ##
411## Compile the grammar described in the documentation. ##
412## ---------------------------------------------------- ##
413
414AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
9501dc6e 415_AT_TEST_GLR_CXXTYPES([],
9ecafbbf 416 [%dprec 1], [%dprec 2])
49b1cf79 417AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 418 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
419AT_CLEANUP
420
421AT_SETUP([GLR: Resolve ambiguity, impure, locations])
9501dc6e 422_AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
49b1cf79 423AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 424 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
12bebc04
PH
425AT_CLEANUP
426
427AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
9501dc6e 428_AT_TEST_GLR_CXXTYPES([%pure-parser],
9ecafbbf 429 [%dprec 1], [%dprec 2])
49b1cf79 430AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 431 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
432AT_CLEANUP
433
434AT_SETUP([GLR: Resolve ambiguity, pure, locations])
9501dc6e 435_AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
9ecafbbf 436 [%dprec 1], [%dprec 2])
49b1cf79 437AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 438 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
12bebc04
PH
439AT_CLEANUP
440
441AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
9501dc6e 442_AT_TEST_GLR_CXXTYPES([],
9ecafbbf 443 [%merge <stmtMerge>], [%merge <stmtMerge>])
49b1cf79 444AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 445 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
446AT_CLEANUP
447
448AT_SETUP([GLR: Merge conflicting parses, impure, locations])
9501dc6e 449_AT_TEST_GLR_CXXTYPES([%locations],
9ecafbbf 450 [%merge <stmtMerge>], [%merge <stmtMerge>])
49b1cf79 451AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 452 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
12bebc04
PH
453AT_CLEANUP
454
455AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
9501dc6e 456_AT_TEST_GLR_CXXTYPES([%pure-parser],
9ecafbbf 457 [%merge <stmtMerge>], [%merge <stmtMerge>])
49b1cf79 458AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 459 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
460AT_CLEANUP
461AT_SETUP([GLR: Merge conflicting parses, pure, locations])
9501dc6e 462_AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
9ecafbbf 463 [%merge <stmtMerge>],[%merge <stmtMerge>])
49b1cf79 464AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 465 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
12bebc04
PH
466AT_CLEANUP
467
468AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
9501dc6e 469_AT_TEST_GLR_CXXTYPES([%error-verbose],
9ecafbbf 470 [%merge <stmtMerge>], [%merge <stmtMerge>])
49b1cf79 471AT_PARSER_CHECK([[./types test-input]], 0,
9ecafbbf 472 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)
12bebc04 473AT_CLEANUP