]> git.saurik.com Git - bison.git/blame_incremental - tests/cxx-type.at
($(TESTSUITE)): Remove warning about ignoring
[bison.git] / tests / cxx-type.at
... / ...
CommitLineData
1# Checking GLR Parsing. -*- Autotest -*-
2# Copyright (C) 2002, 2003 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 = 1;
127
128
129#if YYPURE
130# define yylloc (*llocp)
131# define yylval (*lvalp)
132#endif
133
134 while (1)
135 {
136 c = getchar ();
137 switch (c)
138 {
139 case EOF:
140 return 0;
141 case '\t':
142 colNum = 1 + ((colNum + 7) & ~7);
143 break;
144 case ' ': case '\f':
145 colNum += 1;
146 break;
147 case '\n':
148 lineNum += 1;
149 colNum = 1;
150 break;
151 default:
152 {
153 int tok;
154#if YYLSP_NEEDED
155 yylloc.first_line = yylloc.last_line = lineNum;
156 yylloc.first_column = colNum;
157#endif
158 if (isalpha (c))
159 {
160 i = 0;
161
162 do
163 {
164 buffer[i++] = c;
165 colNum += 1;
166 if (i == sizeof buffer - 1)
167 abort ();
168 c = getchar ();
169 }
170 while (isalnum (c) || c == '_');
171
172 ungetc (c, stdin);
173 buffer[i++] = 0;
174 tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
175 yylval = strcpy (malloc (i), buffer);
176 }
177 else
178 {
179 colNum += 1;
180 tok = c;
181 yylval = "";
182 }
183#if YYLSP_NEEDED
184 yylloc.last_column = colNum-1;
185#endif
186 return tok;
187 }
188 }
189 }
190}
191
192int
193yyerror (ERROR_PARAMETERS)
194{
195#if YYPURE && YYLSP_NEEDED
196 /* Pacify GCC by using llocp. */
197 if (! llocp)
198 abort ();
199#endif
200 fprintf (stderr, "%s\n", s);
201 return 0;
202}
203
204
205static char *
206format (char const *form, ...)
207{
208 char buffer[1024];
209 va_list args;
210 va_start (args, form);
211 vsprintf (buffer, form, args);
212 va_end (args);
213 return strcpy (malloc (strlen (buffer) + 1), buffer);
214}
215
216]]
217m4_bmatch([$2], [stmtMerge],
218[[static YYSTYPE
219stmtMerge (YYSTYPE x0, YYSTYPE x1)
220{
221 return format ("<OR>(%s,%s)", x0, x1);
222}
223]])
224)
225
226AT_DATA([test-input],
227[[
228
229z + q;
230
231T x;
232
233T x = y;
234
235x = y;
236
237T (x) + y;
238
239T (x);
240
241T (y) = z + q;
242
243T (y y) = z + q;
244
245z + q;
246
247@
248
249This is total garbage, but it should be ignored.
250]])
251
252AT_CHECK([bison -o types.c types.y], 0, [], ignore)
253AT_COMPILE([types])
254AT_BISON_OPTION_POPDEFS
255])
256
257m4_define([_AT_RESOLVED_GLR_OUTPUT],
258[[+(z,q)
259<declare>(T,x)
260<init-declare>(T,x,y)
261=(x,y)
262+(<cast>(x,T),y)
263<declare>(T,x)
264<init-declare>(T,y,+(z,q))
265<error>
266+(z,q)
267]])
268
269m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC],
270[[3.1-3.6: +(z,q)
2715.1-5.4: <declare>(T,x)
2727.1-7.8: <init-declare>(T,x,y)
2739.1-9.6: =(x,y)
27411.1-11.10: +(<cast>(x,T),y)
27513.1-13.6: <declare>(T,x)
27615.1-15.14: <init-declare>(T,y,+(z,q))
27717.6-17.16: <error>
27819.1-19.6: +(z,q)
279]])
280
281m4_define([_AT_AMBIG_GLR_OUTPUT],
282[[+(z,q)
283<declare>(T,x)
284<init-declare>(T,x,y)
285=(x,y)
286+(<cast>(x,T),y)
287<OR>(<declare>(T,x),<cast>(x,T))
288<OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
289<error>
290+(z,q)
291]])
292
293m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC],
294[[3.1-3.6: +(z,q)
2955.1-5.4: <declare>(T,x)
2967.1-7.8: <init-declare>(T,x,y)
2979.1-9.6: =(x,y)
29811.1-11.10: +(<cast>(x,T),y)
29913.1-13.6: <OR>(<declare>(T,x),<cast>(x,T))
30015.1-15.14: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q)))
30117.6-17.16: <error>
30219.1-19.6: +(z,q)
303]])
304
305m4_define([_AT_GLR_STDERR],
306[[syntax error
307]])
308
309m4_define([_AT_VERBOSE_GLR_STDERR],
310[[syntax error, unexpected ID, expecting '=' or '+' or ')'
311]])
312
313## ---------------------------------------------------- ##
314## Compile the grammar described in the documentation. ##
315## ---------------------------------------------------- ##
316
317AT_SETUP([GLR: Resolve ambiguity, impure, no locations])
318_AT_TEST_GLR_CXXTYPES([],
319 [%dprec 1], [%dprec 2])
320AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
321 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
322AT_CLEANUP
323
324AT_SETUP([GLR: Resolve ambiguity, impure, locations])
325_AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2])
326AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
327 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
328AT_CLEANUP
329
330AT_SETUP([GLR: Resolve ambiguity, pure, no locations])
331_AT_TEST_GLR_CXXTYPES([%pure-parser],
332 [%dprec 1], [%dprec 2])
333AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
334 _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR)
335AT_CLEANUP
336
337AT_SETUP([GLR: Resolve ambiguity, pure, locations])
338_AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
339 [%dprec 1], [%dprec 2])
340AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
341 _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
342AT_CLEANUP
343
344AT_SETUP([GLR: Merge conflicting parses, impure, no locations])
345_AT_TEST_GLR_CXXTYPES([],
346 [%merge <stmtMerge>], [%merge <stmtMerge>])
347AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
348 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
349AT_CLEANUP
350
351AT_SETUP([GLR: Merge conflicting parses, impure, locations])
352_AT_TEST_GLR_CXXTYPES([%locations],
353 [%merge <stmtMerge>], [%merge <stmtMerge>])
354AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
355 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
356AT_CLEANUP
357
358AT_SETUP([GLR: Merge conflicting parses, pure, no locations])
359_AT_TEST_GLR_CXXTYPES([%pure-parser],
360 [%merge <stmtMerge>], [%merge <stmtMerge>])
361AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
362 _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR)
363AT_CLEANUP
364AT_SETUP([GLR: Merge conflicting parses, pure, locations])
365_AT_TEST_GLR_CXXTYPES([%pure-parser %locations],
366 [%merge <stmtMerge>],[%merge <stmtMerge>])
367AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
368 _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR)
369AT_CLEANUP
370
371AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations])
372_AT_TEST_GLR_CXXTYPES([%error-verbose],
373 [%merge <stmtMerge>], [%merge <stmtMerge>])
374AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0,
375 _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR)
376AT_CLEANUP