Reputation: 10861
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.
Upvotes: 4
Views: 746
Reputation: 49719
The operators involved are ++
, --
, +
and -
. Some parantheses and spaces will help here:
a = ((b++)++) + (b--) - (c--);
I don't know how parsing works exactly, but there's no ambiguity involved (OK, there is, see Dingo's answer), so I guess it could be done with some simple rules like:
+
and -
combine two "expressions"++
and --
are a suffix to an "expression"To remove the ambiguity, you can give ++
and --
a higher priority than +
and -
.
Upvotes: 2
Reputation: 1951
I do know know how much are you familiar with parsers, so just in case: http://en.wikipedia.org/wiki/LL_parser
If you need a formal grammar description, take a look at description for parser generator: https://javacc.dev.java.net/servlets/ProjectDocumentList?folderID=110
Upvotes: 1
Reputation: 3395
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 -- ;
Upvotes: 5