bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

d: improve the example


From: Akim Demaille
Subject: d: improve the example
Date: Tue, 26 Feb 2019 18:26:49 +0100

commit 575b81411907191ce13a6f78a4b23b78263625df
Author: Akim Demaille <address@hidden>
Date:   Sat Feb 23 11:35:26 2019 +0100

    d: improve the example
    
    * examples/d/calc.y: Exit with failure on errors.
    Remove useless operators (=, !) meant for the test suite.
    Add unary + for symmetry.
    * examples/d/calc.test: Adjust expectations.

diff --git a/examples/d/calc.test b/examples/d/calc.test
index e2bfceeb..30736097 100644
--- a/examples/d/calc.test
+++ b/examples/d/calc.test
@@ -23,4 +23,4 @@ run 0 7
 cat >input <<EOF
 1 + 2 * * 3
 EOF
-run 0 "err: syntax error, unexpected *, expecting - or ! or ( or number"
+run 1 "err: syntax error, unexpected *, expecting + or - or ( or number"
diff --git a/examples/d/calc.y b/examples/d/calc.y
index 5cdbf9b5..26b4413a 100644
--- a/examples/d/calc.y
+++ b/examples/d/calc.y
@@ -18,17 +18,15 @@
        MINUS  "-"
        STAR   "*"
        SLASH  "/"
-       BANG   "!"
        LPAR   "("
        RPAR   ")"
        EOL    "end of line"
 %token <ival> NUM "number"
 %type  <ival> exp
 
-%nonassoc "="       /* comparison            */
 %left "-" "+"
 %left "*" "/"
-%precedence NEG     /* negation--unary minus */
+%precedence UNARY   /* unary operators */
 
 /* Grammar follows */
 %%
@@ -44,29 +42,21 @@ line:
 ;
 
 exp:
-  NUM                { $$ = $1; }
-| exp "=" exp
-  {
-    if ($1 != $3)
-      yyerror (format ("calc: error: %d != %d", $1, $3));
-  }
-| exp "+" exp        { $$ = $1 + $3; }
-| exp "-" exp        { $$ = $1 - $3; }
-| exp "*" exp        { $$ = $1 * $3; }
-| exp "/" exp        { $$ = $1 / $3; }
-| "-" exp  %prec NEG { $$ = -$2; }
-| "(" exp ")"        { $$ = $2; }
-| "(" error ")"      { $$ = 1111; }
-| "!"                { $$ = 0; return YYERROR; }
-| "-" error          { $$ = 0; return YYERROR; }
+  NUM                  { $$ = $1; }
+| exp "+" exp          { $$ = $1 + $3; }
+| exp "-" exp          { $$ = $1 - $3; }
+| exp "*" exp          { $$ = $1 * $3; }
+| exp "/" exp          { $$ = $1 / $3; }
+| "+" exp  %prec UNARY { $$ = -$2; }
+| "-" exp  %prec UNARY { $$ = -$2; }
+| "(" exp ")"          { $$ = $2; }
 ;
 
-
 %%
 class CalcLexer : Lexer {
 
-  this ()
-    {}
+  // Should be a local in main, shared with %parse-param.
+  int exit_status = 0;
 
   int
   get_char ()
@@ -84,6 +74,7 @@ class CalcLexer : Lexer {
 
   public void yyerror (string s)
   {
+    exit_status = 1;
     stderr.writeln (s);
   }
 
@@ -141,7 +132,6 @@ class CalcLexer : Lexer {
       case '-': return YYTokenType.MINUS;
       case '*': return YYTokenType.STAR;
       case '/': return YYTokenType.SLASH;
-      case '!': return YYTokenType.BANG;
       case '(': return YYTokenType.LPAR;
       case ')': return YYTokenType.RPAR;
       case '\n': return YYTokenType.EOL;
@@ -150,9 +140,10 @@ class CalcLexer : Lexer {
   }
 }
 
-void main ()
+int main ()
 {
   CalcLexer l = new CalcLexer ();
   Calc p = new Calc (l);
   p.parse ();
+  return l.exit_status;
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]