Mailing List Archive

NASL2: precedence of 'logical NOT'
First I forgot to put '!' in the list of %prec instruction.
Then I added it, but probably in the wrong place.
_Currently_, things like ! x == y are parsed as (!x) == y, which is
wrong IMHO. It should be ! (x == y)

Worse "! x >< y" is parsed as "(!x) >< y" which is absurd.

So, the priority of NOT should be set _below_ the priority of the
compare operators.
Where exactly? Below AND and OR? Or not?
i.e. "! x && y" = "! (x && y)" or "(!x) && y" ?
I vote for the 2nd interpretation.
Re: NASL2: precedence of 'logical NOT' [ In reply to ]
I vote for the last interpretation. It seems to be the most explicit
and clear, and probably will lead to the least confusion when trying to
figure out larger expressions. I can see where either the current or
the first choice below could lead to some expressions that are very hard
to figure out. Myself, I'll just stick with lots of parenthesis, as
that always seems the safe way.

Joe


On Sun, 2003-02-16 at 14:27, Michel Arboi wrote:
> First I forgot to put '!' in the list of %prec instruction.
> Then I added it, but probably in the wrong place.
> _Currently_, things like ! x == y are parsed as (!x) == y, which is
> wrong IMHO. It should be ! (x == y)
>
> Worse "! x >< y" is parsed as "(!x) >< y" which is absurd.
>
> So, the priority of NOT should be set _below_ the priority of the
> compare operators.
> Where exactly? Below AND and OR? Or not?
> i.e. "! x && y" = "! (x && y)" or "(!x) && y" ?
> I vote for the 2nd interpretation.
>
RE: NASL2: precedence of 'logical NOT' [ In reply to ]
Isn't it true that most programming languages give unary operators very high
precedence? I have always found this easy on the brain since it translates
into unary operators always binding to tokens that immediately follow.
Won't most people with at least some programming experience find what you
are suggesting to be unfamiliar?

Jim

> -----Original Message-----
> From: owner-nessus-devel@list.nessus.org
> [mailto:owner-nessus-devel@list.nessus.org]On Behalf Of Michel Arboi
> Sent: Sunday, February 16, 2003 3:28 PM
> To: Nessus Devel
> Subject: NASL2: precedence of 'logical NOT'
>
>
> First I forgot to put '!' in the list of %prec instruction.
> Then I added it, but probably in the wrong place.
> _Currently_, things like ! x == y are parsed as (!x) == y, which is
> wrong IMHO. It should be ! (x == y)
>
> Worse "! x >< y" is parsed as "(!x) >< y" which is absurd.
>
> So, the priority of NOT should be set _below_ the priority of the
> compare operators.
> Where exactly? Below AND and OR? Or not?
> i.e. "! x && y" = "! (x && y)" or "(!x) && y" ?
> I vote for the 2nd interpretation.
>
>
RE: NASL2: precedence of 'logical NOT' [ In reply to ]
I just checked the operator precedence for Perl, TCL, PHP, Java and C/C++.
They all put unary operators at the highest levels of precedence and as well
place "comparators" at the lowest levels of precedence. I may be giving bad
examples of languages. Can you name other popular languages that have the
kind of precedence relationship between unary operators like '!' and
comparator operators like '==' ? I really think you are setting people up
to be confused and having to code against their intuition.

Just my two cents!

Jim

