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

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