Re: Precedence of exponentiation

From: David Frauzel (nemo_at_weathersong.net)
Date: 09/28/04


Date: Tue, 28 Sep 2004 00:37:44 GMT

Abigail <abigail@abigail.nl> wrote in
news:slrnclh90j.hv.abigail@alexandra.abigail.nl:

> Which to me suggest that you think Perl should change on how it parses
> things. I haven't seen any poster in this thread agreeing with that.
> I agree that it would have been better if Perl parsed -a ** b as
> (-a) ** b, but I strongly disagree Perl should change on how it parses
> expressions.

I wrapped it up in <rant> to suggest I wasn't being entirely serious. If
anything I've written gives the appearance of advocating Perl5 reform, it
was not my intention. The parser I'm writing isn't even written in Perl
(nor does it use yacc), and that's my primary reason for this topic.

> Perl6 is a different language, which won't happen any time soon. This
> newsgroup discusses real things, not vaporware.

Yikes, sore spot I guess. Won't bring it up again. :p

> Oh, it has happened. Most of the time it was not intentional. People
> go to great lengths to make sure any program that was written for a
> previous version of Perl will still work with a new version.
> Incompatible changes would need a very good justification. And I do
> believe that a dramatic change the way you like to see hasn't happened
> before - at least not since 5.000.

And I recall programming Perl before 5.000. :D

> What preprocessor are you talking about? Perl doesn't have any. And
> don't get confused by the fact that the deparser doesn't emit
> identical code. It deparses 'q{foo}' as 'q[foo]'. Does that mean the
> '{}' is useless?

Oops, I didn't mean to use the word preprocessor. (I'm cross-referencing
my own parser and perly.y / perly.c, and I'm making quite a few
multitasker typos.) I mean the optimization made throughout each compile
pass. (I know, not the same thing as a preprocessor.)

What I meant by "useless" is that "foo" (double-quotes around 'foo') is
not as efficient as 'foo' (single quotes). The compiler's optimization
sequence converts the double-quotes into single-quotes, because there's
nothing to interpolate. I know you know this, and I already said I was
being nit-picky. ;p

> Grammar:
>
> Expression: Term | '-' Expression
> Term: Digits | Term '**' Expression
> Digits: Digit | Digit Digits
> Digit: '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' |
> '9'

And:

"termbinop : term POWOP term",

No, I don't see any special rules. So, I don't think it's Perl's parser I
misunderstand, it's yacc. My idea of a parser with both precedence and
associativity is not the same as what yacc is doing. In the interest of
building a better parser, I'll do some reading on yacc sometime.

> which to me indicates you are argueing that 'a ** -b' should be
> identical to '-(a ** b)' by extracting the unary minus from the middle
> of the expression to the front.
>
> So, once again I ask, do you know any language that does so? Your
> Smalltalk example doesn't do this.

Mine does. It builds a parse tree by starting at the root, and comparing
precedence, until it finds an available node, or detects that a token
somewhere down from the root has less precedence than the current token
in the parser. It's loosely based on a tree sort. The core of the parser
is about 75 lines of Pascal (Delphi). It works identically to Perl with
everything except the exponentiation operator (bug). So I wrote the
lexer, too (a couple hundred lines).



Relevant Pages

  • Re: Precedence of exponentiation
    ... > compiler compilers work. ... And I do mean to brush up on yacc one of these days. ... I was looking at it when I was first drawing up a blueprint for my parser, ... based on Perl. ...
    (comp.lang.perl.misc)
  • Re: prefix for yacc/bison
    ... The grammars that yacc accepts are a subset ... the parser mechanisms to their hearts content. ... means a bounded lookahead bottom-up machine. ...
    (alt.os.linux)
  • Re: Flex&Bison: how to write a parser for this grammar?
    ... bison? ... it's better to rely on a parser generator than on hand-written ... a yacc parser will be simpler if the grammar is suitable so ... need for flex start conditions there as I feared there might be. ...
    (comp.programming)
  • Csup cvsmode build discussion
    ... lex and yacc files that I would like to discuss. ... I implemented the RCS parser required to operate on RCS files ... I've been compiling the parser and tokenizer with the help of bison ... Now, the base system already have flex, but the flex version in base is heavily ...
    (freebsd-hackers)
  • Re: a good TeX parser for use by software that needs to read TeX?
    ... : read TeX? ... In particular, has anyone used the perl Text::TeX parser, ... In addition, push $found into a list @commands, ...
    (comp.text.tex)