]>
Commit | Line | Data |
---|---|---|
82c035a8 | 1 | # Executing Actions. -*- Autotest -*- |
1f916a78 | 2 | # Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc. |
82c035a8 AD |
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([[User Actions.]]) | |
20 | ||
21 | ## ------------------ ## | |
22 | ## Mid-rule actions. ## | |
23 | ## ------------------ ## | |
24 | ||
25 | AT_SETUP([Mid-rule actions]) | |
26 | ||
27 | # Bison once forgot the mid-rule actions. It was because the action | |
28 | # was attached to the host rule (the one with the mid-rule action), | |
29 | # instead of being attached to the empty rule dedicated to this | |
30 | # action. | |
31 | ||
9501dc6e | 32 | AT_DATA_GRAMMAR([[input.y]], |
82c035a8 | 33 | [[%{ |
d99361e6 AD |
34 | # include <stdio.h> |
35 | # include <stdlib.h> | |
36 | static void yyerror (const char *msg); | |
37 | static int yylex (void); | |
38 | # define YYDEBUG 1 | |
39 | # define YYERROR_VERBOSE 1 | |
82c035a8 AD |
40 | %} |
41 | %% | |
931394cb AD |
42 | exp: { putchar ('0'); } |
43 | '1' { putchar ('1'); } | |
44 | '2' { putchar ('2'); } | |
45 | '3' { putchar ('3'); } | |
46 | '4' { putchar ('4'); } | |
47 | '5' { putchar ('5'); } | |
48 | '6' { putchar ('6'); } | |
49 | '7' { putchar ('7'); } | |
50 | '8' { putchar ('8'); } | |
51 | '9' { putchar ('9'); } | |
52 | { putchar ('\n'); } | |
82c035a8 AD |
53 | ; |
54 | %% | |
55 | static int | |
56 | yylex (void) | |
57 | { | |
58 | static const char *input = "123456789"; | |
59 | return *input++; | |
60 | } | |
61 | ||
62 | static void | |
63 | yyerror (const char *msg) | |
64 | { | |
65 | fprintf (stderr, "%s\n", msg); | |
66 | } | |
67 | ||
68 | int | |
69 | main (void) | |
70 | { | |
71 | return yyparse (); | |
72 | } | |
73 | ]]) | |
74 | ||
b56471a6 | 75 | AT_CHECK([bison -d -v -o input.c input.y]) |
1154cced AD |
76 | AT_COMPILE([input]) |
77 | AT_PARSER_CHECK([./input], 0, | |
931394cb | 78 | [[0123456789 |
82c035a8 AD |
79 | ]]) |
80 | ||
81 | AT_CLEANUP | |
75d1fe16 AD |
82 | |
83 | ||
84 | ||
5dac0025 PE |
85 | |
86 | ||
75d1fe16 AD |
87 | ## ---------------- ## |
88 | ## Exotic Dollars. ## | |
89 | ## ---------------- ## | |
90 | ||
91 | AT_SETUP([Exotic Dollars]) | |
92 | ||
9501dc6e | 93 | AT_DATA_GRAMMAR([[input.y]], |
75d1fe16 AD |
94 | [[%{ |
95 | # include <stdio.h> | |
96 | # include <stdlib.h> | |
97 | static void yyerror (const char *msg); | |
98 | static int yylex (void); | |
99 | # define YYDEBUG 1 | |
100 | # define YYERROR_VERBOSE 1 | |
101 | %} | |
102 | ||
103 | %union | |
104 | { | |
105 | int val; | |
106 | }; | |
107 | ||
0ff67d71 | 108 | %type <val> a_1 a_2 a_5 |
75d1fe16 AD |
109 | sum_of_the_five_previous_values |
110 | ||
111 | %% | |
0ff67d71 PE |
112 | exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5 |
113 | sum_of_the_five_previous_values | |
75d1fe16 AD |
114 | { |
115 | printf ("%d\n", $6); | |
116 | } | |
117 | ; | |
118 | a_1: { $$ = 1; }; | |
119 | a_2: { $$ = 2; }; | |
75d1fe16 AD |
120 | a_5: { $$ = 5; }; |
121 | ||
122 | sum_of_the_five_previous_values: | |
123 | { | |
124 | $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4; | |
125 | } | |
126 | ; | |
127 | ||
128 | %% | |
129 | static int | |
130 | yylex (void) | |
131 | { | |
132 | return EOF; | |
133 | } | |
134 | ||
135 | static void | |
136 | yyerror (const char *msg) | |
137 | { | |
138 | fprintf (stderr, "%s\n", msg); | |
139 | } | |
140 | ||
141 | int | |
142 | main (void) | |
143 | { | |
144 | return yyparse (); | |
145 | } | |
146 | ]]) | |
147 | ||
b56471a6 | 148 | AT_CHECK([bison -d -v -o input.c input.y]) |
1154cced AD |
149 | AT_COMPILE([input]) |
150 | AT_PARSER_CHECK([./input], 0, | |
75d1fe16 AD |
151 | [[15 |
152 | ]]) | |
153 | ||
154 | AT_CLEANUP | |
9280d3ef AD |
155 | |
156 | ||
157 | ||
e776192e AD |
158 | ## -------------------------- ## |
159 | ## Printers and Destructors. ## | |
160 | ## -------------------------- ## | |
9280d3ef | 161 | |
ac700aa6 PE |
162 | # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG) |
163 | # ----------------------------------------------------------------------------- | |
3df37415 | 164 | m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR], |
9c66f418 AD |
165 | [# Make sure complex $n work. |
166 | m4_if([$1$2$3], $[1]$[2]$[3], [], | |
3df37415 AD |
167 | [m4_fatal([$0: Invalid arguments: $@])])dnl |
168 | ||
9c66f418 AD |
169 | # Be sure to pass all the %directives to this macro to have correct |
170 | # helping macros. So don't put any directly in the Bison file. | |
c2729758 | 171 | AT_BISON_OPTION_PUSHDEFS([$5]) |
9501dc6e | 172 | AT_DATA_GRAMMAR([[input.y]], |
9c66f418 | 173 | [[%{ |
9280d3ef AD |
174 | #include <stdio.h> |
175 | #include <stdlib.h> | |
9c66f418 | 176 | #include <assert.h> |
c2729758 ADL |
177 | ]AT_LALR1_CC_IF( |
178 | [#define RANGE(Location) (Location).begin.line, (Location).end.line], | |
179 | [#define RANGE(Location) (Location).first_line, (Location).last_line]) | |
180 | [%} | |
9c66f418 AD |
181 | |
182 | $5] | |
183 | m4_ifval([$6], [%union | |
9280d3ef AD |
184 | { |
185 | int ival; | |
ac700aa6 PE |
186 | }]) |
187 | [ | |
c2729758 | 188 | %{ |
ac700aa6 | 189 | ]AT_LALR1_CC_IF([typedef yy::Location YYLTYPE; |
9c66f418 | 190 | m4_ifval([$6], , [#define YYSTYPE int])]) |
c2729758 | 191 | [static int yylex (]AT_LEX_FORMALS[); |
ca6f187f PE |
192 | ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);]) |
193 | [%} | |
c2729758 | 194 | |
9c66f418 | 195 | ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input])[ |
e3170060 | 196 | |
a5eb1ed2 AD |
197 | %printer |
198 | { | |
1576d44d | 199 | ]AT_LALR1_CC_IF([cdebug_ << $$;], |
3fc16193 | 200 | [fprintf (yyoutput, "%d", $$)])[; |
a5eb1ed2 | 201 | } |
9c66f418 | 202 | input line thing 'x' 'y' |
e3170060 AD |
203 | |
204 | %destructor | |
4c6cc1db | 205 | { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e | 206 | input |
5719c109 | 207 | |
7bd6c77e | 208 | %destructor |
4c6cc1db | 209 | { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e AD |
210 | line |
211 | ||
212 | %destructor | |
4c6cc1db | 213 | { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e AD |
214 | thing |
215 | ||
216 | %destructor | |
4c6cc1db | 217 | { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); } |
7bd6c77e | 218 | 'x' |
5719c109 | 219 | |
9c66f418 AD |
220 | %destructor |
221 | { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); } | |
222 | 'y' | |
223 | ||
9280d3ef | 224 | %% |
9c66f418 AD |
225 | /* |
226 | This grammar is made to exercise error recovery. | |
227 | "Lines" starting with `(' support error recovery, with | |
228 | ')' as synchronizing token. Lines starting with 'x' can never | |
229 | be recovered from if in error. | |
230 | */ | |
231 | ||
9280d3ef AD |
232 | input: |
233 | /* Nothing. */ | |
5719c109 AD |
234 | { |
235 | $$ = 0; | |
16f37b35 | 236 | printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$)); |
5719c109 AD |
237 | } |
238 | | line input /* Right recursive to load the stack so that popping at | |
239 | EOF can be exercised. */ | |
240 | { | |
241 | $$ = 2; | |
16f37b35 | 242 | printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n", |
4c6cc1db | 243 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2)); |
5719c109 | 244 | } |
9280d3ef AD |
245 | ; |
246 | ||
247 | line: | |
248 | thing thing thing ';' | |
5719c109 AD |
249 | { |
250 | $$ = $1; | |
16f37b35 | 251 | printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n", |
4c6cc1db | 252 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), |
16f37b35 | 253 | $3, RANGE (@3), $4, RANGE (@4)); |
5719c109 | 254 | } |
9c66f418 | 255 | | '(' thing thing ')' |
5719c109 AD |
256 | { |
257 | $$ = $1; | |
9c66f418 AD |
258 | printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n", |
259 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), | |
260 | $3, RANGE (@3), $4, RANGE (@4)); | |
5719c109 | 261 | } |
9c66f418 | 262 | | '(' thing ')' |
5719c109 AD |
263 | { |
264 | $$ = $1; | |
9c66f418 AD |
265 | printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n", |
266 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3)); | |
5719c109 | 267 | } |
9c66f418 | 268 | | '(' error ')' |
5719c109 AD |
269 | { |
270 | $$ = -1; | |
9c66f418 AD |
271 | printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n", |
272 | $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3)); | |
5719c109 | 273 | } |
9280d3ef AD |
274 | ; |
275 | ||
276 | thing: | |
5719c109 AD |
277 | 'x' |
278 | { | |
279 | $$ = $1; | |
4c6cc1db AD |
280 | printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n", |
281 | $$, RANGE (@$), $1, RANGE (@1)); | |
5719c109 | 282 | } |
9280d3ef AD |
283 | ; |
284 | %% | |
9c66f418 AD |
285 | /* Alias to ARGV[1]. */ |
286 | const char *yysource = 0; | |
287 | ||
9280d3ef | 288 | static int |
c2729758 | 289 | yylex (]AT_LEX_FORMALS[) |
9280d3ef | 290 | { |
5a08f1ce | 291 | static unsigned int counter = 0; |
9280d3ef | 292 | |
9c66f418 AD |
293 | int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++; |
294 | /* As in BASIC, line numbers go from 10 to 10. */ | |
c2729758 | 295 | ]AT_LALR1_CC_IF( |
9c66f418 AD |
296 | [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c; |
297 | AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9; | |
c2729758 | 298 | ], |
9c66f418 AD |
299 | [ AT_LOC.first_line = AT_LOC.first_column = 10 * c; |
300 | AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9; | |
c2729758 | 301 | ])[ |
9c66f418 AD |
302 | |
303 | if (yysource[c]) | |
304 | printf ("sending: '%c'", yysource[c]); | |
9280d3ef | 305 | else |
9c66f418 AD |
306 | printf ("sending: EOF"); |
307 | printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[)); | |
308 | return yysource[c]; | |
9280d3ef AD |
309 | } |
310 | ||
c2729758 ADL |
311 | ]AT_LALR1_CC_IF( |
312 | [/* Currently, print_ is required in C++. */ | |
313 | void | |
314 | yy::Parser::print_ () | |
315 | { | |
316 | std::cerr << location; | |
317 | } | |
318 | ||
319 | /* A C++ error reporting function. */ | |
320 | void | |
321 | yy::Parser::error_ () | |
322 | { | |
323 | printf ("%d-%d: %s\n", RANGE (location), message.c_str()); | |
324 | } | |
325 | ||
326 | static bool yydebug; | |
327 | int | |
328 | yyparse () | |
329 | { | |
451364ed | 330 | yy::Parser parser (yydebug); |
c2729758 ADL |
331 | return parser.parse (); |
332 | } | |
333 | ], | |
334 | [static void | |
9280d3ef AD |
335 | yyerror (const char *msg) |
336 | { | |
4c6cc1db | 337 | printf ("%d-%d: %s\n", RANGE (yylloc), msg); |
c2729758 | 338 | }])[ |
9280d3ef | 339 | |
9280d3ef | 340 | int |
9c66f418 | 341 | main (int argc, const char *argv[]) |
9280d3ef AD |
342 | { |
343 | yydebug = !!getenv ("YYDEBUG"); | |
9c66f418 AD |
344 | assert (argc == 2); |
345 | yysource = argv[1]; | |
9280d3ef AD |
346 | if (yyparse ()) |
347 | { | |
4c6cc1db | 348 | printf ("Parsing FAILED.\n"); |
9280d3ef AD |
349 | exit (1); |
350 | } | |
4c6cc1db | 351 | printf ("Successful parse.\n"); |
9280d3ef AD |
352 | return 0; |
353 | } | |
354 | ]]) | |
355 | ||
07971983 PE |
356 | AT_LALR1_CC_IF( |
357 | [AT_CHECK([bison -o input.cc input.y]) | |
358 | AT_COMPILE_CXX([input])], | |
359 | [AT_CHECK([bison -o input.c input.y]) | |
360 | AT_COMPILE([input])]) | |
9c66f418 AD |
361 | |
362 | ||
363 | # Check the location of "empty" | |
364 | # ----------------------------- | |
365 | # I.e., epsilon-reductions, as in "(x)" which ends by reducing | |
366 | # an empty "line" nterm. | |
367 | # FIXME: This location is not satisfying. Depend on the lookahead? | |
368 | AT_PARSER_CHECK([./input '(x)'], 0, | |
369 | [[sending: '(' (0@0-9) | |
370 | sending: 'x' (1@10-19) | |
371 | thing (1@10-19): 'x' (1@10-19) | |
372 | sending: ')' (2@20-29) | |
373 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
374 | sending: EOF (3@30-39) | |
b4a20338 AD |
375 | input (0@29-29): /* Nothing */ |
376 | input (2@0-29): line (0@0-29) input (0@29-29) | |
9c66f418 AD |
377 | Successful parse. |
378 | ]]) | |
379 | ||
380 | ||
381 | # Check locations in error recovery | |
382 | # --------------------------------- | |
383 | # '(y)' is an error, but can be recovered from. But what's the location | |
384 | # of the error itself ('y'), and of the resulting reduction ('(error)'). | |
385 | AT_PARSER_CHECK([./input '(y)'], 0, | |
386 | [[sending: '(' (0@0-9) | |
387 | sending: 'y' (1@10-19) | |
388 | 10-19: syntax error, unexpected 'y', expecting 'x' | |
389 | Freeing token 'y' (1@10-19) | |
390 | sending: ')' (2@20-29) | |
391 | line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29) | |
392 | sending: EOF (3@30-39) | |
b4a20338 AD |
393 | input (0@29-29): /* Nothing */ |
394 | input (2@0-29): line (-1@0-29) input (0@29-29) | |
9c66f418 AD |
395 | Successful parse. |
396 | ]]) | |
397 | ||
398 | ||
399 | # Syntax errors caught by the parser | |
400 | # ---------------------------------- | |
401 | # Exercise the discarding of stack top and input until `error' | |
402 | # can be reduced. | |
403 | # | |
404 | # '(', 'x', 'x', 'x', 'x', 'x', ')', | |
405 | # | |
406 | # Load the stack and provoke an error that cannot be caught by the | |
407 | # grammar, to check that the stack is cleared. And make sure the | |
408 | # lookahead is freed. | |
409 | # | |
410 | # '(', 'x', ')', | |
411 | # '(', 'x', ')', | |
412 | # 'y' | |
413 | AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1, | |
414 | [[sending: '(' (0@0-9) | |
4c6cc1db AD |
415 | sending: 'x' (1@10-19) |
416 | thing (1@10-19): 'x' (1@10-19) | |
417 | sending: 'x' (2@20-29) | |
418 | thing (2@20-29): 'x' (2@20-29) | |
419 | sending: 'x' (3@30-39) | |
9c66f418 | 420 | 30-39: syntax error, unexpected 'x', expecting ')' |
4c6cc1db AD |
421 | Freeing nterm thing (2@20-29) |
422 | Freeing nterm thing (1@10-19) | |
4c6cc1db AD |
423 | Freeing token 'x' (3@30-39) |
424 | sending: 'x' (4@40-49) | |
425 | Freeing token 'x' (4@40-49) | |
426 | sending: 'x' (5@50-59) | |
427 | Freeing token 'x' (5@50-59) | |
9c66f418 AD |
428 | sending: ')' (6@60-69) |
429 | line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69) | |
430 | sending: '(' (7@70-79) | |
4c6cc1db AD |
431 | sending: 'x' (8@80-89) |
432 | thing (8@80-89): 'x' (8@80-89) | |
9c66f418 AD |
433 | sending: ')' (9@90-99) |
434 | line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99) | |
435 | sending: '(' (10@100-109) | |
436 | sending: 'x' (11@110-119) | |
437 | thing (11@110-119): 'x' (11@110-119) | |
438 | sending: ')' (12@120-129) | |
439 | line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129) | |
440 | sending: 'y' (13@130-139) | |
b4a20338 AD |
441 | input (0@129-129): /* Nothing */ |
442 | input (2@100-129): line (10@100-129) input (0@129-129) | |
9c66f418 AD |
443 | input (2@70-129): line (7@70-99) input (2@100-129) |
444 | input (2@0-129): line (-1@0-69) input (2@70-129) | |
445 | 130-139: syntax error, unexpected 'y', expecting $end | |
446 | Freeing nterm input (2@0-129) | |
447 | Freeing token 'y' (13@130-139) | |
5719c109 | 448 | Parsing FAILED. |
9280d3ef AD |
449 | ]]) |
450 | ||
3df37415 AD |
451 | ]) |
452 | ||
453 | ||
ac700aa6 PE |
454 | # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG]) |
455 | # -------------------------------------------------------------- | |
3df37415 | 456 | m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR], |
9c66f418 AD |
457 | [AT_SETUP([Printers and Destructors $2: $1]) |
458 | ||
459 | _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4], | |
460 | [%error-verbose | |
461 | %debug | |
462 | %verbose | |
463 | %locations | |
464 | $1], [$2]) | |
465 | ||
466 | AT_CLEANUP | |
3df37415 AD |
467 | ]) |
468 | ||
469 | ||
ac700aa6 PE |
470 | AT_CHECK_PRINTER_AND_DESTRUCTOR([]) |
471 | AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union]) | |
9c66f418 AD |
472 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"]) |
473 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union]) | |
1576d44d AD |
474 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser]) |
475 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union]) |