Int A = 1;
Int B = 1;
A ++ B;
A --- B;
Some people often ask what the values of these expressions are ..? What is the basis for analyzing such expressions ...? Let's discuss this content...
Here we need to talk about the greedy method... in <C traps and pitfalls>, we illustrate the rule that every symbol should contain as many characters as possible.
The compiler is analyzingProgramWhen reading a single character from the left to the right, how can we parse a string like "/* test ..? According to the rules, the first read must be the character '/', so you should try to include as many characters as possible and try to include the character '*', as a result, they can form a valid string "/*". The next character is 'T', Which is meaningless when combined with the preceding string, therefore, "/*" is the longest and meaningful string that can be combined. therefore, the resolution result is displayed. "/* test" should be resolved to "/*", followed by "test" and "test" to continue parsing according to the above rules... this is the process of lexical parsing, that is, the greedy method ".
After understanding the principle, let's look at the expression a ++ B; how should we resolve it:
Also according to the rules:
The next character is the character '+'. According to the principle of containing as many characters as possible, the following one is also the character '+', and the combination is "++ ", inC LanguageIs the auto-incrementing operator, so it is completely valid, so it parses "A ++", and so on, according to the rules, the next step will be resolved to "++". Here there is a problem...
It should be known that in C language, a ++ cannot be used as the left value. Therefore, the rule is resolved to "++", and the whole should be "(a ++) ++ ", but because a ++ cannot be used as the left value and violates the C language syntax, it cannot be parsed as '+, now it is parsed into "(a ++) + (++ B )"...
So the results come out on your own.