HIT compiler theory [hit] parsing Follow sets first set and explain

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. If you have to reprint, plus my original URL trouble, thank you. http://blog.csdn.net/qinglingLS https://blog.csdn.net/qinglingLS/article/details/89341046

Parsing Follow sets first set and explain the need

Patience and thinking

After reading some do question no problem,
believe me, yes drop! ! ! ! !
Here Insert Picture Description

The grammar of the following analysis:

In fact, the rules have made it very clear, the following four cases:
1.假如X已经是一个终结符号了that is solved as in the following forms:
request First (F), first of all, F two productions of the body, which is F -> (E) and F -> id, the id and there is (are already a terminator, whereby the F first set is {(, id}.
2.假如X是一个非终结符,且X->Y1Y2Y3……Yk是一个产生式 ,且Y1不能推导出空ethat follows:
seeking first (T), first, T is only one production, T -> FT ', according to the rules, there is a Y1 is F, Y2 is T', to find Y1 (i.e. F) is first set
above has been seeking over, known is {(, id}, there is no empty e, therefore for here, {(, id} is a collection of First (T) in.
3.假如X是一个非终结符,且X->Y1Y2Y3……Yk是一个产生式 ,且Y1能推导出空ethat follows:
Suppose the above requirements First (F) to give {(, E} , E represents an empty ( note here purely hypothetical !! )
seeking first (T), first, T is only one production, T-> FT ', according to the rules, there is a Y1 is F, Y2 is T', seeking to Y1 (i.e. F) is set first,
this time can be known Y1 (i.e. First (F.)) inside the free give look for Y2 (i.e. T 'of the first set), if First (T') which does not contain air, put First (T ') added to the list, if containing empty, put the empty e also added to the list.
So the question is, if T has only one production, T-> FT'E? This time First (T') or with empty, then it added the {First (T ') - e } after then seek First (E), the First (E) to be added to the list !!
4.如果X->e是一个产生式,That follows:
Seeking First (E '), firstly, E' produce two body type, that is, E '-> + TE' and E '-> e, comprising generating a production of e, so that first (E') = { e, +} ( here, from the + E '-> + TE', in accordance with a rule to give )

The following is a summary of the book:

Here Insert Picture Description

Having a first set of rules, to tell you the follow set of rules:

Here Insert Picture Description
Description notation on my books down here and not the same, but the result is the same, just how you remember.
The rules are divided into three cases:
1.假如s是一个左边的符号:That the form:
E-> the TE ' , E' -> the TE + ' , and the like
where E and E' are on a start symbol,
2.假如存在A->aBp,那么First(p)除了空e之外的所有符号都在Follow(B)里面,如果First(p)包含了空e,那么Follow(A)中的符号也要加入到Follow(B)里面
that is to say of the form: Solving the Follow (F)
where F on the right there are two equations, T-> the FT ' (where a is empty E), T' -> * the FT ', (where a is the multiplication sign), occurs only when T' with behind it,
so that, according to the rules, there First (T ') in addition to the null symbol e in follow (F) inside,
and First (T') according to the above first request set, found: {multiplication of empty e}, i.e. with an empty, so according to the above rules, Follow (T ') and Follow (T) should be added to Follow (F) inside,
i.e. First (T') the U- the Follow (T ') the U- the Follow (T) = {multiplication, empty e} - {empty} E the U- {+,), $} the U- the Follow (T)

3.假如存在A->aB,那么Follow(A)中的符号要加入到Follow(B)里面
For example the Follow (T '), where T' -> * FT ', T-> FT', these two formulas which are T 'at the end, in line with the above form, therefore, should Follow (T) was added to the Follow (T ') which, since T' itself is T ',
so Follow (T') = Follow ( T)!

The following is a summary of the book:

Here Insert Picture Description

The following is a description of some of the examples given in the book, (6) to omit it, I wrote in the above example, the set of rules Follow 2 inside, you can understand yourself first before looking at.
Here Insert Picture Description

The following is a reprint of a small part, to expand it:

Original: https://blog.csdn.net/CooperNiu/article/details/78524688

SELECT set
1, is defined:
for a given context-free grammar production A → α, A∈VN, α∈V * , 若α不能推导出ε,则SELECT(A→α)=FIRST(α)   
如果α能推导出ε则:SELECT(A→α)=(FIRST(α) –{ε})∪FOLLOW(A)to be noted that, SELECT is set for production terms.

2, LL (1) grammar:
① a context-free grammar is a necessary and sufficient condition LL (1) grammar is: for each of the two different nonterminal production of A, A → α, A → β , satisfy the SELECT ( A → α) ∩SELECT (A → β) = empty set where α, β are not simultaneously able to derive ε.
Meaning ② LL (1) grammar:
a first L input string from left to right scan
the second generation L is the leftmost derivation of
an input symbol to the right look can decide which production.
(1) grammar discrimination ③LL: When we need to use a top-down analysis technology, we must first determine whether a given grammar is LL (1) grammar. Thus we need to calculate the FIRST, FOLLOW, SELECT Given any set of grammar, the grammar is further determined whether LL (1) syntax.

Example 3. Solution:
1, grammar G [S] is:
  S → AB
  S bC →
  A [epsilon] →
  A → B
  B [epsilon] →
  B → aD
  C the AD →
  C → B
  D aS →
  D → C
requirements of each production SELECT set, and determines whether the grammar G LL (1) grammar?
Solution: the SELECT (S → AB) = (FIRST (AB) - {[epsilon]}) ∪FOLLOW (S) = {B, A, #}   
the SELECT (bC → S) = FIRST (bC) = {B}   
the SELECT (A [epsilon] →) = (FIRST ([epsilon]) - {[epsilon]}) ∪FOLLOW (A) = {A, C, #}   
the SELECT (A → B) = FIRST (B) = {B}   
the SELECT (B → [epsilon]) = ( FIRST ([epsilon]) - {[epsilon]}) ∪FOLLOW (B)} = {#   
the SELECT (B → aD) = FIRST (aD) = {A}   
the SELECT (the aD → C) = FIRST (the aD) = {A, B, } C   
the SELECT (C → B) = FIRST (B) = {B}   
the SELECT (D → aS) = FIRST (aS) = {A}   
the SELECT (D → C) = {C} = FIRST ©

Produced by the above calculation result can be obtained is the same as the left portion of formula SELECT intersection is:

The SELECT (S → AB) ∩SELECT (bC → S) = {B, A, B #} ∩ {B} ≠ {} = [Phi]   
the SELECT (A → [epsilon]) ∩SELECT (A → B) = {A, C, } ∩ {B} # = [Phi]   
the SELECT (B → [epsilon]) ∩SELECT (aD → B) = {#} ∩ {A} = [Phi]   
the SELECT (the aD → C) ∩SELECT (C → B) = {B, A, {B}} ∩ C = [Phi] {B} ≠   
the SELECT (D → aS) ∩SELECT (C → D) = {A} ∩ {C} = [Phi]   
of LL (1) grammar definition of the grammar is not known LL (1) grammar, same as the intersection of the left portion of its production SELECT set is not empty.

Summary:
1. Select the role set is to be set first set and follow the merger, if the left are two grammar A, if they select set of the intersection is empty, indicating that they are two independent, no uncertainty of grammar, otherwise, it indicates that the grammar is not LL (1) syntax.

Guess you like

Origin blog.csdn.net/qinglingLS/article/details/89341046