mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-09 04:13:03 +00:00
examples: fixes in lexcalc
* examples/c/lexcalc/parse.y: Formatting/comment changes. (line): Don't return a value. Print the result here, which avoids printing a value for lines with an error. (yyerror): Be sure to increment the pointed, not the pointer... * examples/c/lexcalc/lexcalc.test: Check errors. * examples/c/lexcalc/local.mk: Fix a dependency.
This commit is contained in:
@@ -25,3 +25,8 @@ cat >input <<EOF
|
||||
EOF
|
||||
run 0 9
|
||||
run -noerr 0 9 -p
|
||||
|
||||
cat >input <<EOF
|
||||
(1+2) *
|
||||
EOF
|
||||
run 1 'err: syntax error, unexpected end-of-line, expecting ( or number'
|
||||
|
||||
@@ -23,7 +23,7 @@ check_PROGRAMS += %D%/lexcalc
|
||||
TESTS += %D%/lexcalc.test
|
||||
EXTRA_DIST += %D%/lexcalc.test
|
||||
%C%_lexcalc_SOURCES = %D%/parse.y %D%/parse.h %D%/scan.l
|
||||
%D%/lexcalc.c: $(dependencies)
|
||||
%D%/parse.c: $(dependencies)
|
||||
|
||||
# Don't use gnulib's system headers.
|
||||
%C%_lexcalc_CPPFLAGS = -I$(top_srcdir)/%D% -I$(top_builddir)/%D%
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
// Emitted on top of the implementation file.
|
||||
%code top
|
||||
{
|
||||
#include <stdio.h> /* printf. */
|
||||
#include <stdlib.h> /* getenv. */
|
||||
#include <stdio.h> // printf.
|
||||
#include <stdlib.h> // getenv.
|
||||
}
|
||||
|
||||
%define api.pure full
|
||||
@@ -39,7 +39,7 @@
|
||||
;
|
||||
|
||||
%token <int> NUM "number"
|
||||
%type <int> exp line
|
||||
%type <int> exp
|
||||
%printer { fprintf (yyo, "%d", $$); } <int>
|
||||
|
||||
// Precedence (from lowest to highest) and associativity.
|
||||
@@ -50,18 +50,18 @@
|
||||
// Rules.
|
||||
input:
|
||||
%empty
|
||||
| input line { printf ("%d\n", $line); }
|
||||
| input line
|
||||
;
|
||||
|
||||
line:
|
||||
exp EOL { $$ = $1; }
|
||||
exp EOL { printf ("%d\n", $exp); }
|
||||
| error EOL { yyerrok; }
|
||||
;
|
||||
|
||||
exp:
|
||||
exp "+" exp { $$ = $1 + $3; }
|
||||
| exp "-" exp { $$ = $1 - $3; }
|
||||
| exp "*" exp { $$ = $1 * $3; }
|
||||
exp "+" exp { $$ = $1 + $3; }
|
||||
| exp "-" exp { $$ = $1 - $3; }
|
||||
| exp "*" exp { $$ = $1 * $3; }
|
||||
| exp "/" exp
|
||||
{
|
||||
if ($3 == 0)
|
||||
@@ -72,15 +72,15 @@ exp:
|
||||
else
|
||||
$$ = $1 / $3;
|
||||
}
|
||||
| "(" exp ")" { $$ = $2; }
|
||||
| "(" exp ")" { $$ = $2; }
|
||||
| NUM { $$ = $1; }
|
||||
;
|
||||
%%
|
||||
// Epilogue (C code).
|
||||
void yyerror(int *nerrs, const char *msg)
|
||||
void yyerror (int *nerrs, const char *msg)
|
||||
{
|
||||
fprintf (stderr, "%s\n", msg);
|
||||
++nerrs;
|
||||
++*nerrs;
|
||||
}
|
||||
|
||||
int main (void)
|
||||
|
||||
Reference in New Issue
Block a user