53 |
char *strcat(char *dest, const char *src); |
char *strcat(char *dest, const char *src); |
54 |
#endif |
#endif |
55 |
#include <ctype.h> |
#include <ctype.h> |
56 |
|
#include <assert.h> |
57 |
|
|
58 |
extern void perror (const char *s); |
extern void perror (const char *s); |
59 |
|
|
60 |
/* Exercise pre-prologue dependency to %union. */ |
/* Exercise pre-prologue dependency to %union. */ |
61 |
typedef int value_t; |
typedef int value_t; |
62 |
|
|
63 |
|
value_t global_result = 0; |
64 |
|
int global_count = 0; |
65 |
|
|
66 |
%} |
%} |
67 |
|
|
68 |
|
%parse-param "value_t *result", "result" |
69 |
|
%parse-param "int *count", "count" |
70 |
|
|
71 |
/* Exercise %union. */ |
/* Exercise %union. */ |
72 |
%union |
%union |
73 |
{ |
{ |
127 |
%% |
%% |
128 |
input: |
input: |
129 |
line |
line |
130 |
| input line |
| input line { ++*count; ++global_count; } |
131 |
; |
; |
132 |
|
|
133 |
line: |
line: |
134 |
'\n' |
'\n' |
135 |
| exp '\n' |
| exp '\n' { *result = global_result = $1; } |
136 |
; |
; |
137 |
|
|
138 |
exp: |
exp: |
141 |
{ |
{ |
142 |
if ($1 != $3) |
if ($1 != $3) |
143 |
fprintf (stderr, "calc: error: %d != %d\n", $1, $3); |
fprintf (stderr, "calc: error: %d != %d\n", $1, $3); |
144 |
$$ = $1 == $3; |
$$ = $1; |
145 |
} |
} |
146 |
| exp '+' exp { $$ = $1 + $3; } |
| exp '+' exp { $$ = $1 + $3; } |
147 |
| exp '-' exp { $$ = $1 - $3; } |
| exp '-' exp { $$ = $1 - $3; } |
293 |
int |
int |
294 |
main (int argc, const char **argv) |
main (int argc, const char **argv) |
295 |
{ |
{ |
296 |
|
value_t result = 0; |
297 |
|
int count = 0; |
298 |
yyin = NULL; |
yyin = NULL; |
299 |
|
|
300 |
if (argc == 2) |
if (argc == 2) |
311 |
#if YYDEBUG |
#if YYDEBUG |
312 |
yydebug = 1; |
yydebug = 1; |
313 |
#endif |
#endif |
314 |
yyparse (); |
yyparse (&result, &count); |
315 |
|
assert (global_result == result); |
316 |
|
assert (global_count == count); |
317 |
|
|
318 |
return 0; |
return 0; |
319 |
} |
} |
320 |
]]) |
]]) |
447 |
1 - (2 - 3) = 2 |
1 - (2 - 3) = 2 |
448 |
|
|
449 |
2^2^3 = 256 |
2^2^3 = 256 |
450 |
(2^2)^3 = 64], [486]) |
(2^2)^3 = 64], |
451 |
|
[486]) |
452 |
|
|
453 |
# Some parse errors. |
# Some parse errors. |
454 |
_AT_CHECK_CALC_ERROR([$1], [0 0], [11], |
_AT_CHECK_CALC_ERROR([$1], [0 0], [11], |
476 |
1.15-1.16: parse error, unexpected "number" |
1.15-1.16: parse error, unexpected "number" |
477 |
calc: error: 0 != 1]) |
calc: error: 0 != 1]) |
478 |
|
|
|
# Add a studid example demonstrating that Bison can further improve the |
|
|
# error message. FIXME: Fix this ridiculous message. |
|
|
_AT_CHECK_CALC_ERROR([$1], [()], [21], |
|
|
[1.2-1.3: parse error, unexpected ')', expecting "number" or '-' or '(']) |
|
|
|
|
479 |
AT_CLEANUP |
AT_CLEANUP |
480 |
])# AT_CHECK_CALC |
])# AT_CHECK_CALC |
481 |
|
|