Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the parsing rules for expressions in C?

How can I understand the parsing of expressions like

a = b+++++b---c--;

in C?

I just made up the expression above, and yes, I can check the results using any compiler, but what I want to know is the ground rule that I should know to understand the parsing of such expressions in C.

like image 256
Moeb Avatar asked Feb 27 '23 00:02

Moeb


1 Answers

From the standard 6.2(4):

If the input stream has been parsed into preprocessing tokens up to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token.

They even add the example:

EXAMPLE 2 The program fragment x+++++y is parsed as x ++ ++ + y, which violates a constraint on increment operators, even though the parse x ++ + ++ y might yield a correct expression.

So your statement:

a = b+++++b---c--; 

Is equivalent to:

a = b ++ ++ + b -- - c -- ;
like image 116
Dingo Avatar answered Mar 08 '23 05:03

Dingo