mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-21 18:23: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
|
EOF
|
||||||
run 0 9
|
run 0 9
|
||||||
run -noerr 0 9 -p
|
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
|
TESTS += %D%/lexcalc.test
|
||||||
EXTRA_DIST += %D%/lexcalc.test
|
EXTRA_DIST += %D%/lexcalc.test
|
||||||
%C%_lexcalc_SOURCES = %D%/parse.y %D%/parse.h %D%/scan.l
|
%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.
|
# Don't use gnulib's system headers.
|
||||||
%C%_lexcalc_CPPFLAGS = -I$(top_srcdir)/%D% -I$(top_builddir)/%D%
|
%C%_lexcalc_CPPFLAGS = -I$(top_srcdir)/%D% -I$(top_builddir)/%D%
|
||||||
|
|||||||
@@ -15,8 +15,8 @@
|
|||||||
// Emitted on top of the implementation file.
|
// Emitted on top of the implementation file.
|
||||||
%code top
|
%code top
|
||||||
{
|
{
|
||||||
#include <stdio.h> /* printf. */
|
#include <stdio.h> // printf.
|
||||||
#include <stdlib.h> /* getenv. */
|
#include <stdlib.h> // getenv.
|
||||||
}
|
}
|
||||||
|
|
||||||
%define api.pure full
|
%define api.pure full
|
||||||
@@ -39,7 +39,7 @@
|
|||||||
;
|
;
|
||||||
|
|
||||||
%token <int> NUM "number"
|
%token <int> NUM "number"
|
||||||
%type <int> exp line
|
%type <int> exp
|
||||||
%printer { fprintf (yyo, "%d", $$); } <int>
|
%printer { fprintf (yyo, "%d", $$); } <int>
|
||||||
|
|
||||||
// Precedence (from lowest to highest) and associativity.
|
// Precedence (from lowest to highest) and associativity.
|
||||||
@@ -50,18 +50,18 @@
|
|||||||
// Rules.
|
// Rules.
|
||||||
input:
|
input:
|
||||||
%empty
|
%empty
|
||||||
| input line { printf ("%d\n", $line); }
|
| input line
|
||||||
;
|
;
|
||||||
|
|
||||||
line:
|
line:
|
||||||
exp EOL { $$ = $1; }
|
exp EOL { printf ("%d\n", $exp); }
|
||||||
| error EOL { yyerrok; }
|
| error EOL { yyerrok; }
|
||||||
;
|
;
|
||||||
|
|
||||||
exp:
|
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
|
| exp "/" exp
|
||||||
{
|
{
|
||||||
if ($3 == 0)
|
if ($3 == 0)
|
||||||
@@ -72,15 +72,15 @@ exp:
|
|||||||
else
|
else
|
||||||
$$ = $1 / $3;
|
$$ = $1 / $3;
|
||||||
}
|
}
|
||||||
| "(" exp ")" { $$ = $2; }
|
| "(" exp ")" { $$ = $2; }
|
||||||
| NUM { $$ = $1; }
|
| NUM { $$ = $1; }
|
||||||
;
|
;
|
||||||
%%
|
%%
|
||||||
// Epilogue (C code).
|
// Epilogue (C code).
|
||||||
void yyerror(int *nerrs, const char *msg)
|
void yyerror (int *nerrs, const char *msg)
|
||||||
{
|
{
|
||||||
fprintf (stderr, "%s\n", msg);
|
fprintf (stderr, "%s\n", msg);
|
||||||
++nerrs;
|
++*nerrs;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main (void)
|
int main (void)
|
||||||
|
|||||||
Reference in New Issue
Block a user