> -----Original Message-----
> From: owner-nessus-devel@list.nessus.org
> [mailto:owner-nessus-devel@list.nessus.org]On Behalf Of Michel Arboi
> Sent: Sunday, February 16, 2003 4:20 PM
> To: Jim Cervantes
> Cc: Nessus Devel
> Subject: RE: NASL2: precedence of 'logical NOT'
>
>
> On Sun, 2003-02-16 at 10:12, Jim Cervantes wrote:
> > Isn't it true that most programming languages give unary
> operators very high
> > precedence?
>
> Well, that's what C does. But I'm not sure it is a good example (e.g.
> the precedence of & and | is *bad*)
>
> > I have always found this easy on the brain since it translates
> > into unary operators always binding to tokens that immediately follow.
> > Won't most people with at least some programming experience
> find what you
> > are suggesting to be unfamiliar?
>
> Then there is another possibility: implement additional logical
> operators (and, or, not) with very low precedence, like in Perl.
>
> ! x == y <=> (! x) == y
> not x == y <=> ! (x == y)
>
> This means that ! x >< y is buggy code.
> egrep '! *[_a-zA-z0-9-]+.*><' *.nasl does not return any suspicious
> code in the current plugins.
>
> And it seems that nobody had the bad idea of calling a variable 'and'
> 'or' or 'not'. Good.
>
>
Re: NASL2: precedence of 'logical NOT' [ In reply to ]
"Jim Cervantes" <jim.cervantes@verizon.net> writes:

> I really think you are setting people up to be confused and having
> to code against their intuition.

OK, for the moment, nothing is broken because the NASL1 scripts are
full of parenthesis but we have to chose something before NASL2 is
released as "stable".

1. Change ! so that its precedence is similar to && and ||
(counter-intuitive, different from C and Perl, but rather logical)
2. Leave ! as it is and hope that the script writers will put
parenthesis where needed
3. Leave ! as it is and add a low priority "not" operator

