* src/main.c (main): If there are complains after grammar

reductions, then output the report anyway if requested, then die.
* src/symtab.c (bucket_new): Initialize `value' to -1, not 0.
* src/reader.c (eoftoken): New.
(parse_token_decl): If the token being defined has value `0', it
is the eoftoken.
(packsymbols): No longer hack `tags' to insert `$' by hand.
Be sure to preserve the value of the eoftoken.
(reader): Make sure eoftoken is defined.
Initialize nsyms to 0: now eoftoken is created just like the others.
* src/print.c (print_grammar): Don't special case the eof token.
* src/regression.at: Adjust: `$' has value 0, not -1, which was a
lie anyway, albeit pleasant.
* tests/calc.at: Exercise error messages with eoftoken.
Change the grammar so that empty input is invalid.
Adjust expectations.
When yyungeting, be sure to use a valid yylloc: use last_yylloc.
This commit is contained in:
Akim Demaille
2001-12-27 18:13:09 +00:00
parent ec2da99ffe
commit b7c49edf13
8 changed files with 111 additions and 50 deletions

View File

@@ -64,8 +64,8 @@ extern void perror (const char *s);
%}
/* Bison Declarations */
%token CALC_EOF 0
%token NUM
%token CALC_EOF 0 "end of file"
%token NUM "number"
%nonassoc '=' /* comparison */
%left '-' '+'
@@ -78,7 +78,7 @@ extern void perror (const char *s);
/* Grammar follows */
%%
input:
/* empty string */
line
| input line
;
@@ -118,11 +118,16 @@ yyerror (const char *s)
fprintf (stderr, "%s\n", s);
}
#if YYLSP_NEEDED
static YYLTYPE last_yylloc;
#endif
static int
yygetc (void)
{
int res = getc (yyin);
#if YYLSP_NEEDED
last_yylloc = yylloc;
if (res == '\n')
{
yylloc.last_line++;
@@ -140,7 +145,7 @@ yyungetc (int c)
{
#if YYLSP_NEEDED
/* Wrong when C == `\n'. */
yylloc.last_column--;
yylloc = last_yylloc;
#endif
ungetc (c, yyin);
}
@@ -286,6 +291,9 @@ AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0,
# ------------------------------------------------------------
# Run `calc' on INPUT, and expect a `parse error' message.
#
# If INPUT starts with a slash, it is used as absolute input file name,
# otherwise as contents.
#
# If BISON-OPTIONS contains `--location', then make sure the ERROR-LOCATION
# is correctly output on stderr.
#
@@ -296,11 +304,12 @@ AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0,
# If BISON-OPTIONS contains `--debug', then NUM-STDERR-LINES is the number
# of expected lines on stderr.
m4_define([_AT_CHECK_CALC_ERROR],
[AT_DATA([[input]],
[m4_bmatch([$2], [^/],
[AT_CHECK([calc $2], 0, [], [stderr])],
[AT_DATA([[input]],
[[$2
]])
AT_CHECK([calc input], 0, [], [stderr])
AT_CHECK([calc input], 0, [], [stderr])])
AT_CHECK([wc -l <stderr | sed 's/[[^0-9]]//g'], 0,
@@ -362,27 +371,33 @@ _AT_CHECK_CALC([$1],
1 - (2 - 3) = 2
2^2^3 = 256
(2^2)^3 = 64], [491])
(2^2)^3 = 64], [488])
# Some parse errors.
_AT_CHECK_CALC_ERROR([$1], [+1], [8],
[1.0:1.1],
[unexpected '+'])
_AT_CHECK_CALC_ERROR([$1], [1//2], [17],
_AT_CHECK_CALC_ERROR([$1], [0 0], [10],
[1.2:1.3],
[unexpected '/', expecting NUM or '-' or '('])
_AT_CHECK_CALC_ERROR([$1], [error], [8],
[unexpected "number"])
_AT_CHECK_CALC_ERROR([$1], [1//2], [13],
[1.2:1.3],
[unexpected '/', expecting "number" or '-' or '('])
_AT_CHECK_CALC_ERROR([$1], [error], [4],
[1.0:1.1],
[unexpected $undefined.])
_AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [23],
[unexpected $undefined., expecting "number" or '-' or '\n' or '('])
_AT_CHECK_CALC_ERROR([$1], [1 = 2 = 3], [19],
[1.6:1.7],
[unexpected '='])
_AT_CHECK_CALC_ERROR([$1],
[
+1],
[16],
[13],
[2.0:2.1],
[unexpected '+'])
# Exercise error messages with EOF: work on an empty file.
_AT_CHECK_CALC_ERROR([$1],
[/dev/null],
[4],
[1.0:1.1],
[unexpected "end of file", expecting "number" or '-' or '\n' or '('])
AT_CLEANUP
])# AT_CHECK_CALC

View File

@@ -73,7 +73,7 @@ Grammar
Terminals, with rules where they appear
$ (-1)
$ (0)
error (256)
NUM (257) 2
OP (258) 1
@@ -177,7 +177,7 @@ Grammar
Terminals, with rules where they appear
$ (-1)
$ (0)
error (256)
NUM (257) 2
OP (258) 1
@@ -304,7 +304,7 @@ AT_CHECK([cat input.output], [],
Terminals, with rules where they appear
$ (-1)
$ (0)
'a' (97) 2
'b' (98) 2
'c' (99) 4
@@ -652,7 +652,7 @@ AT_CHECK([sed -n 's/ *$//;/^$/!p' input.output], 0,
4 15 @1 -> /* empty */
5 15 CONST_DEC -> @1 undef_id_tok '=' const_id_tok ';'
Terminals, with rules where they appear
$ (-1)
$ (0)
';' (59) 5
'=' (61) 5
error (256)