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.