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


Groups > comp.lang.javascript > #33205

Re: -2 ** 2

Path csiph.com!weretis.net!feeder4.news.weretis.net!feeder5.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: -2 ** 2
Date Fri, 05 May 2017 08:17:14 +0200
Organization PointedEars Software (PES)
Lines 82
Message-ID <1634774.atdPhlSkOF@PointedEars.de> (permalink)
References <exponentiation-20170504145841@ram.dialup.fu-berlin.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Mime-Version 1.0
Content-Type text/plain; charset="UTF-8"
Content-Transfer-Encoding 8Bit
X-Trace solani.org 1493965035 32375 eJwNyckRwCAMBLCWbO8BtAPE/ZcQRk8JTp9By1Q/e3anKrUQt1GAaoZf4GM2zW3AK3zrjPoBAcwP4Q== (5 May 2017 06:17:15 GMT)
X-Complaints-To abuse@news.solani.org
NNTP-Posting-Date Fri, 5 May 2017 06:17:15 +0000 (UTC)
User-Agent KNode/4.14.2
Cancel-Lock sha1:xwoZfPczPRi+XnVcedkuVoEQso0=
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
X-User-ID eJwNy8kBwDAIA7CZCObIOGDI/iO0+svUxRlwc9izV6XLXsW5kO3nmw2xreCsj5zKBKX3MqxeE9PzR0LTg+cDiMgW4g==
X-NNTP-Posting-Host eJwFwYEBwDAEBMCVfPEYpyT2HyF3rgQnjE7z9eU/KNHRNQkpiZZAfDiitzPLtOt6ZeslD/AAAREQEQ==
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Xref csiph.com comp.lang.javascript:33205

Show key headers only | View raw


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.

Back to comp.lang.javascript | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web