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