2 steps to solve the FIRST set + 5 steps to solve the FOLLOW set (guaranteed that there will be no omissions!!)

Write in front

I have seen a lot of methods for solving FIRST and FOLLOW sets on the Internet, but sometimes there are still some omissions when used, so I summarized the following methods. As long as you strictly follow this method, you will definitely not find the omissions.

We know that the reason for finding the omissions is actually that various derivations are always wrong. If we can find all sets by looking at the productions, without the need for specific derivations, then there will be no omissions.

Solve the FIRST set

Since solving the FOLLOW set involves solving the FIRST set, let me first explain how to solve the FIRST set.
According to the definition of FIRST set:
Insert picture description here
Simply put, a non-terminal symbol (only non-terminal symbols are discussed here) can deduce a string starting with a terminal symbol, then the terminal symbol belongs to the FIRST set of the non-terminal symbol. This results in the following algorithm, two steps:

  1. Go through all the productions and find all the productions A->a... whose right side starts with a terminal symbol. Pay special attention here, don't forget that there is only one terminal symbol on the right side of the production! ! Many people see A->a... they take it for granted that there must be something behind a, which is wrong. Finally, add a to FIRST(A).
  2. Traverse all the productions and find the one on the right that starts with a non-terminal symbol. Here also note that A->B is also allowed, there is not necessarily something behind B, and then FIRST(B) is added to FIRST(A).

The point is to find the FOLLOW set. I decompose this step into five steps:

  1. Add # to FOLLOW(S), where S is the start symbol of the grammar.
  2. Search all productions, find all productions similar to A->...Ba..., add a to FOLLOW(B), here also pay attention: there can be nothing in front of B and behind a! !
  3. Go through all the productions and find all two productions similar to A->...BC..., BC is a non-terminal symbol. Here is an extreme example: A->BCDE, we will add any two adjacent non-terminal symbol pairs, and all but the empty string in the FIRST set of the following non-terminal symbols will be added to the FOLLOW set of the preceding non-terminal symbol! Simply put, here you need to add the FIRST sets of C, D, and E to the FOLLOW sets of B, C, and D respectively.
  4. Go through all the productions and find the productions similar to A->...B. Note here: Don’t forget that there is only one non-terminal symbol on the right side of the production, such as A->B. Then we add FOLLOW(A) to FOLLOW(B).
  5. Search all productions and find all productions similar to A->...BC. Take A->BCDE as an example. Here we only need to consider the relationship between DE, because these two adjacent non-terminal symbols must be at the end! ! If FIRST(E) contains empty strings, add FOLLOW(A) to FOLLOW(D). Step 5 is actually a special case of step 4.

Note that all the above processes should be repeated at least twice to prevent this situation: For example, if S wants to join E, S adds a new one in this round, but E is not updated at the end.

Practice it

Insert picture description here

1. Solve the FIRST set

1). According to the above algorithm, first find all productions A->a... whose right side starts with a terminal symbol. We find that S={a},D={ ϵ \epsilon ϵ},T={b},M={b},H={ ϵ \epsilon ϵ }.
2). Traverse all productions and find the one that starts with a non-terminal symbol on the right. D->STe, so add S to D, D={ ϵ \epsilon ϵ ,a}; H->M, add M to H, so H={ ϵ \epsilon ϵ ,b}
3). In summary: S={a},D={ ϵ \epsilon ϵ,a},T={b},M={b},H={ ϵ \epsilon ϵ , b}

2. Solve the FOLLOW set

1). S={#}.
2). Find all productions with a terminal symbol behind the non-terminal symbol on the right side of the production. We find: D->STe, so T={e}.
3). Find all productions with two adjacent nonterminal symbols on the right. We found: D->STe, so FIRST(T) is added to S, S={#,b}.
4). Find all productions whose right side ends with a non-terminal symbol. We find: S->aD, so add S to D, D={#,b}; T->bM, add T to M, M={e}; M->bH, add M to H , H={e}; H->M, add H to M, and M remains unchanged.
5). Look for all the productions that end with two adjacent non-terminal symbols on the right side.
6). To sum up: S={#,b}, D={#,b}, T=M=H={e}.

Guess you like

Origin blog.csdn.net/Cyril_KI/article/details/110089473