C complex declarative parsing (zz)

Source: Internet
Author: User

I. right-left rules
I have read the analysis rules in Expert C programming, but they are not very comfortable to use. I searched online and found a famous "right-left" rule. The rules are summarized as follows after translation:

Right-left rule:
0. Symbols in the rule
* Read as "pointer"
[] Reading as an array"
() Read as a "function that returns"
1. Start Point
Find the identifier in the declaration, which is the starting point of your analysis. Read it as: "$ (identifier) is ...";

2. Right side
View the right of your identifier
A) if you find "()", you will know that this is a function declaration. Then you can say "$ (identifier) is a function that returns ";
B) If you find "[]", you will know that this is an array declaration. Then you can say "$ (identifier) is an array ";
C) continue to the right until the right declaration ends or ")", and continue the following.
3. Left
Look at the left side of your identifier
A) if you encounter a symbol that is not defined in 0., You can directly say it. Otherwise, the symbol meaning defined in 0. is used. Continue to the left until the end of the statement on the left or "(".
4. Repeat steps 2 and 3 until the declared analysis is complete.

Ii. Detailed examples
We have evolved from simple to complex.
[Example 1] int * P [];
1) locate the identifier: P, and read it as: "P is ...";
2) look to the right: Find a "[]", and then read it as "P is an array of..." at the end of the Declaration on the right ";
3) Look to the left: Find a "*" and read it as: "P is an array pointing to the pointer ";
4) continue to the left: If no symbols defined in 0. are found, the analysis ends and reads: "P is an array pointing to the int type Pointer ".

[Example 2] int * (* func ())();
1) Find the identifier: func, and read it as: "func is ...";
2) look to the right: Find a "()" and then encounter ")". Read it as: "func is a function that returns ";
3) Look to the left: Find a "*", and then encounter "(", read as: "func is a function that returns a pointer ";
4) look to the right: Find a "()", then end the Declaration on the Right, and read as: "func is the function that returns the pointer to the function that returns ";
5) Look to the left: Find a "*" and read as: "func is the function that returns the pointer to the function that points ";
6) to the left: If no symbols defined in. are found, the analysis ends and reads: "func is the function that returns the pointer to the int type pointer ".

3. Combination of common and invalid declarative symbols
Including:
[] ()-Cannot have an array of functions
()-Cannot have a function that returns a function
() []-Cannot have a function that returns an array

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.