From 0adda755a2f6703c561cc7d99faf69fe0c427509 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sat, 16 Feb 2019 13:18:31 +0100 Subject: [PATCH] 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. --- examples/c/lexcalc/lexcalc.test | 5 +++++ examples/c/lexcalc/local.mk | 2 +- examples/c/lexcalc/parse.y | 22 +++++++++++----------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/examples/c/lexcalc/lexcalc.test b/examples/c/lexcalc/lexcalc.test index 552508e2..6d1ecdeb 100644 --- a/examples/c/lexcalc/lexcalc.test +++ b/examples/c/lexcalc/lexcalc.test @@ -25,3 +25,8 @@ cat >input <input < /* printf. */ -#include /* getenv. */ +#include // printf. +#include // getenv. } %define api.pure full @@ -39,7 +39,7 @@ ; %token NUM "number" -%type exp line +%type exp %printer { fprintf (yyo, "%d", $$); } // 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)