Converter 4: hand-written PHP to Python compiler, Syntax Parsing part, python Compiler

Source: Internet
Author: User

Converter 4: hand-written PHP to Python compiler, Syntax Parsing part, python Compiler

After writing the lexical part, there were a lot of chores. At last we were free to implement the great Syntax Parsing part.

After coding, we found that the program was too short, not counting the state machine, only 186 lines (including comments), and less than 100 lines of key code. After running the debugging, it is okay. You can parse function. php In OneThink. This file can be called a gathering of Php programs. There are all demons and ghosts in it. It is really a tear during debugging. Of course, I won't say that either.

 

Because the program is too short, I am going to explain it in detail so that you don't quite understand the details :)

We know that Syntax Parsing generally involves analysis methods such as LL (1), LR (0), SLR (1), LALR (1), and LR (1. The common ones are LL (1) and LR (0)

LL: This analysis method is left-to-right scanning and left-to-left derivation. LR is left-to-right scanning and rightmost derivation. LL uses the prediction table and LR uses the analysis table; the difficulty of LR lies in LL, and the analytical capability is also above LL, and there are more changes in LR. Therefore, for such a ticket-winning project, you must use LR to create a new (bi) steadily ).

The LR analyzer model is shown in.

There are two stacks. The most important task is to generate the LR analysis table. Of course, I am not prepared to follow the classic methods in the textbooks honestly. How can I create a new (lan? This is the key.

We can see that SLR (1), LALR (1), and LR (1) are all improvements to LR (0). The most important one is that (1 ), forward. Why do we need to look forward? To reduce the size of the analysis table. There will be countless possibilities in the future. Looking forward, the possibility will be reduced, and the analysis scale will be greatly reduced. To reduce the size of the analysis, we must look forward. The more we see the analysis, the smaller the table, and the more difficult the programming is. Is there any way for me to stand up and make money, so that I don't look forward, the difficulty does not increase, and the analysis table is reduced.

Yes, I do. It's hard to be a senior lazy. We know that the demand for looking forward comes from grammar expressions, such as A → Abc. The longer the length of A single expression, the greater the uncertainty. Therefore, to limit the maximum length of a Method Expression, You can ensure 100% certainty in this length, so you do not need to look forward. I name this method with a length limit of LR, that is, LLLR (0, n), n> = 3.

In this case, I naturally chose LLLR () as the analysis method. For convenience, I decided not to save the status, so I didn't need to generate an analysis table and didn't generate a table, no table generated ......

Mom! This is also too lazy. If you do not save the status, you need to search from the beginning every time. efficiency is the life of a programmer!

Please be careful. The maximum length of the expression is 3. You can search for a maximum of 3 steps. You can rest assured that this is the case. :)

 

This is the mystery of the 100-line super-simple Php compiler. How can we create a new bi. Source code: converterV0.4.zip Enjoy!

 

<To be continued>

 

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.