FIRST set and FOLLOW set

First集

How to understand the FIRST set? As the name implies, it is the set of all possible start symbols of a grammar.
The steps to find the FIRST set are as follows:

  • If X->a.., add the terminator a to FIRST(X);
  • If X->e, add the terminal symbol e to FIRST(X) (e represents an empty set);
  • If X->BCD...E, add all elements of First (B) (except the empty set) to First (X), and then detect First (B), if there is no empty set in First (B), that is, e, stop , if it exists, look behind B, add all elements (except the empty set) in First (C) to First (X), and then check whether there is e in First (C)... Until the end, if all non-existent elements before E If the First set of the terminator contains e, then when E is detected, First (E) is also added to First (X), if First (E) contains e, then e is added to First (X).

The first two are quite easy to understand, and the third is to explain: a grammar, if the preceding non-terminal symbol cannot deduce an empty set, then the terminal symbol generated by the latter non-terminal symbol can never become the first symbol of the grammar, Therefore, special consideration needs to be given to the case of empty sets.

FOLLOW集

For the Follow set, it is actually similar, which refers to the set of all characters that may appear after the end of the string introduced by the non-terminal symbol. Also, "#" represents the trailing symbol of the identifier. Steps to
find the Follow set:

  • For grammar start symbol S, put # in FOLLOW(S);
  • For the production formula: A->aBC, add First (C), which removes the empty set e, to Follow (B);
  • For the production: A->aB or A->aBC, (where C can deduce an empty string, C=>*e), then Follow(A) is added to Follow(B).

Follow(B) is the set of characters after the end of the string pushed by B. In the case of A->aBC, B pushes the set of characters after the end of the string, that is, the set of characters at the beginning of the string pushed by C, that is, removed from First(C). collection of e.
A->aB , then A pushes out the character set after the end of the string, which is equivalent to B pushes out the character set at the end of the string, FOLLOW(A) = FOLLOW(B).
Here's an example (e stands for empty):

E => TE`
E`=> FT`| e 
T => FT`
T`=>*FT`|e
F =>(E)|i
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
Follow(E) = (#,))
Follow(E`) = (#,))
Follow(T) = (#,+,))
Follow(T`) = (#,+,))
Follow(F) = (#,*,),+)
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325934777&siteId=291194637
set
set