What are the parsing rules for expressions in C?

2019-05-04 16:57发布

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.

3条回答
Luminary・发光体
2楼-- · 2019-05-04 17:25

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 -- ;
查看更多
时光不老,我们不散
3楼-- · 2019-05-04 17:40

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

查看更多
神经病院院长
4楼-- · 2019-05-04 17:46

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

查看更多
登录 后发表回答