Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #33205 > unrolled thread

Re: -2 ** 2

Started byThomas 'PointedEars' Lahn <PointedEars@web.de>
First post2017-05-05 08:17 +0200
Last post2017-05-05 08:17 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.javascript

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: -2 ** 2 Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 08:17 +0200

#33205 — Re: -2 ** 2

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2017-05-05 08:17 +0200
SubjectRe: -2 ** 2
Message-ID<1634774.atdPhlSkOF@PointedEars.de>
Stefan Ram wrote:

>   In a recent Firefox:
> 
> -2 ** 2
> 
> SyntaxError: unparenthesized unary expression can't appear on
> the left-hand side of '**'
> 
>   . Can't seem to find the corresponding rule in a recent copy
>   of ECMAScript (12.6), though.

Next time, *cite* your sources, OK?

<http://www.ecma-international.org/ecma-262/7.0/#sec-exp-operator>

| 12.6 Exponentiation Operator
| 
| Syntax
| 
| ExponentiationExpression[Yield] :
|   UnaryExpression[?Yield]
|   UpdateExpression[?Yield] ** ExponentiationExpression[?Yield]

(via /Statement/ → /ExpressionStatement/ → /Expression/ → 
/AssignmentExpression/ → … → /MultiplicativeExpression/)

*The longest match wins*, so check the second (longer) alternative first:  
“-2” must be producible by UpdateExpression.  Check by looking at the 
possible productions for that:

| 12.4 Update Expressions
| 
| Syntax
| 
| UpdateExpression :
|   LeftHandSideExpression[?Yield]
|   LeftHandSideExpression[?Yield][no LineTerminator here]++
|   LeftHandSideExpression[?Yield][no LineTerminator here]--
|   ++ UnaryExpression[?Yield]
|   -- UnaryExpression[?Yield]

Conclusion: “-2” cannot be produced by the /UpdateExpression/ production’s 
results. 

The only remaining alternative is /UnaryExpression/:

| 12.5 Unary Operators
| 
| Syntax
| 
| UnaryExpression :
|   UpdateExpression[?Yield]
|   delete UnaryExpression
|   void UnaryExpression
|   typeof UnaryExpression
|   + UnaryExpression
|   - UnaryExpression
|   ~ UnaryExpression
|   ! UnaryExpression

And therein lies the problem.

“-2” cannot be produced by /UpdateExpression/, so it must be producible by 
“- UnaryExpression” for “-2 ** 2” to be syntactically valid.

But “2” cannot be produced by /UnaryExpression/, because it can neither be 
produced by /UpdateExpression/ nor any other /UnaryExpression/ production 
result.

Conclusion: “-2 ** 2” is *syntactically invalid*.

    [This has nothing to do with operator precedence.]

Finding out why “(-2) ** 2” is valid syntax, and works as intended, is left 
as an exercise to the reader.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

[toc] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web