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:
Akim Demaille
2019-02-16 13:18:31 +01:00
parent 0782ed3274
commit 0adda755a2
3 changed files with 17 additions and 12 deletions

View File

@@ -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'

View File

@@ -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%

View File

@@ -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)