views:

108

answers:

3

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.

+2  A: 

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:

  • One or more characters make a variable name, the most simple type of "expression"
  • Operators + and - combine two "expressions"
  • Operators ++ and -- are a suffix to an "expression"

To remove the ambiguity, you can give ++ and -- a higher priority than + and -.

schnaader
@schnaader: thanks, but I want to know how I can figure this out myself.
Amoeba
+5  A: 

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 -- ;
Dingo
That lexes it. Depending what the questioner means by "parse", you also need to use the operator precedence rules to construct an expression tree.
Steve Jessop
2.1.1.2 Translation phases 7. [...] Preprocessing tokens are converted into tokens. [...] http://flash-gordon.me.uk/ansi.c.txt
starblue
Given the lack of spaces, I assume he means parsing into tokens. There isn't a valid expression tree for his example, anyway.
Dingo
"I assume he means parsing into tokens". Yes, that is what I meant.
Amoeba
+1  A: 

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

ika