Programming syntax analysis & ldquo; Priority & rdquo; and & ldquo; combination Law & rdquo;, syntax priority

Source: Internet
Author: User

Programming syntax analysis: "Priority" and "Combination Law", syntax priority

Speaking of expressions in the previous section "Analysis of programming Syntax", the main function of expression is to return a value! The specific value depends on the entire operation process of the expression. To understand the operation process of an expression, you must understand the "Priority" and "Combination law ".

As mentioned earlier, Expressions generally consist of operands and operators (or operators. The "Priority" and "Combination Law" are analyzed for operands and operators. Now let's look at the table:

 

This table describes "Priority" and "Combination law". The smaller the priority number, the higher the priority. When the priority is the same, the "Combination Law" will be considered "!

Note that in the preceding table, the right-to-left column indicates the right combination, and the left-to-right column indicates the left combination. Simply put, the "Combination law" is only divided into "left combination" and "right combination "!

To better understand the "Priority" and "Combination Law", I will give a few meaningful examples.

 

Example 1: This example describes the combination of left and right based on the combination law.

The so-called combination is to combine multiple things into a whole and become a new thing. When an operator is a left Union operator and is a binary operator, it treats the entire thing on its left as a whole and combines it with it. The right side only recognizes the nearest operator. (Right combination is the opposite)

For example: (this is a C ++ example, which can be well understood and combined with this concept)

For the second <operator, the operator considers all on the left as a whole and treats it as a whole. The whole is actually an expression and a value, the value is the value of std: cout.

For the first <operator, only "Hello, World!" is recognized on the right !" And only the one closest to him on the right.

This is explained in the original <Accelerated C ++>;

If you are not familiar with C ++, let's look at the second example:

 

Example 2: A super simple expression, at this time you should have a deeper understanding:

A + B + c

First of all, in this expression, the operators are all plus signs, and everyone has the same priority. Therefore, considering the combination, + is the left combination.

Therefore, the first plus sign is first combined with a, and only recognizes B closest to him on the right. For the second plus sign, a + B is regarded as a whole and combined with it, then, only the right side and the nearest c are recognized. The result is (a) + B) + c.

If the first two examples are not satisfactory, let's look at the Third example:

 

Example 3: This example takes priority and combination law into consideration ".

(* (Void (*) () 0 ))()

Here, the operator has a small arc-a forced conversion character (also called a small arc). The unreferencing operator has only one operand, that is, 0. The operator is for the operand.

Starting from 0, we can see that the closest to 0 is a small arc. This small arc contains a void (*) (), which is actually a type-function pointer type, use a small arc to enclose the type. This small arc is actually a force conversion character. Then the expression (void (*) () 0 is combined to forcibly convert 0 to the function pointer type.

It is also a small arc, which means that the small arc is a combination of the whole. At this time, because (void (*) () 0 is a whole and represents a function pointer, so (void (*) () 0 is replaced with p, the result is (* p) (), which means calling a function using the function pointer p. This p actually points to address 0. So the side effect of this expression is to let the program pointer PC jump to address 0 and complete a software reset function.

To further discuss, remove the brackets:

(* (Void (*) () 0 )()

There are two operators at the beginning for the operand 0. One is the forced conversion operator (type), and the other is the unreferencing operator *. We can see that the (type) operator and the * operator have the same priority as the * operator, the combination is the right combination, which simplifies the analysis of expressions:

(* (Type) 0 )()

Because it is a combination of the right, * will regard the (type) 0 on the right as a whole, and (type) first and 0, so the conclusion is that the meaning of this layer of brackets remains unchanged!

Let's take a look at the fourth example:

 

Example 4: ph-> pNext

This expression is usually seen in the access to the linked list. Although it is so long, it returns a value, so you don't have to worry about it.

Ph is the head pointer.-> this symbolic lookup shows that it is a combination of the Left and the left as a whole, if you only recognize one on the right, then ph-> pNext can be separately separated and combined to form a whole,

After ph-> pNext is taken as a whole, a pointer is actually returned and the pointer value stored in 0th nodes! The Pointer Points to 1st nodes. So ph-> pNext can be replaced by p1.

For the remaining p1-> pNext, follow the above method to get p2-> pNext. The final result is p4-> pNext.

If ph points to 0th nodes, the final result of p4-> pNext is actually the pointer value stored in 4th nodes, and the Pointer Points to 5th nodes. This is the result of the entire expression.

The last example introduces the comma expression as a dessert after dinner.

 

Example 5. Comma expression

The comma expression is like expression 1 and expression 2.

First, a comma expression is also an expression. A comma expression also returns a value as a whole!

Second, the result of the entire comma expression is the result of expression 2.

Some may ask, isn't expression 1 soy sauce? In fact, expression 1 is generally used to pave the way for expression 2, for example: (an example of intercepting from MFC)

Expression 1 actually assigns a value to str, and expression 2 is a comparison statement. if, then, only checks whether expression 2 is true, regardless of the return value of expression 1.

When using a comma expression, pay attention to the priority issue, because "," has a lower priority than "=.

For example, a = 3*5, a * 4; because the priority of "," is lower than that of "=", a = 3*5 is first combined to calculate that a is equal to 15, then a * 4 gets 60. then, based on "the result of the entire comma expression is the result of expression 2"

Therefore, the result of the entire expression (a = 3*5, a * 4) is 60.

The test method is as follows:

Int;

Printf ("% d", (a = 3*5, a * 4 ));

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.