# --first set, follow set and select set of compiling principle

Source: Internet
Author: User
First set, follow set, and select Set First SetFirst (a) is the start or set of symbols for a. 1. Definition:Set g= (v T, v N, s,p) is a context-independent grammar, first (α) ={a|α can derive aβ,a∈vt,α,β∈v*} special, if α can deduce the ε, then the Ε∈first (α).
2, according to the definition of the first set (for each grammar symbol X∈V COMPUTE First (X)):

①. If X∈VT, then first (X) ={x}. (Simply put, Terminator's first set is itself)

②. If x∈vn, and there is a production x→a...,a∈vt, then A∈first (x) x→ε, then Ε∈first (x). (Simply, if the non-terminator X, can deduce a string beginning with Terminator A, then this terminator a belongs to first (x), if X can deduce the empty symbol string ε, then the empty symbol string ε also belongs to the first set of X)

③. X→y. is a production and y∈vn adds all non-empty symbol string ε elements in first (Y) to First (X). (This can be understood.)

④. If x∈vn;y1,y2,...,yi∈vn, and there is a production x→y1 Y2 ... Yn: When Y1 Y2 ... When Yn-1 can derive ε, all non-empty elements of first (Y1), first (Y2) 、...、 First (Yn-1), and first (Yn) are included in first (X).

That is: First (X) = (First (Y1)-{ε}) ∪ (First (Y2)-{ε}) ∪ ... ∪ (First (Yn-1)-{ε}) ∪{first (Yn)}

⑤. When all Yi in (4) can derive the ε, (i=1,2,... N), then

First (X) = (First (Y1)-{ε}) ∪ (First (Y2)-{ε}∪ ... ∪ (First (Yn)-{ε}) ∪{ε}

Use the above ②~⑤ step repeatedly until the first collection of each symbol no longer increases.

3, the solution Example : 1, the grammar g [S] is:
S→ab
S→bc
a→ε
A→b
C→b
D→as
D→c

The first set of each non-terminator.

Solution: For non-Terminator s,s start character has Terminator B and non-Terminator A, according to the above definition ② to add B to first (s), by the above definition ③ all the non-empty symbol string ε element in first (A) is added to first (s), and because S→ab conforms to the above definition ④ and ⑤, So first (S) = (∪ (a)-{ε}) ∪{ε} then the starting character of a begins with the Terminator B and the empty symbol string ε, which is all added by the definition ① and the definition ②, which is a. First ( A) ={ε,b}, similarly, first(b) ={a,ε}, so that first (S) is also asked, First (S) ={b,a,ε}, actually looked back to the reason why the definition of ②~⑤ is called repeatedly because a in the start character may be an empty string ε, so consider the case after a.

Again, first (c), by defining the ①, adding B to start (c), and then by defining ④,first (c) = ((A)-{ε}) ∪{first (d)}, firstly (d), easy to know,(d) ={a,c}, So first (C) ={b,a,c}

4, Summary:

1. For Terminator, the element of first concentration is only its own

2, for non-Terminator, if the start character is terminator or the empty symbol string ε, then join its first set, if the start character is non-terminator, you want to add its non-ε first set, and consider the case of ε.

second, follow set Follow (A) ={a| S can deduce μaβ, and A∈VT, A∈first (β), μ∈v T *, Β∈v +}, if s can derive μaβ, and β can deduce ε, then #∈follow (a). can also be defined as: Follow (A) ={a| S can deduce ... AA...,A∈VT}, if there is s can deduce ... A, which specifies #∈follow (a), where we use ' # ' as the terminator of the input string, or as a sentence bracket,
such as: #输入串 #.

Follow (a) is a collection of symbols that are not terminator a followed by a symbol.
1, definition: set g= (vt,vn,s,p) is a context-independent grammar, A∈vn,s is the beginning of the symbol

It should be noted that the follow (a) set is for non-terminator A, the element in the collection is Terminator, is a set of all the followed symbols of a, and when a is the starting character (identifier) of the grammar g, the ' #也加入其中 '

2, according to the definition of follow set (for each grammar symbol S∈VN calculation follow (S)):

①. Set S as the starting symbol in the grammar, add {#} to follow (S) (Here "#" is the sentence bracket). Ii. If A→αbβ is a production type, the non-empty element of first (Beta) is added

③. Reuse (b) until each non-terminator follow set no longer increases.

3, the solution Example : 1, the grammar g [E] is: E→te '
E ' →+te ' | ε
T→ft '
T ' →*ft ' | ε
f→ (E) | I

Related Keywords:

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.