bison-patches
[Top][All Lists]
Advanced

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

Re: FYI: default %printer/%destructor


From: Hans Aberg
Subject: Re: FYI: default %printer/%destructor
Date: Thu, 23 Nov 2006 13:59:41 +0100

On 23 Nov 2006, at 09:47, Paul Eggert wrote:

Maybe it's just me, but I prefer Hans' suggestion:

  exp/sum: exp/term1 '+' exp/term2

over

  exp#sum: exp#term1 '+' exp#term2

I can't think of any semantic reason to prefer one over the other. The
slash is just a little easier on my eyes.

I think "/" bugs me because it means "or" in ABNF, which is the
standard grammatical notation used in Internet RFCs; see
<http://www.ietf.org/rfc/rfc4234>.  I could live with "/",
I suppose.

Here are the two variations, for comparisons:

  expression:
      INTEGRAL_NUMBER/x                 { $$ = $x; }
    | '-' expression/x %prec NEGATION   { $$ = -$x; }
    | '(' expression/x ')'              { $$ = $x; }
    | expression/x '+' expression/y     { $$ = $x + $y; }
    | expression/x '-' expression/y     { $$ = $x - $y; }
    | expression/x '*' expression/y     { $$ = $x * $y; }
    | expression/x '/' expression/y     { $$ = $x / $y; }
    | expression/x '^' expression/y     { $$ = pow($x, $y); }
  ;

  expression:
      INTEGRAL_NUMBER#x                 { $$ = $x; }
    | '-' expression#x %prec NEGATION   { $$ = -$x; }
    | '(' expression#x ')'              { $$ = $x; }
    | expression#x '+' expression#y     { $$ = $x + $y; }
    | expression#x '-' expression#y     { $$ = $x - $y; }
    | expression#x '*' expression#y     { $$ = $x * $y; }
    | expression#x '/' expression#y     { $$ = $x / $y; }
    | expression#x '^' expression#y     { $$ = pow($x, $y); }
  ;

One advantage of the first one, using "/", is that frees "#" for indicating actions, if it should be cobined with my other proposal. Then it will look like:

  expression:
      INTEGRAL_NUMBER/x                 #identity
    | '-' expression/x %prec NEGATION   #neg
    | '(' expression/x ')'              #identity
    | expression/x '+' expression/y     #add
    | expression/x '-' expression/y     #sub
    | expression/x '*' expression/y     #mul
    | expression/x '/' expression/y     #div
    | expression/x '^' expression/y     #pow
  ;

identity { $$ = $x; }
neg { $$ = -$x; }
add { $$ = $x + $y; }
sub { $$ = $x - $y; }
mul { $$ = $x * $y; }
div { $$ = $x / $y; }
pow { $$ = pow($x, $y); }

I think this last variation makes the grammar standing out quite clearly.

One problem though is that the variable definitions end up at different places. Ten one might combine the different ideas to arrive at:

  expression:
      INTEGRAL_NUMBER/x                 #identity(x)
    | '-' expression/x %prec NEGATION   #neg(x)
    | '(' expression/x ')'              #identity(x)
    | expression/x '+' expression/y     #add(x, y)
    | expression/x '-' expression/y     #sub(x, y)
    | expression/x '*' expression/y     #mul(x, y)
    | expression/x '/' expression/y     #div(x, y)
    | expression/x '^' expression/y     #pow(x, y)
  ;

identity(x) { $$ = $x; }
neg(x) { $$ = -$x; }
add(x, y) { $$ = $x + $y; }
sub(x, y) { $$ = $x - $y; }
mul(x, y) { $$ = $x * $y; }
div(x, y) { $$ = $x / $y; }
pow(x, y) { $$ = pow($x, $y); }

But I am not sure it adds something. - I put in this example, to see where things are heading. Apparently, the actions act as implicit functions, and the grammar variables insert values into those functions.

  Hans Aberg






reply via email to

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