]>
Commit | Line | Data |
---|---|---|
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 | ||
19 | AT_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 | 26 | m4_define([_AT_TEST_GLR_CXXTYPES], |
25005f6a PH |
27 | [ |
28 | AT_BISON_OPTION_PUSHDEFS([$1]) | |
29 | ||
30 | AT_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 | 70 | prog : |
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 | 80 | stmt : expr ';' $2 { $$ = ]$[1; } |
1154cced | 81 | | decl $3 |
25005f6a PH |
82 | | error ';' { $$ = "<error>"; } |
83 | | '@' { YYACCEPT; } | |
12bebc04 PH |
84 | ; |
85 | ||
25005f6a PH |
86 | expr : 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 | 92 | decl : 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 |
98 | declarator : 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 | 109 | int |
671881d1 | 110 | main (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 | 119 | int |
b8a204c0 | 120 | yylex (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 | ||
191 | int | |
b8a204c0 PE |
192 | yyerror (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 |
204 | static char * |
205 | format (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 | ]] |
216 | m4_bmatch([$2], [stmtMerge], | |
217 | [[static YYSTYPE | |
218 | stmtMerge (YYSTYPE x0, YYSTYPE x1) | |
12bebc04 | 219 | { |
25005f6a | 220 | return format ("<OR>(%s,%s)", x0, x1); |
12bebc04 PH |
221 | } |
222 | ]]) | |
1154cced | 223 | ) |
12bebc04 PH |
224 | |
225 | AT_DATA([test-input], | |
226 | [[ | |
227 | ||
228 | z + q; | |
229 | ||
230 | T x; | |
231 | ||
232 | T x = y; | |
233 | ||
234 | x = y; | |
235 | ||
236 | T (x) + y; | |
237 | ||
238 | T (x); | |
239 | ||
240 | T (y) = z + q; | |
241 | ||
242 | T (y y) = z + q; | |
243 | ||
244 | z + q; | |
245 | ||
246 | @ | |
247 | ||
248 | This is total garbage, but it should be ignored. | |
249 | ]]) | |
250 | ||
b56471a6 | 251 | AT_CHECK([bison -o types.c types.y], 0, [], ignore) |
1154cced | 252 | AT_COMPILE([types]) |
25005f6a | 253 | AT_BISON_OPTION_POPDEFS |
12bebc04 PH |
254 | ]) |
255 | ||
256 | m4_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 | ||
268 | m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC], | |
e342c3be AD |
269 | [[3.0-3.5: +(z,q) |
270 | 5.0-5.3: <declare>(T,x) | |
271 | 7.0-7.7: <init-declare>(T,x,y) | |
272 | 9.0-9.5: =(x,y) | |
273 | 11.0-11.9: +(<cast>(x,T),y) | |
274 | 13.0-13.5: <declare>(T,x) | |
275 | 15.0-15.13: <init-declare>(T,y,+(z,q)) | |
276 | 17.0-17.15: <error> | |
277 | 19.0-19.5: +(z,q) | |
12bebc04 PH |
278 | ]]) |
279 | ||
280 | m4_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 | ||
292 | m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC], | |
e342c3be AD |
293 | [[3.0-3.5: +(z,q) |
294 | 5.0-5.3: <declare>(T,x) | |
295 | 7.0-7.7: <init-declare>(T,x,y) | |
296 | 9.0-9.5: =(x,y) | |
297 | 11.0-11.9: +(<cast>(x,T),y) | |
298 | 13.0-13.5: <OR>(<declare>(T,x),<cast>(x,T)) | |
299 | 15.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q))) | |
300 | 17.0-17.15: <error> | |
301 | 19.0-19.5: +(z,q) | |
12bebc04 PH |
302 | ]]) |
303 | ||
1154cced | 304 | m4_define([_AT_GLR_STDERR], |
6e649e65 | 305 | [[syntax error |
12bebc04 PH |
306 | ]]) |
307 | ||
1154cced | 308 | m4_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 | ||
316 | AT_SETUP([GLR: Resolve ambiguity, impure, no locations]) | |
9501dc6e AD |
317 | _AT_TEST_GLR_CXXTYPES([], |
318 | [%dprec 1], [%dprec 2]) | |
9e32add8 AD |
319 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
320 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) | |
12bebc04 PH |
321 | AT_CLEANUP |
322 | ||
323 | AT_SETUP([GLR: Resolve ambiguity, impure, locations]) | |
9501dc6e | 324 | _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2]) |
9e32add8 | 325 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
25005f6a | 326 | _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
327 | AT_CLEANUP |
328 | ||
329 | AT_SETUP([GLR: Resolve ambiguity, pure, no locations]) | |
9501dc6e AD |
330 | _AT_TEST_GLR_CXXTYPES([%pure-parser], |
331 | [%dprec 1], [%dprec 2]) | |
9e32add8 AD |
332 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
333 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) | |
12bebc04 PH |
334 | AT_CLEANUP |
335 | ||
336 | AT_SETUP([GLR: Resolve ambiguity, pure, locations]) | |
9501dc6e AD |
337 | _AT_TEST_GLR_CXXTYPES([%pure-parser %locations], |
338 | [%dprec 1], [%dprec 2]) | |
1154cced | 339 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
25005f6a | 340 | _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
341 | AT_CLEANUP |
342 | ||
343 | AT_SETUP([GLR: Merge conflicting parses, impure, no locations]) | |
9501dc6e AD |
344 | _AT_TEST_GLR_CXXTYPES([], |
345 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
1154cced | 346 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
9e32add8 | 347 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
348 | AT_CLEANUP |
349 | ||
350 | AT_SETUP([GLR: Merge conflicting parses, impure, locations]) | |
9501dc6e AD |
351 | _AT_TEST_GLR_CXXTYPES([%locations], |
352 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
1154cced | 353 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
25005f6a | 354 | _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
355 | AT_CLEANUP |
356 | ||
357 | AT_SETUP([GLR: Merge conflicting parses, pure, no locations]) | |
9501dc6e AD |
358 | _AT_TEST_GLR_CXXTYPES([%pure-parser], |
359 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
1154cced | 360 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
9e32add8 | 361 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
362 | AT_CLEANUP |
363 | AT_SETUP([GLR: Merge conflicting parses, pure, locations]) | |
9501dc6e AD |
364 | _AT_TEST_GLR_CXXTYPES([%pure-parser %locations], |
365 | [%merge <stmtMerge>],[%merge <stmtMerge>]) | |
1154cced | 366 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
25005f6a | 367 | _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
368 | AT_CLEANUP |
369 | ||
370 | AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations]) | |
9501dc6e AD |
371 | _AT_TEST_GLR_CXXTYPES([%error-verbose], |
372 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
1154cced | 373 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
9e32add8 | 374 | _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR) |
12bebc04 | 375 | AT_CLEANUP |