What are the parsing rules for expressions in C?
Asked Answered
G

3

4

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.

Gauntlett answered 23/10, 2010 at 16:56 Comment(2)
@Martin: if you can get 6.2/4 removed from the C standard as "who cares", then you can close this question "who cares" ;-p Daft examples can illustrate fundamentals.Careerist
Possible duplicate of Why doesn't a+++++b work in C?It
S
5

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 -- ;
Spahi answered 23/10, 2010 at 17:5 Comment(4)
That lexes it. Depending what the questioner means by "parse", you also need to use the operator precedence rules to construct an expression tree.Careerist
2.1.1.2 Translation phases 7. [...] Preprocessing tokens are converted into tokens. [...] flash-gordon.me.uk/ansi.c.txtPoltergeist
Given the lack of spaces, I assume he means parsing into tokens. There isn't a valid expression tree for his example, anyway.Spahi
"I assume he means parsing into tokens". Yes, that is what I meant.Gauntlett
O
2

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 -.

Of answered 23/10, 2010 at 16:59 Comment(0)
G
1

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

Godsey answered 23/10, 2010 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.