]>
Commit | Line | Data |
---|---|---|
1154cced | 1 | # Checking GLR Parsing. -*- Autotest -*- |
9a2b42a3 | 2 | # Copyright (C) 2002, 2003 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 PH |
125 | static int lineNum = 1; |
126 | static int colNum = 1; | |
127 | ||
1154cced AD |
128 | |
129 | #if YYPURE | |
25005f6a | 130 | # define yylloc (*llocp) |
1154cced | 131 | # define yylval (*lvalp) |
1154cced AD |
132 | #endif |
133 | ||
c469acce PE |
134 | while (1) |
135 | { | |
136 | c = getchar (); | |
137 | switch (c) | |
138 | { | |
139 | case EOF: | |
140 | return 0; | |
25005f6a PH |
141 | case '\t': |
142 | colNum = 1 + ((colNum + 7) & ~7); | |
143 | break; | |
144 | case ' ': case '\f': | |
145 | colNum += 1; | |
146 | break; | |
671881d1 | 147 | case '\n': |
25005f6a PH |
148 | lineNum += 1; |
149 | colNum = 1; | |
c469acce PE |
150 | break; |
151 | default: | |
25005f6a PH |
152 | { |
153 | int tok; | |
154 | #if YYLSP_NEEDED | |
671881d1 | 155 | yylloc.first_line = yylloc.last_line = lineNum; |
25005f6a PH |
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 | } | |
671881d1 | 177 | else |
25005f6a PH |
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 | } | |
c469acce | 188 | } |
12bebc04 | 189 | } |
12bebc04 PH |
190 | } |
191 | ||
192 | int | |
b8a204c0 PE |
193 | yyerror (ERROR_PARAMETERS) |
194 | { | |
2a8d363a | 195 | #if YYPURE && YYLSP_NEEDED |
b8a204c0 PE |
196 | /* Pacify GCC by using llocp. */ |
197 | if (! llocp) | |
198 | abort (); | |
2a8d363a | 199 | #endif |
12bebc04 PH |
200 | fprintf (stderr, "%s\n", s); |
201 | return 0; | |
202 | } | |
203 | ||
25005f6a | 204 | |
671881d1 PE |
205 | static char * |
206 | format (char const *form, ...) | |
25005f6a PH |
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 | ||
1154cced AD |
216 | ]] |
217 | m4_bmatch([$2], [stmtMerge], | |
218 | [[static YYSTYPE | |
219 | stmtMerge (YYSTYPE x0, YYSTYPE x1) | |
12bebc04 | 220 | { |
25005f6a | 221 | return format ("<OR>(%s,%s)", x0, x1); |
12bebc04 PH |
222 | } |
223 | ]]) | |
1154cced | 224 | ) |
12bebc04 PH |
225 | |
226 | AT_DATA([test-input], | |
227 | [[ | |
228 | ||
229 | z + q; | |
230 | ||
231 | T x; | |
232 | ||
233 | T x = y; | |
234 | ||
235 | x = y; | |
236 | ||
237 | T (x) + y; | |
238 | ||
239 | T (x); | |
240 | ||
241 | T (y) = z + q; | |
242 | ||
243 | T (y y) = z + q; | |
244 | ||
245 | z + q; | |
246 | ||
247 | @ | |
248 | ||
249 | This is total garbage, but it should be ignored. | |
250 | ]]) | |
251 | ||
b56471a6 | 252 | AT_CHECK([bison -o types.c types.y], 0, [], ignore) |
1154cced | 253 | AT_COMPILE([types]) |
25005f6a | 254 | AT_BISON_OPTION_POPDEFS |
12bebc04 PH |
255 | ]) |
256 | ||
257 | m4_define([_AT_RESOLVED_GLR_OUTPUT], | |
25005f6a PH |
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 | ||
269 | m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC], | |
270 | [[3.1-3.6: +(z,q) | |
271 | 5.1-5.4: <declare>(T,x) | |
272 | 7.1-7.8: <init-declare>(T,x,y) | |
273 | 9.1-9.6: =(x,y) | |
274 | 11.1-11.10: +(<cast>(x,T),y) | |
275 | 13.1-13.6: <declare>(T,x) | |
276 | 15.1-15.14: <init-declare>(T,y,+(z,q)) | |
277 | 17.6-17.16: <error> | |
278 | 19.1-19.6: +(z,q) | |
12bebc04 PH |
279 | ]]) |
280 | ||
281 | m4_define([_AT_AMBIG_GLR_OUTPUT], | |
25005f6a PH |
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 | ||
293 | m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC], | |
294 | [[3.1-3.6: +(z,q) | |
295 | 5.1-5.4: <declare>(T,x) | |
296 | 7.1-7.8: <init-declare>(T,x,y) | |
297 | 9.1-9.6: =(x,y) | |
298 | 11.1-11.10: +(<cast>(x,T),y) | |
299 | 13.1-13.6: <OR>(<declare>(T,x),<cast>(x,T)) | |
300 | 15.1-15.14: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q))) | |
301 | 17.6-17.16: <error> | |
302 | 19.1-19.6: +(z,q) | |
12bebc04 PH |
303 | ]]) |
304 | ||
1154cced | 305 | m4_define([_AT_GLR_STDERR], |
6e649e65 | 306 | [[syntax error |
12bebc04 PH |
307 | ]]) |
308 | ||
1154cced | 309 | m4_define([_AT_VERBOSE_GLR_STDERR], |
6e649e65 | 310 | [[syntax error, unexpected ID, expecting '=' or '+' or ')' |
12bebc04 PH |
311 | ]]) |
312 | ||
313 | ## ---------------------------------------------------- ## | |
314 | ## Compile the grammar described in the documentation. ## | |
315 | ## ---------------------------------------------------- ## | |
316 | ||
317 | AT_SETUP([GLR: Resolve ambiguity, impure, no locations]) | |
9501dc6e AD |
318 | _AT_TEST_GLR_CXXTYPES([], |
319 | [%dprec 1], [%dprec 2]) | |
9e32add8 AD |
320 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
321 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) | |
12bebc04 PH |
322 | AT_CLEANUP |
323 | ||
324 | AT_SETUP([GLR: Resolve ambiguity, impure, locations]) | |
9501dc6e | 325 | _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2]) |
9e32add8 | 326 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
25005f6a | 327 | _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
328 | AT_CLEANUP |
329 | ||
330 | AT_SETUP([GLR: Resolve ambiguity, pure, no locations]) | |
9501dc6e AD |
331 | _AT_TEST_GLR_CXXTYPES([%pure-parser], |
332 | [%dprec 1], [%dprec 2]) | |
9e32add8 AD |
333 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
334 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) | |
12bebc04 PH |
335 | AT_CLEANUP |
336 | ||
337 | AT_SETUP([GLR: Resolve ambiguity, pure, locations]) | |
9501dc6e AD |
338 | _AT_TEST_GLR_CXXTYPES([%pure-parser %locations], |
339 | [%dprec 1], [%dprec 2]) | |
1154cced | 340 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
25005f6a | 341 | _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
342 | AT_CLEANUP |
343 | ||
344 | AT_SETUP([GLR: Merge conflicting parses, impure, no locations]) | |
9501dc6e AD |
345 | _AT_TEST_GLR_CXXTYPES([], |
346 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
1154cced | 347 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
9e32add8 | 348 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
349 | AT_CLEANUP |
350 | ||
351 | AT_SETUP([GLR: Merge conflicting parses, impure, locations]) | |
9501dc6e AD |
352 | _AT_TEST_GLR_CXXTYPES([%locations], |
353 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
1154cced | 354 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
25005f6a | 355 | _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
356 | AT_CLEANUP |
357 | ||
358 | AT_SETUP([GLR: Merge conflicting parses, pure, no locations]) | |
9501dc6e AD |
359 | _AT_TEST_GLR_CXXTYPES([%pure-parser], |
360 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
1154cced | 361 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
9e32add8 | 362 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
363 | AT_CLEANUP |
364 | AT_SETUP([GLR: Merge conflicting parses, pure, locations]) | |
9501dc6e AD |
365 | _AT_TEST_GLR_CXXTYPES([%pure-parser %locations], |
366 | [%merge <stmtMerge>],[%merge <stmtMerge>]) | |
1154cced | 367 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
25005f6a | 368 | _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
369 | AT_CLEANUP |
370 | ||
371 | AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations]) | |
9501dc6e AD |
372 | _AT_TEST_GLR_CXXTYPES([%error-verbose], |
373 | [%merge <stmtMerge>], [%merge <stmtMerge>]) | |
1154cced | 374 | AT_PARSER_CHECK([[./types test-input | sed 's/ *$//']], 0, |
9e32add8 | 375 | _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR) |
12bebc04 | 376 | AT_CLEANUP |