[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: examples: style changes in lexcalc
From: |
Akim Demaille |
Subject: |
Re: examples: style changes in lexcalc |
Date: |
Sat, 16 Feb 2019 16:51:56 +0100 |
> Le 16 févr. 2019 à 15:57, Akim Demaille <address@hidden> a écrit :
>
> commit a29ea1e5a431957024c3d6a889b556e8a8897434
> Author: Akim Demaille <address@hidden>
> Date: Sat Feb 16 13:18:31 2019 +0100
>
> examples: style changes in lexcalc
>
> * examples/c/lexcalc/parse.y: Formatting/comment changes.
Scratch that, there were more changes needed.
commit 0adda755a2f6703c561cc7d99faf69fe0c427509
Author: Akim Demaille <address@hidden>
Date: Sat Feb 16 13:18:31 2019 +0100
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.
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 <<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'
diff --git a/examples/c/lexcalc/local.mk b/examples/c/lexcalc/local.mk
index 85cdb077..74f60109 100644
--- a/examples/c/lexcalc/local.mk
+++ b/examples/c/lexcalc/local.mk
@@ -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%
diff --git a/examples/c/lexcalc/parse.y b/examples/c/lexcalc/parse.y
index f2f29df1..68b1b175 100644
--- a/examples/c/lexcalc/parse.y
+++ b/examples/c/lexcalc/parse.y
@@ -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)