]> git.saurik.com Git - bison.git/blame - tests/cxx-type.at
* data/lalr1.cc (symprint_): Use cdebug_ to avoid warnings.
[bison.git] / tests / cxx-type.at
CommitLineData
1154cced 1# Checking GLR Parsing. -*- Autotest -*-
e342c3be 2# Copyright (C) 2002, 2003, 2004 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
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17# 02111-1307, USA.
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>
671881d1 37 #define YYSTYPE char const *
1154cced
AD
38]m4_bmatch([$2], [stmtMerge],
39[ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
12bebc04 40 #define YYINITDEPTH 10
671881d1 41 static char *format (char const *, ...);
b8a204c0
PE
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);
12bebc04
PH
59%}
60
61%token TYPENAME ID
62
63%right '='
64%left '+'
65
66%glr-parser
67
68%%
69
1154cced 70prog :
671881d1 71 | prog stmt {
25005f6a 72]AT_LOCATION_IF([
671881d1 73 printf ("%d.%d-%d.%d: ",
25005f6a
PH
74 @2.first_line, @2.first_column,
75 @2.last_line, @2.last_column);])[
76 printf ("%s\n", ]$[2);
77 }
12bebc04
PH
78 ;
79
25005f6a 80stmt : expr ';' $2 { $$ = ]$[1; }
1154cced 81 | decl $3
25005f6a
PH
82 | error ';' { $$ = "<error>"; }
83 | '@' { YYACCEPT; }
12bebc04
PH
84 ;
85
25005f6a
PH
86expr : 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); }
12bebc04
PH
90 ;
91
1154cced 92decl : TYPENAME declarator ';'
25005f6a 93 { $$ = format ("<declare>(%s,%s)", ]$[1, ]$[2); }
12bebc04 94 | TYPENAME declarator '=' expr ';'
25005f6a 95 { $$ = format ("<init-declare>(%s,%s,%s)", ]$[1, ]$[2, ]$[4); }
12bebc04
PH
96 ;
97
25005f6a
PH
98declarator : ID
99 | '(' declarator ')' { $$ = ]$[2; }
12bebc04
PH
100 ;
101
102%%
103
104#include <ctype.h>
c469acce 105#include <stdlib.h>
1154cced 106#include <string.h>
25005f6a 107#include <stdarg.h>
12bebc04 108
1154cced 109int
671881d1 110main (int argc, char **argv)
12bebc04 111{
500bbfcd
PE
112 if (argc != 2)
113 abort ();
927f7817
AD
114 if (!freopen (argv[1], "r", stdin))
115 abort ();
12bebc04
PH
116 exit (yyparse ());
117}
118
1154cced 119int
b8a204c0 120yylex (LEX_PARAMETERS)
12bebc04
PH
121{
122 char buffer[256];
123 int c;
c469acce 124 unsigned int i;
25005f6a 125 static int lineNum = 1;
e342c3be 126 static int colNum = 0;
1154cced
AD
127
128#if YYPURE
25005f6a 129# define yylloc (*llocp)
1154cced 130# define yylval (*lvalp)
1154cced
AD
131#endif
132
c469acce
PE
133 while (1)
134 {
135 c = getchar ();
136 switch (c)
137 {
138 case EOF:
139 return 0;
25005f6a 140 case '\t':
e342c3be 141 colNum = (colNum + 7) & ~7;
25005f6a
PH
142 break;
143 case ' ': case '\f':
144 colNum += 1;
145 break;
671881d1 146 case '\n':
25005f6a 147 lineNum += 1;
e342c3be 148 colNum = 0;
c469acce
PE
149 break;
150 default:
25005f6a
PH
151 {
152 int tok;
153#if YYLSP_NEEDED
671881d1 154 yylloc.first_line = yylloc.last_line = lineNum;
25005f6a
PH
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 }
671881d1 176 else
25005f6a
PH
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 }
c469acce 187 }
12bebc04 188 }
12bebc04
PH
189}
190
191int
b8a204c0
PE
192yyerror (ERROR_PARAMETERS)
193{
2a8d363a 194#if YYPURE && YYLSP_NEEDED
b8a204c0
PE
195 /* Pacify GCC by using llocp. */
196 if (! llocp)
197 abort ();
2a8d363a 198#endif
12bebc04
PH
199 fprintf (stderr, "%s\n", s);
200 return 0;
201}
202
25005f6a 203
671881d1
PE
204static char *
205format (char const *form, ...)
25005f6a
PH
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
1154cced
AD
215]]
216m4_bmatch([$2], [stmtMerge],
217[[static YYSTYPE
218stmtMerge (YYSTYPE x0, YYSTYPE x1)
12bebc04 219{
25005f6a 220 return format ("<OR>(%s,%s)", x0, x1);
12bebc04
PH
221}
222]])
1154cced 223)
12bebc04
PH
224
225AT_DATA([test-input],
226[[
227
228z + q;
229
230T x;
231
232T x = y;
233
234x = y;
235
236T (x) + y;
237
238T (x);
239
240T (y) = z + q;
241
242T (y y) = z + q;
243
244z + q;
245
246@
247
248This is total garbage, but it should be ignored.
249]])
250
b56471a6 251AT_CHECK([bison -o types.c types.y], 0, [], ignore)
1154cced 252AT_COMPILE([types])
25005f6a 253AT_BISON_OPTION_POPDEFS
12bebc04
PH
254])
255
256m4_define([_AT_RESOLVED_GLR_OUTPUT],
25005f6a
PH
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
268m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
e342c3be
AD
269[[3.0-3.5: +(z,q)
2705.0-5.3: <declare>(T,x)
2717.0-7.7: <init-declare>(T,x,y)
2729.0-9.5: =(x,y)
27311.0-11.9: +(<cast>(x,T),y)
27413.0-13.5: <declare>(T,x)
27515.0-15.13: <init-declare>(T,y,+(z,q))
27617.0-17.15: <error>
27719.0-19.5: +(z,q)
12bebc04
PH
278]])
279
280m4_define([_AT_AMBIG_GLR_OUTPUT],
25005f6a
PH
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
292m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
e342c3be
AD
293[[3.0-3.5: +(z,q)
2945.0-5.3: <declare>(T,x)
2957.0-7.7: <init-declare>(T,x,y)
2969.0-9.5: =(x,y)
29711.0-11.9: +(<cast>(x,T),y)
29813.0-13.5: <OR>(<declare>(T,x),<cast>(x,T))
29915.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
30017.0-17.15: <error>
30119.0-19.5: +(z,q)
12bebc04
PH
302]])
303
1154cced 304m4_define([_AT_GLR_STDERR],
6e649e65 305[[syntax error
12bebc04
PH
306]])
307
1154cced 308m4_define([_AT_VERBOSE_GLR_STDERR],
6e649e65 309[[syntax error, unexpected ID, expecting '=' or '+' or ')'
12bebc04
PH
310]])
311
312## ---------------------------------------------------- ##
313## Compile the grammar described in the documentation. ##
314## ---------------------------------------------------- ##
315
316AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
9501dc6e
AD
317_AT_TEST_GLR_CXXTYPES([],
318 [%dprec 1], [%dprec 2])
9e32add8
AD
319AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
320 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
321AT_CLEANUP
322
323AT_SETUP([GLR: Resolve ambiguity, impure, locations])
9501dc6e 324_AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
9e32add8 325AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
25005f6a 326 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
12bebc04
PH
327AT_CLEANUP
328
329AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
9501dc6e
AD
330_AT_TEST_GLR_CXXTYPES([%pure-parser],
331 [%dprec 1], [%dprec 2])
9e32add8
AD
332AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
333 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
334AT_CLEANUP
335
336AT_SETUP([GLR: Resolve ambiguity, pure, locations])
9501dc6e
AD
337_AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
338 [%dprec 1], [%dprec 2])
1154cced 339AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
25005f6a 340 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
12bebc04
PH
341AT_CLEANUP
342
343AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
9501dc6e
AD
344_AT_TEST_GLR_CXXTYPES([],
345 [%merge <stmtMerge>], [%merge <stmtMerge>])
1154cced 346AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
9e32add8 347 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
348AT_CLEANUP
349
350AT_SETUP([GLR: Merge conflicting parses, impure, locations])
9501dc6e
AD
351_AT_TEST_GLR_CXXTYPES([%locations],
352 [%merge <stmtMerge>], [%merge <stmtMerge>])
1154cced 353AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
25005f6a 354 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
12bebc04
PH
355AT_CLEANUP
356
357AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
9501dc6e
AD
358_AT_TEST_GLR_CXXTYPES([%pure-parser],
359 [%merge <stmtMerge>], [%merge <stmtMerge>])
1154cced 360AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
9e32add8 361 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
12bebc04
PH
362AT_CLEANUP
363AT_SETUP([GLR: Merge conflicting parses, pure, locations])
9501dc6e
AD
364_AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
365 [%merge <stmtMerge>],[%merge <stmtMerge>])
1154cced 366AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
25005f6a 367 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
12bebc04
PH
368AT_CLEANUP
369
370AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
9501dc6e
AD
371_AT_TEST_GLR_CXXTYPES([%error-verbose],
372 [%merge <stmtMerge>], [%merge <stmtMerge>])
1154cced 373AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
9e32add8 374 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)
12bebc04 375AT_CLEANUP