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