Follow and First of compilation principles

Source: Internet
Author: User

Given that the grammar in the book is too advanced, I have summarized the following methods to ensure that you will be able to learn it as soon as you learn it!

I would like to thank you, Mr. Zhang feiqing. Without your help, I will not be able to do it today. Haha !!

Example 4.6 for grammar g (E)

E → te'

E' → + Te | E ε

T → ft'

T → * ft 'hour | E ε

F → (e) | I

Construct the first and follow sets of each non-Terminator:

First (e) = {(, I}

First (e ') = {+, ε}

First (t) = {(, I}

First (t') = {*, ε}

First (f) = {(, I}

Follow (e) = {),#}

Follow (E Branch) = {),#}

Follow (t) = {+ ,),#}

Follow (T Branch) = {+ ,),#}

Follow (f) = {*, + ,),#}

Now!

The first is the first set:

This is relatively simple, that is, it is always deduced until a Terminator is found! Note that you only need to check the first character on the right of each generative formula.

The focus is on the follow set:

First, you must know that the folllow set is performed in the right part of the production formula.

1: Find the non-Terminator that requires the follow set in the right of the production formula.

2: Check whether the required non-Terminator is the start character. If yes, add # To the follow set.

2: check whether there are any characters after this Terminator. Are you sure you want to find it? I can't speak anymore!

3: The following is an if condition judgment.

If (not followed by characters)

{

Add the follow set of non-terminator in the right part of the formula to the desired set;

}

Else (with characters)

{

Evaluate the first set for this character first;

If (this character can be deduced to ε)

{

Calculate the follow set of this character;

}

}

4: Done!

Haha, my first learning experience is actually the worst compilation principle I have learned.

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.