]> git.saurik.com Git - bison.git/blame_incremental - tests/cxx-type.at
Catch up to last couple of days' checkins.
[bison.git] / tests / cxx-type.at
... / ...
CommitLineData
1# Checking GLR Parsing. -*- Autotest -*-
2# Copyright (C) 2002, 2003, 2004, 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA
17# 02110-1301, 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 *
38]m4_bmatch([$2], [stmtMerge],
39[ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[
40 #define YYINITDEPTH 10
41 #define YYSTACKEXPANDABLE 1
42 static char *format (char const *, ...);
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);
59 void yyerror (ERROR_PARAMETERS);
60%}
61
62%token TYPENAME ID
63
64%right '='
65%left '+'
66
67%glr-parser
68
69%destructor { free ($$); } TYPENAME ID
70
71%%
72
73prog :
74 | prog stmt {
75]AT_LOCATION_IF([
76 printf ("%d.%d-%d.%d: ",
77 @2.first_line, @2.first_column,
78 @2.last_line, @2.last_column);])[
79 printf ("%s\n", ]$[2);
80 }
81 ;
82
83stmt : expr ';' $2 { $$ = ]$[1; }
84 | decl $3
85 | error ';' { static char error_msg[] = "<error>"; $$ = error_msg; }
86 | '@' { YYACCEPT; }
87 ;
88
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); }
93 ;
94
95decl : TYPENAME declarator ';'
96 { $$ = format ("<declare>(%s,%s)", ]$[1, ]$[2); }
97 | TYPENAME declarator '=' expr ';'
98 { $$ = format ("<init-declare>(%s,%s,%s)", ]$[1, ]$[2, ]$[4); }
99 ;
100
101declarator : ID
102 | '(' declarator ')' { $$ = ]$[2; }
103 ;
104
105%%
106
107#include <ctype.h>
108#include <stdlib.h>
109#include <string.h>
110#include <stdarg.h>
111
112int
113main (int argc, char **argv)
114{
115 if (argc != 2)
116 abort ();
117 if (!freopen (argv[1], "r", stdin))
118 return 3;
119 return yyparse ();
120}
121
122int
123yylex (LEX_PARAMETERS)
124{
125 char buffer[256];
126 int c;
127 unsigned int i;
128 static int lineNum = 1;
129 static int colNum = 0;
130
131#if YYPURE
132# define yylloc (*llocp)
133# define yylval (*lvalp)
134#endif
135
136 while (1)
137 {
138 c = getchar ();
139 switch (c)
140 {
141 case EOF:
142 return 0;
143 case '\t':
144 colNum = (colNum + 7) & ~7;
145 break;
146 case ' ': case '\f':
147 colNum += 1;
148 break;
149 case '\n':
150 lineNum += 1;
151 colNum = 0;
152 break;
153 default:
154 {
155 int tok;
156#if YYLSP_NEEDED
157 yylloc.first_line = yylloc.last_line = lineNum;
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;
177 yylval = strcpy ((char *) malloc (i), buffer);
178 }
179 else
180 {
181 static char empty_string[] = "";
182 colNum += 1;
183 tok = c;
184 yylval = empty_string;
185 }
186#if YYLSP_NEEDED
187 yylloc.last_column = colNum-1;
188#endif
189 return tok;
190 }
191 }
192 }
193}
194
195void
196yyerror (ERROR_PARAMETERS)
197{
198#if YYPURE && YYLSP_NEEDED
199 /* Pacify GCC by using llocp. */
200 if (! llocp)
201 abort ();
202#endif
203 fprintf (stderr, "%s\n", s);
204}
205
206
207static char *
208format (char const *form, ...)
209{
210 char buffer[1024];
211 va_list args;
212 va_start (args, form);
213 vsprintf (buffer, form, args);
214 va_end (args);
215 return strcpy ((char *) malloc (strlen (buffer) + 1), buffer);
216}
217
218]]
219m4_bmatch([$2], [stmtMerge],
220[[static YYSTYPE
221stmtMerge (YYSTYPE x0, YYSTYPE x1)
222{
223 return format ("<OR>(%s,%s)", x0, x1);
224}
225]])
226)
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
254AT_CHECK([bison -o types.c types.y], 0, [], ignore)
255AT_COMPILE([types])
256AT_BISON_OPTION_POPDEFS
257])
258
259m4_define([_AT_RESOLVED_GLR_OUTPUT],
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],
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)
281]])
282
283m4_define([_AT_AMBIG_GLR_OUTPUT],
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],
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)
305]])
306
307m4_define([_AT_GLR_STDERR],
308[[syntax error
309]])
310
311m4_define([_AT_VERBOSE_GLR_STDERR],
312[[syntax error, unexpected ID, expecting '=' or '+' or ')'
313]])
314
315## ---------------------------------------------------- ##
316## Compile the grammar described in the documentation. ##
317## ---------------------------------------------------- ##
318
319AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
320_AT_TEST_GLR_CXXTYPES([],
321 [%dprec 1], [%dprec 2])
322AT_PARSER_CHECK([[./types test-input]], 0,
323 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
324AT_CLEANUP
325
326AT_SETUP([GLR: Resolve ambiguity, impure, locations])
327_AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
328AT_PARSER_CHECK([[./types test-input]], 0,
329 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
330AT_CLEANUP
331
332AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
333_AT_TEST_GLR_CXXTYPES([%pure-parser],
334 [%dprec 1], [%dprec 2])
335AT_PARSER_CHECK([[./types test-input]], 0,
336 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
337AT_CLEANUP
338
339AT_SETUP([GLR: Resolve ambiguity, pure, locations])
340_AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
341 [%dprec 1], [%dprec 2])
342AT_PARSER_CHECK([[./types test-input]], 0,
343 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
344AT_CLEANUP
345
346AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
347_AT_TEST_GLR_CXXTYPES([],
348 [%merge <stmtMerge>], [%merge <stmtMerge>])
349AT_PARSER_CHECK([[./types test-input]], 0,
350 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
351AT_CLEANUP
352
353AT_SETUP([GLR: Merge conflicting parses, impure, locations])
354_AT_TEST_GLR_CXXTYPES([%locations],
355 [%merge <stmtMerge>], [%merge <stmtMerge>])
356AT_PARSER_CHECK([[./types test-input]], 0,
357 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
358AT_CLEANUP
359
360AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
361_AT_TEST_GLR_CXXTYPES([%pure-parser],
362 [%merge <stmtMerge>], [%merge <stmtMerge>])
363AT_PARSER_CHECK([[./types test-input]], 0,
364 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
365AT_CLEANUP
366AT_SETUP([GLR: Merge conflicting parses, pure, locations])
367_AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
368 [%merge <stmtMerge>],[%merge <stmtMerge>])
369AT_PARSER_CHECK([[./types test-input]], 0,
370 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
371AT_CLEANUP
372
373AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
374_AT_TEST_GLR_CXXTYPES([%error-verbose],
375 [%merge <stmtMerge>], [%merge <stmtMerge>])
376AT_PARSER_CHECK([[./types test-input]], 0,
377 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)
378AT_CLEANUP