]>
Commit | Line | Data |
---|---|---|
1154cced | 1 | # Checking GLR Parsing. -*- Autotest -*- |
e141f4d4 | 2 | # Copyright (C) 2002-2010 Free Software Foundation, Inc. |
12bebc04 | 3 | |
f16b0819 | 4 | # This program is free software: you can redistribute it and/or modify |
12bebc04 | 5 | # it under the terms of the GNU General Public License as published by |
f16b0819 PE |
6 | # the Free Software Foundation, either version 3 of the License, or |
7 | # (at your option) any later version. | |
8 | # | |
12bebc04 PH |
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. | |
f16b0819 | 13 | # |
12bebc04 | 14 | # You should have received a copy of the GNU General Public License |
f16b0819 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
12bebc04 PH |
16 | |
17 | AT_BANNER([[C++ Type Syntax (GLR).]]) | |
18 | ||
9501dc6e AD |
19 | # _AT_TEST_GLR_CXXTYPES(DECL, RESOLVE1, RESOLVE2) |
20 | # ----------------------------------------------- | |
12bebc04 PH |
21 | # Store into types.y the calc program, with DECL inserted as a declaration, |
22 | # and with RESOLVE1 and RESOLVE2 as annotations on the conflicted rule for | |
23 | # stmt. Then compile the result. | |
9501dc6e | 24 | m4_define([_AT_TEST_GLR_CXXTYPES], |
25005f6a PH |
25 | [ |
26 | AT_BISON_OPTION_PUSHDEFS([$1]) | |
27 | ||
28 | AT_DATA_GRAMMAR([types.y], | |
1154cced | 29 | [[/* Simplified C++ Type and Expression Grammar. */ |
12bebc04 | 30 | |
1154cced | 31 | $1 |
12bebc04 PH |
32 | |
33 | %{ | |
34 | #include <stdio.h> | |
2c3b392a AD |
35 | union Node { |
36 | struct { | |
9ecafbbf | 37 | int isNterm; |
2c3b392a | 38 | int parents; |
9ecafbbf | 39 | } nodeInfo; |
2c3b392a | 40 | struct { |
9ecafbbf | 41 | int isNterm; /* 1 */ |
2c3b392a AD |
42 | int parents; |
43 | char const *form; | |
44 | union Node *children[3]; | |
45 | } nterm; | |
46 | struct { | |
9ecafbbf | 47 | int isNterm; /* 0 */ |
2c3b392a AD |
48 | int parents; |
49 | char *text; | |
50 | } term; | |
51 | }; | |
52 | typedef union Node Node; | |
53 | static Node *new_nterm (char const *, Node *, Node *, Node *); | |
54 | static Node *new_term (char *); | |
55 | static void free_node (Node *); | |
56 | static char *node_to_string (Node *); | |
57 | #define YYSTYPE Node * | |
1154cced AD |
58 | ]m4_bmatch([$2], [stmtMerge], |
59 | [ static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);])[ | |
12bebc04 | 60 | #define YYINITDEPTH 10 |
e2688cd9 | 61 | #define YYSTACKEXPANDABLE 1 |
b8a204c0 PE |
62 | struct YYLTYPE; |
63 | #if YYPURE | |
64 | # if YYLSP_NEEDED | |
65 | # define LEX_PARAMETERS YYSTYPE *lvalp, struct YYLTYPE *llocp | |
66 | # define ERROR_PARAMETERS struct YYLTYPE *llocp, char const *s | |
67 | # else | |
68 | # define LEX_PARAMETERS YYSTYPE *lvalp | |
69 | # endif | |
70 | #endif | |
71 | #ifndef LEX_PARAMETERS | |
72 | # define LEX_PARAMETERS void | |
73 | #endif | |
74 | #ifndef ERROR_PARAMETERS | |
75 | # define ERROR_PARAMETERS char const *s | |
76 | #endif | |
77 | int yylex (LEX_PARAMETERS); | |
ac8c5689 | 78 | void yyerror (ERROR_PARAMETERS); |
12bebc04 PH |
79 | %} |
80 | ||
81 | %token TYPENAME ID | |
82 | ||
83 | %right '=' | |
84 | %left '+' | |
85 | ||
86 | %glr-parser | |
87 | ||
2c3b392a | 88 | %destructor { free_node ($$); } stmt expr decl declarator TYPENAME ID |
a22ff96f | 89 | |
12bebc04 PH |
90 | %% |
91 | ||
1154cced | 92 | prog : |
671881d1 | 93 | | prog stmt { |
9ecafbbf | 94 | char *output;]AT_LOCATION_IF([ |
671881d1 | 95 | printf ("%d.%d-%d.%d: ", |
25005f6a PH |
96 | @2.first_line, @2.first_column, |
97 | @2.last_line, @2.last_column);])[ | |
9ecafbbf | 98 | output = node_to_string (]$[2); |
f0064700 | 99 | printf ("%s\n", output); |
9ecafbbf AD |
100 | free (output); |
101 | free_node (]$[2); | |
25005f6a | 102 | } |
12bebc04 PH |
103 | ; |
104 | ||
25005f6a | 105 | stmt : expr ';' $2 { $$ = ]$[1; } |
1154cced | 106 | | decl $3 |
9ecafbbf | 107 | | error ';' { $$ = new_nterm ("<error>", 0, 0, 0); } |
04098407 | 108 | | '@' { YYACCEPT; } |
12bebc04 PH |
109 | ; |
110 | ||
25005f6a | 111 | expr : ID |
2c3b392a | 112 | | TYPENAME '(' expr ')' |
9ecafbbf AD |
113 | { $$ = new_nterm ("<cast>(%s,%s)", ]$[3, ]$[1, 0); } |
114 | | expr '+' expr { $$ = new_nterm ("+(%s,%s)", ]$[1, ]$[3, 0); } | |
2c3b392a | 115 | | expr '=' expr { $$ = new_nterm ("=(%s,%s)", ]$[1, ]$[3, 0); } |
12bebc04 PH |
116 | ; |
117 | ||
1154cced | 118 | decl : TYPENAME declarator ';' |
9ecafbbf | 119 | { $$ = new_nterm ("<declare>(%s,%s)", ]$[1, ]$[2, 0); } |
12bebc04 | 120 | | TYPENAME declarator '=' expr ';' |
9ecafbbf AD |
121 | { $$ = new_nterm ("<init-declare>(%s,%s,%s)", ]$[1, |
122 | ]$[2, ]$[4); } | |
12bebc04 PH |
123 | ; |
124 | ||
25005f6a PH |
125 | declarator : ID |
126 | | '(' declarator ')' { $$ = ]$[2; } | |
12bebc04 PH |
127 | ; |
128 | ||
129 | %% | |
130 | ||
131 | #include <ctype.h> | |
c469acce | 132 | #include <stdlib.h> |
1154cced | 133 | #include <string.h> |
25005f6a | 134 | #include <stdarg.h> |
12bebc04 | 135 | |
1154cced | 136 | int |
671881d1 | 137 | main (int argc, char **argv) |
12bebc04 | 138 | { |
500bbfcd PE |
139 | if (argc != 2) |
140 | abort (); | |
927f7817 | 141 | if (!freopen (argv[1], "r", stdin)) |
6100a9aa PE |
142 | return 3; |
143 | return yyparse (); | |
12bebc04 PH |
144 | } |
145 | ||
1154cced | 146 | int |
b8a204c0 | 147 | yylex (LEX_PARAMETERS) |
12bebc04 PH |
148 | { |
149 | char buffer[256]; | |
150 | int c; | |
c469acce | 151 | unsigned int i; |
25005f6a | 152 | static int lineNum = 1; |
e342c3be | 153 | static int colNum = 0; |
1154cced AD |
154 | |
155 | #if YYPURE | |
c66dfadd | 156 | # undef yylloc |
25005f6a | 157 | # define yylloc (*llocp) |
c66dfadd | 158 | # undef yylval |
1154cced | 159 | # define yylval (*lvalp) |
1154cced AD |
160 | #endif |
161 | ||
c469acce PE |
162 | while (1) |
163 | { | |
cf806753 PE |
164 | if (feof (stdin)) |
165 | abort (); | |
c469acce PE |
166 | c = getchar (); |
167 | switch (c) | |
168 | { | |
169 | case EOF: | |
170 | return 0; | |
25005f6a | 171 | case '\t': |
e342c3be | 172 | colNum = (colNum + 7) & ~7; |
25005f6a PH |
173 | break; |
174 | case ' ': case '\f': | |
175 | colNum += 1; | |
04098407 | 176 | break; |
671881d1 | 177 | case '\n': |
25005f6a | 178 | lineNum += 1; |
e342c3be | 179 | colNum = 0; |
c469acce PE |
180 | break; |
181 | default: | |
25005f6a PH |
182 | { |
183 | int tok; | |
184 | #if YYLSP_NEEDED | |
671881d1 | 185 | yylloc.first_line = yylloc.last_line = lineNum; |
25005f6a PH |
186 | yylloc.first_column = colNum; |
187 | #endif | |
188 | if (isalpha (c)) | |
189 | { | |
190 | i = 0; | |
191 | ||
192 | do | |
193 | { | |
194 | buffer[i++] = c; | |
195 | colNum += 1; | |
196 | if (i == sizeof buffer - 1) | |
197 | abort (); | |
198 | c = getchar (); | |
199 | } | |
200 | while (isalnum (c) || c == '_'); | |
201 | ||
202 | ungetc (c, stdin); | |
203 | buffer[i++] = 0; | |
204 | tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID; | |
9ecafbbf | 205 | yylval = new_term (strcpy ((char *) malloc (i), buffer)); |
25005f6a | 206 | } |
671881d1 | 207 | else |
25005f6a PH |
208 | { |
209 | colNum += 1; | |
210 | tok = c; | |
9ecafbbf | 211 | yylval = 0; |
25005f6a PH |
212 | } |
213 | #if YYLSP_NEEDED | |
214 | yylloc.last_column = colNum-1; | |
215 | #endif | |
216 | return tok; | |
217 | } | |
c469acce | 218 | } |
12bebc04 | 219 | } |
12bebc04 PH |
220 | } |
221 | ||
ac8c5689 | 222 | void |
b8a204c0 PE |
223 | yyerror (ERROR_PARAMETERS) |
224 | { | |
2a8d363a | 225 | #if YYPURE && YYLSP_NEEDED |
b8a204c0 PE |
226 | /* Pacify GCC by using llocp. */ |
227 | if (! llocp) | |
228 | abort (); | |
2a8d363a | 229 | #endif |
12bebc04 | 230 | fprintf (stderr, "%s\n", s); |
12bebc04 PH |
231 | } |
232 | ||
2c3b392a AD |
233 | static Node * |
234 | new_nterm (char const *form, Node *child0, Node *child1, Node *child2) | |
235 | { | |
9d9b8b70 | 236 | Node *node = (Node *) malloc (sizeof (Node)); |
9ecafbbf | 237 | node->nterm.isNterm = 1; |
2c3b392a AD |
238 | node->nterm.parents = 0; |
239 | node->nterm.form = form; | |
240 | node->nterm.children[0] = child0; | |
241 | if (child0) | |
9ecafbbf | 242 | child0->nodeInfo.parents += 1; |
2c3b392a AD |
243 | node->nterm.children[1] = child1; |
244 | if (child1) | |
9ecafbbf | 245 | child1->nodeInfo.parents += 1; |
2c3b392a AD |
246 | node->nterm.children[2] = child2; |
247 | if (child2) | |
9ecafbbf | 248 | child2->nodeInfo.parents += 1; |
2c3b392a AD |
249 | return node; |
250 | } | |
251 | ||
252 | static Node * | |
253 | new_term (char *text) | |
254 | { | |
9d9b8b70 | 255 | Node *node = (Node *) malloc (sizeof (Node)); |
9ecafbbf | 256 | node->term.isNterm = 0; |
2c3b392a AD |
257 | node->term.parents = 0; |
258 | node->term.text = text; | |
259 | return node; | |
260 | } | |
261 | ||
262 | static void | |
263 | free_node (Node *node) | |
264 | { | |
265 | if (!node) | |
266 | return; | |
9ecafbbf | 267 | node->nodeInfo.parents -= 1; |
2c3b392a | 268 | /* Free only if 0 (last parent) or -1 (no parents). */ |
9ecafbbf | 269 | if (node->nodeInfo.parents > 0) |
2c3b392a | 270 | return; |
9ecafbbf | 271 | if (node->nodeInfo.isNterm == 1) |
2c3b392a AD |
272 | { |
273 | free_node (node->nterm.children[0]); | |
274 | free_node (node->nterm.children[1]); | |
275 | free_node (node->nterm.children[2]); | |
276 | } | |
277 | else | |
278 | free (node->term.text); | |
279 | free (node); | |
280 | } | |
25005f6a | 281 | |
671881d1 | 282 | static char * |
2c3b392a | 283 | node_to_string (Node *node) |
25005f6a | 284 | { |
2c3b392a AD |
285 | char *child0; |
286 | char *child1; | |
287 | char *child2; | |
288 | char *buffer; | |
289 | if (!node) | |
290 | { | |
9d9b8b70 | 291 | buffer = (char *) malloc (1); |
2c3b392a AD |
292 | buffer[0] = 0; |
293 | } | |
9ecafbbf | 294 | else if (node->nodeInfo.isNterm == 1) |
2c3b392a AD |
295 | { |
296 | child0 = node_to_string (node->nterm.children[0]); | |
297 | child1 = node_to_string (node->nterm.children[1]); | |
298 | child2 = node_to_string (node->nterm.children[2]); | |
9d9b8b70 PE |
299 | buffer = (char *) malloc (strlen (node->nterm.form) + strlen (child0) |
300 | + strlen (child1) + strlen (child2) + 1); | |
2c3b392a AD |
301 | sprintf (buffer, node->nterm.form, child0, child1, child2); |
302 | free (child0); | |
303 | free (child1); | |
304 | free (child2); | |
305 | } | |
306 | else | |
307 | buffer = strdup (node->term.text); | |
308 | return buffer; | |
25005f6a PH |
309 | } |
310 | ||
1154cced AD |
311 | ]] |
312 | m4_bmatch([$2], [stmtMerge], | |
313 | [[static YYSTYPE | |
314 | stmtMerge (YYSTYPE x0, YYSTYPE x1) | |
12bebc04 | 315 | { |
2c3b392a | 316 | return new_nterm ("<OR>(%s,%s)", x0, x1, 0); |
12bebc04 PH |
317 | } |
318 | ]]) | |
1154cced | 319 | ) |
12bebc04 PH |
320 | |
321 | AT_DATA([test-input], | |
322 | [[ | |
323 | ||
324 | z + q; | |
325 | ||
326 | T x; | |
327 | ||
328 | T x = y; | |
329 | ||
330 | x = y; | |
331 | ||
332 | T (x) + y; | |
333 | ||
334 | T (x); | |
335 | ||
336 | T (y) = z + q; | |
337 | ||
338 | T (y y) = z + q; | |
339 | ||
340 | z + q; | |
341 | ||
342 | @ | |
343 | ||
344 | This is total garbage, but it should be ignored. | |
345 | ]]) | |
346 | ||
da730230 | 347 | AT_BISON_CHECK([-o types.c types.y], 0, [], ignore) |
1154cced | 348 | AT_COMPILE([types]) |
25005f6a | 349 | AT_BISON_OPTION_POPDEFS |
12bebc04 PH |
350 | ]) |
351 | ||
352 | m4_define([_AT_RESOLVED_GLR_OUTPUT], | |
215b40ac | 353 | [[[+(z,q) |
25005f6a PH |
354 | <declare>(T,x) |
355 | <init-declare>(T,x,y) | |
356 | =(x,y) | |
357 | +(<cast>(x,T),y) | |
358 | <declare>(T,x) | |
359 | <init-declare>(T,y,+(z,q)) | |
360 | <error> | |
361 | +(z,q) | |
215b40ac | 362 | ]]]) |
25005f6a PH |
363 | |
364 | m4_define([_AT_RESOLVED_GLR_OUTPUT_WITH_LOC], | |
215b40ac | 365 | [[[3.0-3.5: +(z,q) |
e342c3be AD |
366 | 5.0-5.3: <declare>(T,x) |
367 | 7.0-7.7: <init-declare>(T,x,y) | |
368 | 9.0-9.5: =(x,y) | |
369 | 11.0-11.9: +(<cast>(x,T),y) | |
370 | 13.0-13.5: <declare>(T,x) | |
371 | 15.0-15.13: <init-declare>(T,y,+(z,q)) | |
372 | 17.0-17.15: <error> | |
373 | 19.0-19.5: +(z,q) | |
215b40ac | 374 | ]]]) |
12bebc04 PH |
375 | |
376 | m4_define([_AT_AMBIG_GLR_OUTPUT], | |
215b40ac | 377 | [[[+(z,q) |
25005f6a PH |
378 | <declare>(T,x) |
379 | <init-declare>(T,x,y) | |
380 | =(x,y) | |
381 | +(<cast>(x,T),y) | |
382 | <OR>(<declare>(T,x),<cast>(x,T)) | |
383 | <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q))) | |
384 | <error> | |
385 | +(z,q) | |
215b40ac | 386 | ]]]) |
25005f6a PH |
387 | |
388 | m4_define([_AT_AMBIG_GLR_OUTPUT_WITH_LOC], | |
215b40ac | 389 | [[[3.0-3.5: +(z,q) |
e342c3be AD |
390 | 5.0-5.3: <declare>(T,x) |
391 | 7.0-7.7: <init-declare>(T,x,y) | |
392 | 9.0-9.5: =(x,y) | |
393 | 11.0-11.9: +(<cast>(x,T),y) | |
394 | 13.0-13.5: <OR>(<declare>(T,x),<cast>(x,T)) | |
395 | 15.0-15.13: <OR>(<init-declare>(T,y,+(z,q)),=(<cast>(y,T),+(z,q))) | |
396 | 17.0-17.15: <error> | |
397 | 19.0-19.5: +(z,q) | |
215b40ac | 398 | ]]]) |
12bebc04 | 399 | |
1154cced | 400 | m4_define([_AT_GLR_STDERR], |
215b40ac EB |
401 | [[[syntax error |
402 | ]]]) | |
12bebc04 | 403 | |
1154cced | 404 | m4_define([_AT_VERBOSE_GLR_STDERR], |
215b40ac EB |
405 | [[[syntax error, unexpected ID, expecting '=' or '+' or ')' |
406 | ]]]) | |
12bebc04 PH |
407 | |
408 | ## ---------------------------------------------------- ## | |
409 | ## Compile the grammar described in the documentation. ## | |
410 | ## ---------------------------------------------------- ## | |
411 | ||
412 | AT_SETUP([GLR: Resolve ambiguity, impure, no locations]) | |
9501dc6e | 413 | _AT_TEST_GLR_CXXTYPES([], |
9ecafbbf | 414 | [%dprec 1], [%dprec 2]) |
49b1cf79 | 415 | AT_PARSER_CHECK([[./types test-input]], 0, |
9ecafbbf | 416 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
417 | AT_CLEANUP |
418 | ||
419 | AT_SETUP([GLR: Resolve ambiguity, impure, locations]) | |
9501dc6e | 420 | _AT_TEST_GLR_CXXTYPES([%locations],[%dprec 1],[%dprec 2]) |
49b1cf79 | 421 | AT_PARSER_CHECK([[./types test-input]], 0, |
9ecafbbf | 422 | _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
423 | AT_CLEANUP |
424 | ||
425 | AT_SETUP([GLR: Resolve ambiguity, pure, no locations]) | |
d9df47b6 | 426 | _AT_TEST_GLR_CXXTYPES([%define api.pure], |
9ecafbbf | 427 | [%dprec 1], [%dprec 2]) |
49b1cf79 | 428 | AT_PARSER_CHECK([[./types test-input]], 0, |
9ecafbbf | 429 | _AT_RESOLVED_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
430 | AT_CLEANUP |
431 | ||
432 | AT_SETUP([GLR: Resolve ambiguity, pure, locations]) | |
d9df47b6 | 433 | _AT_TEST_GLR_CXXTYPES([%define api.pure %locations], |
9ecafbbf | 434 | [%dprec 1], [%dprec 2]) |
49b1cf79 | 435 | AT_PARSER_CHECK([[./types test-input]], 0, |
9ecafbbf | 436 | _AT_RESOLVED_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
437 | AT_CLEANUP |
438 | ||
439 | AT_SETUP([GLR: Merge conflicting parses, impure, no locations]) | |
9501dc6e | 440 | _AT_TEST_GLR_CXXTYPES([], |
9ecafbbf | 441 | [%merge <stmtMerge>], [%merge <stmtMerge>]) |
49b1cf79 | 442 | AT_PARSER_CHECK([[./types test-input]], 0, |
9ecafbbf | 443 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
444 | AT_CLEANUP |
445 | ||
446 | AT_SETUP([GLR: Merge conflicting parses, impure, locations]) | |
9501dc6e | 447 | _AT_TEST_GLR_CXXTYPES([%locations], |
9ecafbbf | 448 | [%merge <stmtMerge>], [%merge <stmtMerge>]) |
49b1cf79 | 449 | AT_PARSER_CHECK([[./types test-input]], 0, |
9ecafbbf | 450 | _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
451 | AT_CLEANUP |
452 | ||
453 | AT_SETUP([GLR: Merge conflicting parses, pure, no locations]) | |
d9df47b6 | 454 | _AT_TEST_GLR_CXXTYPES([%define api.pure], |
9ecafbbf | 455 | [%merge <stmtMerge>], [%merge <stmtMerge>]) |
49b1cf79 | 456 | AT_PARSER_CHECK([[./types test-input]], 0, |
9ecafbbf | 457 | _AT_AMBIG_GLR_OUTPUT, _AT_GLR_STDERR) |
12bebc04 PH |
458 | AT_CLEANUP |
459 | AT_SETUP([GLR: Merge conflicting parses, pure, locations]) | |
d9df47b6 | 460 | _AT_TEST_GLR_CXXTYPES([%define api.pure %locations], |
9ecafbbf | 461 | [%merge <stmtMerge>],[%merge <stmtMerge>]) |
49b1cf79 | 462 | AT_PARSER_CHECK([[./types test-input]], 0, |
9ecafbbf | 463 | _AT_AMBIG_GLR_OUTPUT_WITH_LOC, _AT_GLR_STDERR) |
12bebc04 PH |
464 | AT_CLEANUP |
465 | ||
466 | AT_SETUP([GLR: Verbose messages, resolve ambiguity, impure, no locations]) | |
9501dc6e | 467 | _AT_TEST_GLR_CXXTYPES([%error-verbose], |
9ecafbbf | 468 | [%merge <stmtMerge>], [%merge <stmtMerge>]) |
49b1cf79 | 469 | AT_PARSER_CHECK([[./types test-input]], 0, |
9ecafbbf | 470 | _AT_AMBIG_GLR_OUTPUT, _AT_VERBOSE_GLR_STDERR) |
12bebc04 | 471 | AT_CLEANUP |