After all, that may be a good compromise
Re: NASL2: precedence of 'logical NOT' [ In reply to ]
Don't go changing the precedence from the standard ones used
in other programming languages. Standardize on A choice
(be it C, Java, Perl, whatever), and use the same one. It's
alot easier to document and just say "we use language Z's
standard". Sometimes the right choice is not necessarily
the best one (else vi wouldn't live on so long ;-))

Thomas

Michel Arboi wrote:
> "Jim Cervantes" <jim.cervantes@verizon.net> writes:
>
>
>>I really think you are setting people up to be confused and having
>>to code against their intuition.
>
>
> OK, for the moment, nothing is broken because the NASL1 scripts are
> full of parenthesis but we have to chose something before NASL2 is
> released as "stable".
>
> 1. Change ! so that its precedence is similar to && and ||
> (counter-intuitive, different from C and Perl, but rather logical)
> 2. Leave ! as it is and hope that the script writers will put
> parenthesis where needed
> 3. Leave ! as it is and add a low priority "not" operator
>
> After all, that may be a good compromise
>
RE: NASL2: precedence of 'logical NOT' [ In reply to ]
I strongly agree with the idea of matching another popular language's
precedence (and associativity for that matter). Why do we need another
language's precedence rules?

> -----Original Message-----
> From: owner-nessus-devel@list.nessus.org
> [mailto:owner-nessus-devel@list.nessus.org]On Behalf Of Thomas Reinke
> Sent: Sunday, February 16, 2003 12:02 PM
> To: Nessus Devel
> Subject: Re: NASL2: precedence of 'logical NOT'
>
>
> Don't go changing the precedence from the standard ones used
> in other programming languages. Standardize on A choice
> (be it C, Java, Perl, whatever), and use the same one. It's
> alot easier to document and just say "we use language Z's
> standard". Sometimes the right choice is not necessarily
> the best one (else vi wouldn't live on so long ;-))
>
> Thomas
>
> Michel Arboi wrote:
> > "Jim Cervantes" <jim.cervantes@verizon.net> writes:
> >
> >
> >>I really think you are setting people up to be confused and having
> >>to code against their intuition.
> >
> >
> > OK, for the moment, nothing is broken because the NASL1 scripts are
> > full of parenthesis but we have to chose something before NASL2 is
> > released as "stable".
> >
> > 1. Change ! so that its precedence is similar to && and ||
> > (counter-intuitive, different from C and Perl, but rather logical)
> > 2. Leave ! as it is and hope that the script writers will put
> > parenthesis where needed
> > 3. Leave ! as it is and add a low priority "not" operator
> >
> > After all, that may be a good compromise
> >
>
>
>
RE: NASL2: precedence of 'logical NOT' [ In reply to ]
C sucks in some ways that you have articulated pretty well. That said, it
is familiar - I know when to use parentheses, either because C is stupid or
my poor brain can't be trusted. And when things go wrong, I know where to
look. So, I would rather stick with the familiar.

Let's face it, whatever set of rules you choose, the language will work.
This is really a practical matter of what will make people most productive.
Isn't that what scripting languages are all about - an easy and forgiving
learning curve? Anyway, I really do appreciate that NASL2 has emerged with
a full-fledged grammar, no matter what the precedence rules! Thanks!

Jim




> -----Original Message-----
> From: owner-nessus-devel@list.nessus.org
> [mailto:owner-nessus-devel@list.nessus.org]On Behalf Of Michel Arboi
> Sent: Sunday, February 16, 2003 6:32 PM
> To: Jim Cervantes
> Cc: Nessus Devel
> Subject: RE: NASL2: precedence of 'logical NOT'
>
>
> On Sun, 2003-02-16 at 12:20, Jim Cervantes wrote:
> > I strongly agree with the idea of matching another popular language's
> > precedence (and associativity for that matter). Why do we need another
> > language's precedence rules?
>
> Because most of the time, C or Perl try to be a little too smart for my
> tired brain. And I cannot trust them fully:
> * I have _never_ seen an example of C or Perl code where "x & y ==
> z" should really behave as it is implemented.
> * I cannot see why "x == y == z" or " x < y < z" is authorized in C
> or Perl. If somebody needs it, it should be implemented the way it
> is in mathematics (i.e. "x < y and y < z" and not some crazy
> integer to boolean compare)
> * And I am just realizing that we should forbid things like "!!x" or
> "~~ x" because they are useless now and may clash with some future
> operator (Sorry Renaud! Implementing "!!" is easier than the fish
> operator ">(°>" :)
>
> Anyway I agree that one expects a "compact" operator (I mean, with a
> very short name) like "!" to be strongly tied to the next variable.
> That's more a problem of readability than logic.
>
>
>
RE: NASL2: precedence of 'logical NOT' [ In reply to ]
On Sun, 2003-02-16 at 10:12, Jim Cervantes wrote:
> Isn't it true that most programming languages give unary operators very high
> precedence?

Well, that's what C does. But I'm not sure it is a good example (e.g.
the precedence of & and | is *bad*)

> I have always found this easy on the brain since it translates
> into unary operators always binding to tokens that immediately follow.
> Won't most people with at least some programming experience find what you
> are suggesting to be unfamiliar?

Then there is another possibility: implement additional logical
operators (and, or, not) with very low precedence, like in Perl.

! x == y <=> (! x) == y
not x == y <=> ! (x == y)

This means that ! x >< y is buggy code.
egrep '! *[_a-zA-z0-9-]+.*><' *.nasl does not return any suspicious
code in the current plugins.

And it seems that nobody had the bad idea of calling a variable 'and'
'or' or 'not'. Good.
Re: NASL2: precedence of 'logical NOT' [ In reply to ]
On Sun, 2003-02-16 at 12:02, Thomas Reinke wrote:
> Don't go changing the precedence from the standard ones used
> in other programming languages. Standardize on A choice
> (be it C, Java, Perl, whatever), and use the same one. It's
> alot easier to document and just say "we use language Z's
> standard".

Too late, I put a table with the operator precedences in my NASL2
reference manual :-]

I already changed at least two things if we compare with C:
1. All "compare" operators have the same precedence, but you cannot
associate them, so it does not really matter (i.e. "x <= y == w <= z" is
lawful in C, but not in NASL2)
2. The bitwise and/or operators have a higher precedence than in C. In
NASL2, "x & y == z" is "(x & y) == z"; in C, it is "x & (y == z)"
(that's why you always have to use parenthesis in C for this. Silly,
no?)

Anyway, it seems that there are more vote for "keep ! as in C" than the
contrary. Renaud, any comment?
RE: NASL2: precedence of 'logical NOT' [ In reply to ]
On Sun, 2003-02-16 at 12:20, Jim Cervantes wrote:
> I strongly agree with the idea of matching another popular language's
> precedence (and associativity for that matter). Why do we need another
> language's precedence rules?

Because most of the time, C or Perl try to be a little too smart for my
tired brain. And I cannot trust them fully:
* I have _never_ seen an example of C or Perl code where "x & y ==
z" should really behave as it is implemented.
* I cannot see why "x == y == z" or " x < y < z" is authorized in C
or Perl. If somebody needs it, it should be implemented the way it
is in mathematics (i.e. "x < y and y < z" and not some crazy
integer to boolean compare)
* And I am just realizing that we should forbid things like "!!x" or
"~~ x" because they are useless now and may clash with some future
operator (Sorry Renaud! Implementing "!!" is easier than the fish
operator ">(°>" :)

Anyway I agree that one expects a "compact" operator (I mean, with a
very short name) like "!" to be strongly tied to the next variable.
That's more a problem of readability than logic.
Re: NASL2: precedence of 'logical NOT' [ In reply to ]
On 16 Feb 2003, Michel Arboi wrote:

> _Currently_, things like ! x == y are parsed as (!x) == y, which is
> wrong IMHO. It should be ! (x == y)
> Worse "! x >< y" is parsed as "(!x) >< y" which is absurd.
> So, the priority of NOT should be set _below_ the priority of the
> compare operators.

This makes some sense because ! is supposed to negate a logical formula
like x == y, not a term like x, y (in the first-order logic). I bet you
can find some textbooks where "x is not equal to y" is written as !x==y
(of course, they use a "hook" instead of !, and = instead of ==).

On the other hand, I am sure you can find even more textbooks where the
same things is always written as "!(x==y)". I doubt the authors of the
latter books decided to write it that way under the influence of any
programming language...

Personally, I find a syntax with an unary operator (esp. a short one)
having lower precedence than a binary operator highly confusing.

> Where exactly? Below AND and OR? Or not?
> i.e. "! x && y" = "! (x && y)" or "(!x) && y" ?
> I vote for the 2nd interpretation.

The logical negation has always had higher priority than binary logical
connectives.


On 16 Feb 2003, Michel Arboi wrote:

> * I cannot see why "x == y == z" or " x < y < z" is authorized in C
> or Perl. If somebody needs it, it should be implemented the way it
> is in mathematics (i.e. "x < y and y < z" and not some crazy
> integer to boolean compare)

These languages have no dedicated boolean type and no dedicated operators
for logical equivalence and implication. When you need them, you have to
use the integer equality for equivalence (obvious) or the right integer
inequality for implication (much less obvious, perhaps even counter-
intiutive, because A ---> B has to be written as A <= B...of course, you
can always use !A || B...). This might explain why things like (x == y)
== z or (x < y) < z (parentheses are intentional) are allowed.

Why they made relational and equality operators associative is beyond
me. Perhaps they intended to give just another proof these languages
provide anyone enough rope to hang himself? :)

> * And I am just realizing that we should forbid things like "!!x" or
> "~~ x" because they are useless now and may clash with some future

~~x (i.e. bitwise negation of bitwise negation) is pointless because the
result is the identity. !!x is not the same as x in C--it is a funny
shortcut for x != 0 (which is a shortcut for x != 0 ? 1 : 0). (Also,
those two things are not the same in intuitionistic logic...but I suppose
such a formal logic finesse is irrelevant for the design of a simple
imperativeprograming language...)

> operator (Sorry Renaud! Implementing "!!" is easier than the fish
> operator ">(°>" :)

Do not forget the Broccoli operator ("<>")! :)

> Anyway I agree that one expects a "compact" operator (I mean, with a
> very short name) like "!" to be strongly tied to the next variable.
> That's more a problem of readability than logic.

Yes. (See above.)

--Pavel Kankovsky aka Peak [ Boycott Microsoft--http://www.vcnet.com/bms ]
"Resistance is futile. Open your source code and prepare for assimilation."