bison-patches
[Top][All Lists]
Advanced

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

Re: FYI: default %printer/%destructor


From: Paul Eggert
Subject: Re: FYI: default %printer/%destructor
Date: Wed, 22 Nov 2006 11:28:20 -0800
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.4 (gnu/linux)

"Joel E. Denny" <address@hidden> writes:

> When optionality and repetitions are 
> possible, do named semantic values even make sense?

Sure.  You can give names to the optional or repeated parts.  The
types associated with these parts are the equivalent of the ML "'a
option" and "'a list" parameterized types.

> Should we really sacrifice the quality of our current notation to
> accommodate a feature that may never happen and that may not make
> sense anyway?

No, but if the quality of our notation isn't much affected either way,
we might as well do something that is compatible.

> At least () for named semantic values has some precedence in Lemon.
>
> With all that in mind, I say we stick with parentheses:
>
>   a(name1): b c() d(@name2)
>
> For a, the value and location are $name1 and @name1.
> For b, they are $b and @b.  
> For c and d, the values are declared unused.
> For d, the location is @name2.

I like the rule for 'b' -- that's simple.  The rest works, but I'd
like something simpler if possible.

First, I forget: why is it important that values can be declared
"unused"?  That's extra complexity -- is it really worth it?  After
all, Bison and the human reader should be able to determine easily
whether a value is used by reading the corresponding action.  If the
action is so complicated that this is difficult for the human reader,
the action (or grammar) should probably be rewritten anyway.

I do see the need for declaring a different name for the value and
location than the nonterminal's name, since one can have two different
instances of the same nonterminal.  But parentheses are overkill for
that, since we don't need brackets just to declare a different name.

I sort-of liked ".", but it isn't really component selection, and
anyway that collides with existing practice, which allows "." in
identifiers (POSIX requires support for this), so that's out.  Of the
other characters unused by ISO EBNF, ":", "%", and "_" are also
reserved by POSIX.  "$", "\", and "<" have special meanings in Solaris
10 /usr/ccs/bin/yacc (for example, "$" is a valid character in
identifiers), so it's better not to use them in case we ever want to
support those special meanings compatibly.  That leaves "!+@&#>/^`~".

How about this notation, which is in something of a different and
more-restricted style?

  a#1: a#2 b c d;

The '#1' and '#2' serve to disambiguate the 'a's.  In the action, one
writes $a#1 to get the value for the first a, and $a#2 for the second.
One writes @a#1 and @a#2 for locations.  One writes $b and @b for b's
action and location, and similarly for c and d.

For example, instead of this:

  exp(result):
      NUM
        { $result = $NUM; }
    | '-' exp(subtrahend) %prec NEG
        { $result = - $subtrahend; }
    | '(' exp(subexpression) ')'
        { $result = $subexpression; }
    | exp(augend) '+' exp(addend)
        { $result = $augend + $addend; }
    | exp(minuend) '-' exp(subtrahend)      
        { $result = $minuend - $subtrahend; }
    | exp(multiplicand) '*' exp(multiplier)
        { $result = $multiplicand * $multiplier; }
    | exp(numerator) '/' exp(denominator)       
        { $result = $numerator / $denominator; }
    | exp(base) '^' exp(exponent)
        { $result = pow ($base, $exponent); }

you write this:

  exp:
      NUM               { $$ = $NUM; }
    | '-' exp %prec NEG { $$ = - $exp; }
    | '(' exp ')'       { $$ = $exp; }
    | exp#1 '+' exp#2   { $$ = $exp#1 + $exp#2; }
    | exp#1 '-' exp#2   { $$ = $exp#1 - $exp#2; }
    | exp#1 '*' exp#2   { $$ = $exp#1 * $exp#2; }
    | exp#1 '/' exp#2   { $$ = $exp#1 / $exp#2; }
    | exp#1 '^' exp#2   { $$ = pow ($exp#1, $exp#2); }

It's not entirely a fair comparison, but I hope you see the idea.
Often the alternative names are weird or arbitrary.  The English word
"augend" hasn't been important since we stopped using Roman numerals
to do arithmetic, and I just now fixed
<http://en.wikipedia.org/wiki/Multiplication> because it misstated the
distinction between "multiplier" and "multiplicand".  In cases like
these, "exp#1" is just as good.

If it's important to say a value can be unused, that could be written
"a#-", or something like that.




reply via email to

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