LR analysis process of a simple instance

Source: Internet
Author: User

After the first two articlesArticle. We have already explained the construction process of the most important analysis table in LR syntax analysis. Add a small question, that is, the classification of LR (0) projects.

The project is divided into the following types based on the position of the dot and whether it is a terminator or not a terminator after the DOT:

Input project: for example, a → α. A β. A is the Terminator, and a, B, and V * are the same
Project to be executed: A → α. B β, where B is not the Terminator
Reduction Project: A → α. indicates that the formula has been analyzed.
Accepted items: such as S → S.
Special. A → ε's LR (0) project only has a → • It is a reduction project

Because the structure of the LR analysis table has been clearly explained in the previous two articles, this topic is important to explain how to use the analysis table to construct the table, you may have to refer to the previous two articles to construct the analysis table. The question is from the network.

Well, let's take a look at the question. We know the grammar G [s]:

(1) S → aacbe
(2) A → B
(3) A → AB
(4) B → D

Write the LR analysis process for the input string abbcde.

During analysis, because of our manual analysis, we still need a table to record our steps. Otherwise, you cannot remember it. The table contains seven columns. The number of rows is not fixed. What is done.

Procedure Symbol Stack Input symbol Stack Action Status Stack Action Goto


The step is to increase from 1 down. The symbol stack is used to save the results in the operation. The initial value is #. The input string is saved in the input symbol stack. The initial value is given. In the action, it is used to comment on whether to move in or to the Protocol. The status stack is the one that maintains the LR analysis table. The same is true for action and Goto.

Through the steps in the first two articles, the following LR analysis table can be constructed:


The analysis table contains Si and RJ. S is the abbreviation of shift, that is, the input, and r is the abbreviation of Reduce, that is, the protocol. The Statute is a derived inverse operation. Everyone knows it.

Let's take a look at the S and J operation rules during analysis.

Si: Move in, move I to the State stack, and move a to the grammar symbol stack. Where I, J indicates the status number.

RI: reduction. Use the I-th generation type reduction. At the same time, the state stack and symbol stack exit the corresponding symbol, then, the Goto table's corresponding status and the left non-terminator of the I-th formula are written into the stack.

The syntax contains a → β production formula. If the length of β is R (that is, | β | = R), the R symbols are removed from the top and bottom of the stack from the State stack and grammar symbol stack, that is, the stack pointer P minus R. And move a into the grammar symbol stack, SJ = goto [Si, A] into the status stack, where Si is the top status of the stack after the pointer is modified.

When there is only the grammar start symbol S left in the grammar symbol stack, and the input symbol string has ended, that is, the current input character is '#', the analysis is successful.

Use the auxiliary table we will use for analysis. I still give the answer directly. Then analyze the typical situation.


In the first step, the symbol stack is #. The input symbol string is the given string to be analyzed. Because the state stack starts from 0, the State stack is directly filled with 0. As we all know, LR analysis is performed from left to right. So I thought about a pointer P. P first points to a of the input string, and then we query the analysis table (0, a). 0 is the state 0, and a is the current character of the pointer. In the analysis table, (0, A) is S2. Fill in the action in the first step, and fill in the Action column to move in. According to the rule, 2 into the status stack, and a into the symbol stack,

In step 2, the pointer P must be one step forward, so the input string enters B. This step is the same as the previous step.

The key is to enter Step 3. At this time, the symbol stack is # AB, the input symbol string is bcde #, and the State stack is 024. In this case, go to the lookup table. The difference is (4, B ), 4 is the top of the State stack, and B is the current position of the P pointer. It is found that it is R2. According to the rule, use the second generative formula (2) A → B. Fill in the Action column first, and exit the corresponding symbol between the status stack and the symbol stack, that is, exit the top 4 of the status bar stack, and exit B of the symbol stack, keep in mind, do not fill in the table, and import the left non-terminator of The Goto table's corresponding status and the I-th formula into the stack. In the Goto table, we need to check (2, A) = 3, 2 is the 2 of R2, and a is the left part of the second generative formula. Therefore, 3 is written into the State stack and A is written into the symbol stack.

The following are all the same. No. To learn how to compile. Multiple hands-on operations are required. You can also try it manually.

Refer:
Http://leaver.me/archives/574.html
Http://leaver.me/archives/548.html

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.