Reading dragon Book compiling Principles of Grammar Analysis (5) ...

Source: Internet
Author: User

Then there is the recursive Descent analysis algorithm (also considered a subclass of the top-down analysis algorithm (also improved)) ...

And then give the pseudo-code ...

However the imagination is beautiful, the reality is cruel:

If, for example, the real thing is to be realized, it is that when the first token is read (it is obviously num), it is possible to find that both E + T and T are feasible, so there is a problem here ... But this situation can sometimes be avoided by using the particularity of the grammar:

In this example, E is ultimately nothing more than T + T + t + ... Format, and T is f * f * f * ... format, it is possible to call parse_t () after reading in 3, then read into a token, if +, call parse_t (), if not, stop calling ...

Next is a small assignment:

Recursive descent analysis algorithm

Given the following grammar g:

A-a B

| A

B-B

| C

Suppose you are writing a recursive descent analysis algorithm for this grammar, give a recursive descent analysis algorithm for the algorithm. What difficulties have you encountered? How do you solve these difficulties?

This is very simple, according to gourd painting scoop, pseudo code as follows

1 parse_a () {2     token = tokens[i++]; 3     if (token = = A) {4         token = tokens[i++]; 5         if (token = = EOF) {6             ret Urn 7         }else{8             parse_b () 9         }10     }else{11         error (...);     }13}14 Parse_b () {+     token = tokens[i++];17     if (token = = B) {         ...     }else if (token = = c) {...     }else{22         error (...);     }24}

Reading dragon Book compiling Principles of Grammar Analysis (5) ...

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.