Compiler First Set Follow Set Brief

3,001 views

Published on

Published in: Technology, Self Improvement
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
3,001
On SlideShare
0
From Embeds
0
Number of Embeds
23
Actions
Shares
0
Downloads
95
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Compiler First Set Follow Set Brief

  1. 1. First Set Red : A Blue :  First(A) , if  ∉ First(A) • First (=A) = First(A) – {} ∪First(), if  ∈ First(A) We will use some examples for this operation…
  2. 2. First Set (2) Red : A Blue :  EPrefix(E) EV Tail PrefixF G0 Prefix Tail+E Tail 
  3. 3. EPrefix(E) EV Tail First Set (2) PrefixF G0 Prefix Red : A Blue :  Tail+E Tail  Step 1: • First (EPrefix(E))=First(Prefix(E)) ? = {Waiting operation} – We don’t know First(Prefix) at this moment. – So we keep the operation of First(Prefix(E)) until we know First(Prefix). • First (EV Tail) = First(V) = {V} • First (PrefixF) = First(F) = {F} First (Prefix ) = First() - {} ∪First() = {} • • First (Tail+E) = First(+) = {+} First (Tail ) = First() - {} ∪First() = {} •
  4. 4. EPrefix(E) EV Tail First Set (2) PrefixF G0 Prefix Red : A Blue :  Tail+E Tail  Step 1: • First (EPrefix(E))=First(Prefix(E)) ? = {Waiting operation} – We don’t know First(Prefix) at this moment. – So we keep the operation of First(Prefix(E)) until we know First(Prefix). • First (EV Tail) = {V} • First (PrefixF) = {F} • First (Prefix ) = {} • First (Tail+E) = {+} • First (Tail ) = {} Step First Set E Prefix Tail ( ) V F + {F, } {+, } First(Prefix(E))∪{V} Step 1
  5. 5. EPrefix(E) EV Tail First Set (2) PrefixF G0 Prefix Red : A Blue :  Tail+E Tail  Step 2: • First (EPrefix(E))=First(Prefix(E)) ? = {Waiting operation} – In this step, we know First(Prefix). – So we replace the First(Prefix) with {F, }. {F, } • First (EV Tail) = {V} • First (PrefixF) = {F} • First (Prefix ) = {} • First (Tail+E) = {+} • First (Tail ) = {} Step First Set E Prefix Tail ( ) V F + {F, } {+, } First(Prefix(E))∪{V} Step 1
  6. 6. EPrefix(E) EV Tail First Set (2) PrefixF G0 Prefix Red : A Blue :  Tail+E Tail  Step 2: • First (EPrefix(E))=First(Prefix(E)) = {F, } = {F, } - {} ∪ First((E)) = {F, (} • First (EV Tail) = {V} • First (PrefixF) = {F} • First (Prefix ) = {} • First (Tail+E) = {+} • First (Tail ) = {} Step First Set E Prefix Tail ( ) V F + {F, } {+, } First(Prefix(E))∪{V} Step 1
  7. 7. EPrefix(E) EV Tail First Set (2) PrefixF G0 Prefix Red : A Blue :  Tail+E Tail  Step 2: • First (EPrefix(E))= {F, (} • First (EV Tail) = {V} • First (PrefixF) = {F} • First (Prefix ) = {} • First (Tail+E) = {+} • First (Tail ) = {} Step First Set E Prefix Tail ( ) V F + {F, } {+, } First(Prefix(E))∪{V} Step 1 {F, } {+, } {F, (} ∪ {V} ={F, (, V} Step 2
  8. 8. EPrefix(E) EV Tail First Set (2) PrefixF G0 Prefix Red : A Blue :  Tail+E Tail  Step 3: • First (EPrefix(E))= {F, (} • First (EV Tail) = {V} • First (PrefixF) = {F} • First (Prefix ) = {} If no more change… • First (Tail+E) = {+} The first set of a terminal • First (Tail ) = {} symbol is itself Step First Set E Prefix Tail ( ) V F + {F, } {+, } First(Prefix(E))∪{V} Step 1 {F, } {+, } {F, (} ∪ {V} ={F, (, V} Step 2 {F, } {+, } {F, (, V} {(} {)} {V} {F} {+} Step 3
  9. 9. Another Example….
  10. 10. First Set (2) Red : A Blue :  S  aSe SB B  bBe G0 BC C  cCe Cd
  11. 11. S  aSe SB B  bBe G0 BC First Set (2) C  cCe Cd Red : A Blue :  Step 1: • First (SaSe) = First(a) ={a} • First (SB) = First(B) • First (B  bBe) = First(b)={b} • First (B  C) = First(C) • First (C  cCe) = First(c) ={c} • First (C  d) = First(d)={d}
  12. 12. S  aSe SB B  bBe G0 BC First Set (2) C  cCe Cd Red : A Blue :  Step 1: • First (SaSe) = {a} • First (SB) = First(B) • First (B  bBe) = {b} • First (B  C) = First(C) • First (C  cCe) = {c} • First (C  d) = {d} Step First Set S B C a b c d {c, d} {a}∪First(B) {b}∪First(C) Step 1
  13. 13. S  aSe SB B  bBe G0 BC First Set (2) C  cCe Cd Red : A Blue :  Step 2: • First (SaSe) = {a} • First (SB) = First(B) = {b}∪First(C) • First (B  bBe) = {b} • First (B  C) = First(C) • First (C  cCe) = {c} • First (C  d) = {d} Step First Set S B C a b c d {c, d} {a}∪First(B) {b}∪First(C) Step 1
  14. 14. S  aSe SB B  bBe G0 BC First Set (2) C  cCe Cd Red : A Blue :  Step 2: • First (SaSe) = {a} • First (SB) = {b}∪First(C) • First (B  bBe) = {b} • First (B  C) = First(C) • First (C  cCe) = {c} • First (C  d) = {d} Step First Set S B C a b c d {c, d} {a}∪First(B) {b}∪First(C) Step 1 {a}∪ {b}∪First(C) Step 2
  15. 15. S  aSe SB B  bBe G0 BC First Set (2) C  cCe Cd Red : A Blue :  Step 2: • First (SaSe) = {a} • First (SB) = {b}∪First(C) • First (B  bBe) = {b} • First (B  C) = First(C) = {c, d} • First (C  cCe) = {c} • First (C  d) = {d} Step First Set S B C a b c d {c, d} {a}∪First(B) {b}∪First(C) Step 1 {a}∪ {b}∪First(C) Step 2
  16. 16. S  aSe SB B  bBe G0 BC First Set (2) C  cCe Cd Red : A Blue :  Step 2: • First (SaSe) = {a} • First (SB) = {b}∪First(C) • First (B  bBe) = {b} • First (B  C) = {c, d} • First (C  cCe) = {c} • First (C  d) = {d} Step First Set S B C a b c d {c, d} {a}∪First(B) {b}∪First(C) Step 1 {c, d} {a}∪ {b}∪First(C) {b}∪{c, d} = {b,c,d} Step 2
  17. 17. S  aSe SB B  bBe G0 BC First Set (2) C  cCe Cd Red : A Blue :  Step 3: • First (SaSe) = {a} • First (SB) = {b}∪First(C) = {b}∪ {c, d} • First (B  bBe) = {b} • First (B  C) = {c, d} • First (C  cCe) = {c} • First (C  d) = {d} Step First Set S B C a b c d {c, d} {a}∪First(B) {b}∪First(C) Step 1 {c, d} {a}∪ {b}∪First(C) {b}∪{c, d} = {b,c,d} Step 2
  18. 18. S  aSe SB B  bBe G0 BC First Set (2) C  cCe Cd Red : A Blue :  Step 3: • First (SaSe) = {a} • First (SB) = {b, c, d} • First (B  bBe) = {b} • First (B  C) = {c, d} • First (C  cCe) = {c} • First (C  d) = {d} Step First Set S B C a b c d {c, d} {a}∪First(B) {b}∪First(C) Step 1 {c, d} {a}∪ {b}∪First(C) {b}∪{c, d} = {b,c,d} Step 2
  19. 19. S  aSe SB B  bBe G0 BC First Set (2) C  cCe Cd Red : A Blue :  Step 3: • First (SaSe) = {a} • First (SB) = {b, c, d} • First (B  bBe) = {b} • First (B  C) = {c, d} If no more change… • First (C  cCe) = {c} The first set of a terminal • = {d} symbol is itself First (C  d) Step First Set S B C a b c d {c, d} {a}∪First(B) {b}∪First(C) Step 1 {c, d} {a}∪ {b}∪First(C) {b}∪{c, d} = {b,c,d} Step 2 {c, d} {a} {b} {c} {d} {a}∪ {b}∪{c, d} = {a,b,c,d} {b}∪{c, d} = {b,c,d} Step 3
  20. 20. Another Example….
  21. 21. First Set (2) Red : A Blue :  S  ABc Aa A G0 Bb B
  22. 22. S  ABc Aa A G0 First Set (2) Bb B Red : A Blue :  Step 1: • First (SABc) = First(ABc) • First (Aa) = First(a) • First (A  ) = First()∪First() • First (B  b) = First(b) • First (B  ) = First()∪First()
  23. 23. S  ABc Aa A G0 First Set (2) Bb B Red : A Blue :  Step 1: • First (SABc) = First(ABc) • First (Aa) = {a} • First (A  ) = {} • First (B  b) = {b} • First (B  ) = {} Step First Set S A B a b c {a, } {b, } First(ABc) Step 1
  24. 24. S  ABc Aa A G0 First Set (2) Bb B Red : A Blue :  Step 2: • First (SABc) = First(ABc) = {a, } = {a, } - {} ∪ First(Bc) = {a} ∪ First(Bc) • First (Aa) = {a} • First (A  ) = {} • First (B  b) = {b} • First (B  ) = {} Step First Set S A B a b c {a, } {b, } First(ABc) Step 1 {a, } {b, } {a} ∪ First(Bc) Step 2
  25. 25. S  ABc Aa A G0 First Set (2) Bb B Red : A Blue :  Step 3: = {a} ∪ First(Bc) • First (SABc) = {a} ∪{b, } = {a} ∪{b, } - {} ∪First(c) = {a} ∪{b,c} • First (Aa) = {a} • First (A  ) = {} • First (B  b) = {b} Step First Set • First (B  ) = {} S A B a b c {a, } {b, } First(ABc) Step 1 {a, } {b, } {a} ∪ First(Bc) Step 2 {a, } {b, } {a} ∪ {b, c}= {a,b,c} Step 3
  26. 26. S  ABc Aa A G0 First Set (2) Bb B Red : A Blue :  Step 3: • First (SABc) = {a,b,c} • First (Aa) = {a} • First (A  ) = {} • First (B  b) = {b} If no more change… • First (B  ) = {} The first set of a terminal symbol is itself Step First Set S A B a b c {a, } {b, } First(ABc) Step 1 {a, } {b, } {a} ∪ First(Bc) Step 2
  27. 27. Follow Set E…A • Follow (A) = Follow(A) ∪ First() , if  ∉ First() Follow(A) ∪First() – {} ∪Follow(E) , if  ∈ First() • We will use some examples for this operation…
  28. 28. EPrefix(E) EV Tail E…A PrefixF Follow(A) ∪ First() , if  ∉ First() G0 Follow (A) = Prefix Follow(A) ∪First() – {} ∪Follow(E) , if  ∈ First() Tail+E Tail  • Step 0: Step Follow Set E Prefix Tail   {} Step 0
  29. 29. E…A EPrefix(E) EV Tail Follow(A) ∪ First() , if  ∉ First() Follow (A) = PrefixF G0 Follow(A) ∪First() – {} ∪Follow(E) , if  ∈ First() Prefix Tail+E Tail  Step 1: • From “EPrefix(E) “, We can get… • Follow (Prefix) = Follow (Prefix) ∪First(() = Follow (Prefix) ∪{(} and • Follow (E) = Follow (E) ∪ First()) = Follow (E) ∪ {)} • From “EVTail “, We can get… • Follow (Tail) = Follow (Tail) ∪ First() = Follow (Tail) ∪ {} = Follow (Tail) ∪ Follow (E) • From “Tail+E “, We can get… • Follow (E) = Follow (E) ∪ First() = Follow (E) ∪ {} = Follow (E) ∪ Follow (Tail) Step Follow Set E Prefix Tail    Step 0 Follow(E) ∪ {)} ∪ Follow (Tail) Follow (Prefix) ∪{(} Follow (Tail) ∪ Follow (E) Step 1 ={(} = {, )} = {}
  30. 30. E…A EPrefix(E) Follow(A) ∪ First() , if  ∉ First() EV Tail Follow (A) = PrefixF G0 Follow(A) ∪First() – {} ∪Follow(E) , if  ∈ First() Prefix Tail+E Step 2: Tail  • From “EPrefix(E) “, We can get… • Follow (Prefix) = Follow (Prefix) ∪First(() = Follow (Prefix) ∪{(} and • Follow (E) = Follow (E) ∪ First()) = Follow (E) ∪ {)} • From “EVTail “, We can get… • Follow (Tail) = Follow (Tail) ∪ First() = Follow (Tail) ∪ {} = Follow (Tail) ∪ Follow (E) • From “Tail+E “, We can get… • Follow (E) = Follow (E) ∪ First() = Follow (E) ∪ {} = Follow (E) ∪ Follow (Tail) Step Follow Set E Prefix Tail    Step 0 Follow(E) ∪ {)} ∪ Follow (Tail) Follow (Prefix) ∪{(} Follow (Tail) ∪ Follow (E) Step 1 ={(} = {, )} = {} Follow(E) ∪ {)} ∪ Follow (Tail) Follow (Prefix) ∪{(} Follow (Tail) ∪ Follow (E) Step 2 ={(} = {, )} = {, }}
  31. 31. EPrefix(E) EV Tail E…A PrefixF Follow(A) ∪ First() , if  ∉ First() G0 Follow (A) = Prefix Follow(A) ∪First() – {} ∪Follow(E) , if  ∈ First() Tail+E Tail  Step 3: Step Follow Set E Prefix Tail    Step 0 Follow(E) ∪ {)} ∪ Follow (Tail) Follow (Prefix) ∪{(} Follow (Tail) ∪ Follow (E) Step 1 ={(} = {, )} = {} Follow(E) ∪ {)} ∪ Follow (Tail) Follow (Prefix) ∪{(} Follow (Tail) ∪ Follow (E) Step 2 ={(} = {, )} = {, }} Follow(E) ∪ {)} ∪ Follow (Tail) Follow (Prefix) ∪{(} Follow (Tail) ∪ Follow (E) Step 3 ={(} = {, )} = {, }}
  32. 32. Another Example….
  33. 33. E…A Follow(A) ∪ First() , if  ∉ First() Follow (A) = Follow(A) ∪First() – {} ∪Follow(E) , if  ∈ First() S  aSe SB B  bBe G0 BC C  cCe Cd
  34. 34. S  aSe E…A SB B  bBe Follow(A) ∪ First() , if  ∉ First() Follow (A) = G0 BC Follow(A) ∪First() – {} ∪Follow(E) , if  ∈ First() C  cCe Cd Step 0: Step Follow Set S B C   {} Step 0
  35. 35. S  aSe E…A SB B  bBe Follow(A) ∪ First() , if  ∉ First() Follow (A) = G0 BC Follow(A) ∪First() – {} ∪Follow(E) , if  ∈ First() C  cCe Cd Step 1: • S  aSe • BC  Follow (C)  Follow (S) = Follow (S) ∪ {e}  = Follow (C) ∪ Follow (B) • SB  Follow (B ) • C  cCe  Follow (C) = Follow (C) ∪ {e} = Follow (B) ∪ Follow (S) • B  bBe • Cd  Follow (B) = Follow (B) ∪ {e} => No Non-Terminal Step Follow Set S B C    Step 0 Follow(S) ∪ {e} Follow (B) ∪Follow(S) ∪ {e} Follow (C) ∪ Follow(B) ∪ {e} Step 1 = {e} = {, e} ={, e}
  36. 36. S  aSe E…A SB B  bBe Follow(A) ∪ First() , if  ∉ First() Follow (A) = G0 BC Follow(A) ∪First() – {} ∪Follow(E) , if  ∈ First() C  cCe Cd Step 2: • S  aSe • BC  Follow (C)  Follow (S) = Follow (S) ∪ {e}  = Follow (C) ∪ Follow (B) • SB  Follow (B ) • C  cCe  Follow (C) = Follow (C) ∪ {e} = Follow (B) ∪ Follow (S) • B  bBe • Cd  Follow (B) = Follow (B) ∪ {e} => No Non-Terminal Step Follow Set S B C    Step 0 Follow(S) ∪ {e} Follow (B) ∪Follow(S) ∪ {e} Follow (C) ∪ Follow(B) ∪ {e} Step 1 = {e} = {, e} ={, e} Follow(S) ∪ {e} Follow (B) ∪Follow(S) ∪ {e} Follow (C) ∪ Follow(B) ∪ {e} Step 2 = {, e} ={, e} ={, e}
  37. 37. S  aSe E…A SB B  bBe Follow(A) ∪ First() , if  ∉ First() Follow (A) = G0 BC Follow(A) ∪First() – {} ∪Follow(E) , if  ∈ First() C  cCe Cd Step 3: Step Follow Set S B C    Step 0 Follow(S) ∪ {e} Follow (B) ∪Follow(S) ∪ {e} Follow (C) ∪ Follow(B) ∪ {e} Step 1 = {e} = {, e} ={, e} Follow(S) ∪ {e} Follow (B) ∪Follow(S) ∪ {e} Follow (C) ∪ Follow(B) ∪ {e} Step 2 = {, e} ={, e} ={, e} Follow(S) ∪ {e} Follow (B) ∪Follow(S) ∪ {e} Follow (C) ∪ Follow(B) ∪ {e} Step 3 = {, e} ={, e} ={, e}
  38. 38. Another Example….
  39. 39. E…A Follow(A) ∪ First() , if  ∉ First() Follow (A) = Follow(A) ∪First() – {} ∪Follow(E) , if  ∈ First() S  ABc Aa A G0 Bb B
  40. 40. E…A S  ABc Follow(A) ∪ First() , if  ∉ First() Aa Follow (A) = A G0 Follow(A) ∪First() – {} ∪Follow(E) , if  ∈ First() Bb B Step 0: Step Follow Set S A B   {} Step 0
  41. 41. E…A S  ABc Follow(A) ∪ First() , if  ∉ First() Aa Follow (A) = A G0 Follow(A) ∪First() – {} ∪Follow(E) , if  ∈ First() Bb B Step 1: • S  ABc  Follow (A) • A = Follow (A) ∪ First (Bc) => No Non-Terminal • Bb = Follow (A) ∪ {b,c} • S  ABc => No Non-Terminal  Follow (B) = Follow (B) ∪ {c} • B  • Aa => No Non-Terminal  No Non-Terminal Step Follow Set S A B   {} Step 0 Follow (A) ∪ {b,c} = {b,c} Follow (B) ∪ {c} = {c} {} Step 1
  42. 42. E…A S  ABc Follow(A) ∪ First() , if  ∉ First() Aa Follow (A) = A G0 Follow(A) ∪First() – {} ∪Follow(E) , if  ∈ First() Bb B Step 2: • S  ABc  Follow (A) • A = Follow (A) ∪ First (Bc) => No Non-Terminal • Bb = Follow (A) ∪ {b,c} • S  ABc => No Non-Terminal  Follow (B) = Follow (B) ∪ {c} • B  • Aa => No Non-Terminal  No Non-Terminal Step Follow Set S A B   {} Step 0 Follow (A) ∪ {b,c} = {b,c} Follow (B) ∪ {c} = {c} {} Step 1 Follow (A) ∪ {b,c} = {b,c} Follow (B) ∪ {c} = {c} {} Step 2

×