Why is `x && y` not parsed as` x & (& y) `?

I am implementing a C compiler and have discovered a curious problem. Since & has a higher priority than && , it seems reasonable to consider it as binary - and the first operand with the address of the second:

 x && y = (x) & ( &(y) ) 

A syntax review of specification C seems to allow this interpretation. Perhaps I lost something or misunderstood?

My understanding of syntax:

and expression: = expressionExpression | (andExpression '&' expressionExpression) | ...
...
unaryExpression: = postfixExpression | (('&' | '*' | '+' | '-' | '~' | '!') castExpression) | ...

+5
source share
1 answer
Expression operators

C are processed through something known as the " maximum munch " 1) which means that from left to right, the compiler is sent to the longest fragment of characters that can form a valid token. Since x && longer than x & , the compiler selects the first.

This is why code like x+++1 compiles, +++x does not work, but + ++x does.


1) C11 Β§6.4 Lexical elements ΒΆ4 :

If the input stream is parsed in the preprocessing token to the specified character, the next preprocessing token is the longest sequence of characters that can make up the preprocessing token.

+12
source

Source: https://habr.com/ru/post/1274945/


All Articles