]> git.saurik.com Git - bison.git/blame_incremental - tests/cxx-type.at
* data/lalr1.cc (symprint_): Use cdebug_ to avoid warnings.
[bison.git] / tests / cxx-type.at
... / ...
CommitLineData
1# Checking GLR Parsing. -*- Autotest -*-
2# Copyright (C) 2002, 2003, 2004 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., 59 Temple Place - Suite 330, Boston, MA
17# 02111-1307, USA.
18
19AT_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.
26m4_define([_AT_TEST_GLR_CXXTYPES],
27[
28AT_BISON_OPTION_PUSHDEFS([$1])
29
30AT_DATA_GRAMMAR([types.y],
31[[/* Simplified C++ Type and Expression Grammar. */
32
33$1
34
35%{
36 #include <stdio.h>
37 #define YYSTYPE char const *
38]m4_bmatch([$2], [stmtMerge],
39[ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
40 #define YYINITDEPTH 10
41 static char *format (char const *, ...);
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);
59%}
60
61%token TYPENAME ID
62
63%right '='
64%left '+'
65
66%glr-parser
67
68%%
69
70prog :
71 | prog stmt {
72]AT_LOCATION_IF([
73 printf ("%d.%d-%d.%d: ",
74 @2.first_line, @2.first_column,
75 @2.last_line, @2.last_column);])[
76 printf ("%s\n", ]$[2);
77 }
78 ;
79
80stmt : expr ';' $2 { $$ = ]$[1; }
81 | decl $3
82 | error ';' { $$ = "<error>"; }
83 | '@' { YYACCEPT; }
84 ;
85
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); }
90 ;
91
92decl : TYPENAME declarator ';'
93 { $$ = format ("<declare>(%s,%s)", ]$[1, ]$[2); }
94 | TYPENAME declarator '=' expr ';'
95 { $$ = format ("<init-declare>(%s,%s,%s)", ]$[1, ]$[2, ]$[4); }
96 ;
97
98declarator : ID
99 | '(' declarator ')' { $$ = ]$[2; }
100 ;
101
102%%
103
104#include <ctype.h>
105#include <stdlib.h>
106#include <string.h>
107#include <stdarg.h>
108
109int
110main (int argc, char **argv)
111{
112 if (argc != 2)
113 abort ();
114 if (!freopen (argv[1], "r", stdin))
115 abort ();
116 exit (yyparse ());
117}
118
119int
120yylex (LEX_PARAMETERS)
121{
122 char buffer[256];
123 int c;
124 unsigned int i;
125 static int lineNum = 1;
126 static int colNum = 0;
127
128#if YYPURE
129# define yylloc (*llocp)
130# define yylval (*lvalp)
131#endif
132
133 while (1)
134 {
135 c = getchar ();
136 switch (c)
137 {
138 case EOF:
139 return 0;
140 case '\t':
141 colNum = (colNum + 7) & ~7;
142 break;
143 case ' ': case '\f':
144 colNum += 1;
145 break;
146 case '\n':
147 lineNum += 1;
148 colNum = 0;
149 break;
150 default:
151 {
152 int tok;
153#if YYLSP_NEEDED
154 yylloc.first_line = yylloc.last_line = lineNum;
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 }
176 else
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 }
187 }
188 }
189}
190
191int
192yyerror (ERROR_PARAMETERS)
193{
194#if YYPURE && YYLSP_NEEDED
195 /* Pacify GCC by using llocp. */
196 if (! llocp)
197 abort ();
198#endif
199 fprintf (stderr, "%s\n", s);
200 return 0;
201}
202
203
204static char *
205format (char const *form, ...)
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
215]]
216m4_bmatch([$2], [stmtMerge],
217[[static YYSTYPE
218stmtMerge (YYSTYPE x0, YYSTYPE x1)
219{
220 return format ("<OR>(%s,%s)", x0, x1);
221}
222]])
223)
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
251AT_CHECK([bison -o types.c types.y], 0, [], ignore)
252AT_COMPILE([types])
253AT_BISON_OPTION_POPDEFS
254])
255
256m4_define([_AT_RESOLVED_GLR_OUTPUT],
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],
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)
278]])
279
280m4_define([_AT_AMBIG_GLR_OUTPUT],
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],
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)
302]])
303
304m4_define([_AT_GLR_STDERR],
305[[syntax error
306]])
307
308m4_define([_AT_VERBOSE_GLR_STDERR],
309[[syntax error, unexpected ID, expecting '=' or '+' or ')'
310]])
311
312## ---------------------------------------------------- ##
313## Compile the grammar described in the documentation. ##
314## ---------------------------------------------------- ##
315
316AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
317_AT_TEST_GLR_CXXTYPES([],
318 [%dprec 1], [%dprec 2])
319AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
320 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
321AT_CLEANUP
322
323AT_SETUP([GLR: Resolve ambiguity, impure, locations])
324_AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
325AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
326 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
327AT_CLEANUP
328
329AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
330_AT_TEST_GLR_CXXTYPES([%pure-parser],
331 [%dprec 1], [%dprec 2])
332AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
333 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
334AT_CLEANUP
335
336AT_SETUP([GLR: Resolve ambiguity, pure, locations])
337_AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
338 [%dprec 1], [%dprec 2])
339AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
340 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
341AT_CLEANUP
342
343AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
344_AT_TEST_GLR_CXXTYPES([],
345 [%merge <stmtMerge>], [%merge <stmtMerge>])
346AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
347 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
348AT_CLEANUP
349
350AT_SETUP([GLR: Merge conflicting parses, impure, locations])
351_AT_TEST_GLR_CXXTYPES([%locations],
352 [%merge <stmtMerge>], [%merge <stmtMerge>])
353AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
354 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
355AT_CLEANUP
356
357AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
358_AT_TEST_GLR_CXXTYPES([%pure-parser],
359 [%merge <stmtMerge>], [%merge <stmtMerge>])
360AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
361 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
362AT_CLEANUP
363AT_SETUP([GLR: Merge conflicting parses, pure, locations])
364_AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
365 [%merge <stmtMerge>],[%merge <stmtMerge>])
366AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
367 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
368AT_CLEANUP
369
370AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
371_AT_TEST_GLR_CXXTYPES([%error-verbose],
372 [%merge <stmtMerge>], [%merge <stmtMerge>])
373AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
374 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)
375AT_CLEANUP