42
First Set First (=A) = First(A) , if First(A) First(A) – {} ∪First(), if First(A) We will use some examples for this operation… ed : A Blue :

Compiler First Set Follow Set Brief

Embed Size (px)

Citation preview

Page 1: Compiler First Set Follow Set Brief

First Set

• First (=A) = First(A) , if ∉ First(A) First(A) – {} First(∪ ), if ∈ First(A)

We will use some examples for this operation…

Red : A Blue :

Page 2: Compiler First Set Follow Set Brief

First Set (2)

EPrefix(E)EV TailPrefixFPrefixTail+ETail

G0

Red : A Blue :

Page 3: Compiler First Set Follow Set Brief

First Set (2)

• First (EPrefix(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 (EV Tail) = First(V) = {V}• First (PrefixF) = First(F) = {F}• First (Prefix ) = First() - {} First(∪ ) = {}• First (Tail+E) = First(+) = {+}• First (Tail )= First() - {} First(∪ ) = {}

EPrefix(E)EV TailPrefixFPrefixTail+ETail

G0

Red : A Blue :

Step 1:

Page 4: Compiler First Set Follow Set Brief

First Set (2)

• First (EPrefix(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 (EV Tail) = {V}• First (PrefixF) = {F}• First (Prefix ) = {}• First (Tail+E) = {+}• First (Tail )= {}

EPrefix(E)EV TailPrefixFPrefixTail+ETail

G0

Red : A Blue :

Step First Set

E Prefix Tail ( ) V F +

Step 1 First(Prefix(E))∪{V} {F, } {+, }

Step 1:

Page 5: Compiler First Set Follow Set Brief

First Set (2)

• First (EPrefix(E))=First(Prefix(E)) ? = {Waiting operation}– In this step, we know First(Prefix). – So we replace the First(Prefix) with {F, }.

• First (EV Tail) = {V}• First (PrefixF) = {F}• First (Prefix ) = {}• First (Tail+E) = {+}• First (Tail )= {}

EPrefix(E)EV TailPrefixFPrefixTail+ETail

G0

Red : A Blue :

{F, }

Step 2:

Step First Set

E Prefix Tail ( ) V F +

Step 1 First(Prefix(E))∪{V} {F, } {+, }

Page 6: Compiler First Set Follow Set Brief

First Set (2)

• First (EPrefix(E))=First(Prefix(E)) = {F, }

= {F, } - {} ∪ First((E)) = {F, (}

• First (EV Tail) = {V}• First (PrefixF) = {F}• First (Prefix ) = {}• First (Tail+E) = {+}• First (Tail )= {}

EPrefix(E)EV TailPrefixFPrefixTail+ETail

G0

Red : A Blue :

Step 2:

Step First Set

E Prefix Tail ( ) V F +

Step 1 First(Prefix(E))∪{V} {F, } {+, }

Page 7: Compiler First Set Follow Set Brief

First Set (2)

• First (EPrefix(E))= {F, (}

• First (EV Tail) = {V}• First (PrefixF) = {F}• First (Prefix ) = {}• First (Tail+E) = {+}• First (Tail )= {}

EPrefix(E)EV TailPrefixFPrefixTail+ETail

G0

Red : A Blue :

Step First Set

E Prefix Tail ( ) V F +

Step 1 First(Prefix(E))∪{V} {F, } {+, }

Step 2 {F, (} ∪ {V} ={F, (, V} {F, } {+, }

Step 2:

Page 8: Compiler First Set Follow Set Brief

First Set (2)

• First (EPrefix(E))= {F, (}• First (EV Tail) = {V}• First (PrefixF) = {F}• First (Prefix ) = {}• First (Tail+E) = {+}• First (Tail )= {}

EPrefix(E)EV TailPrefixFPrefixTail+ETail

G0

Step First Set

E Prefix Tail ( ) V F +

Step 1 First(Prefix(E))∪{V} {F, } {+, }

Step 2 {F, (} ∪ {V} ={F, (, V} {F, } {+, }

Step 3 {F, (, V} {F, } {+, } {(} {)} {V} {F} {+}

If no more change…The first set of a terminal symbol is itself

Red : A Blue :

Step 3:

Page 9: Compiler First Set Follow Set Brief

Another Example….

Page 10: Compiler First Set Follow Set Brief

First Set (2)

S aSeS BB bBeB CC cCeC d

G0

Red : A Blue :

Page 11: Compiler First Set Follow Set Brief

S aSeS BB bBeB CC cCeC d

G0

First Set (2)

• First (SaSe) = First(a) ={a}• First (SB) = 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}

Red : A Blue :

Step 1:

Page 12: Compiler First Set Follow Set Brief

S aSeS BB bBeB CC cCeC d

G0

First Set (2)

• First (SaSe) = {a}• First (SB) = First(B) • First (B bBe) = {b}• First (B C) = First(C)• First (C cCe) = {c}• First (C d) = {d}

Red : A Blue :

Step First Set

S B C a b c d

Step 1 {a} First(∪ B) {b} First(∪ C) {c, d}

Step 1:

Page 13: Compiler First Set Follow Set Brief

S aSeS BB bBeB CC cCeC d

G0

First Set (2)

• First (SaSe) = {a}• First (SB) = First(B) = {b} First(∪ C) • First (B bBe) = {b}• First (B C) = First(C)• First (C cCe) = {c}• First (C d) = {d}

Red : A Blue :

Step First Set

S B C a b c d

Step 1 {a} First(∪ B) {b} First(∪ C) {c, d}

Step 2:

Page 14: Compiler First Set Follow Set Brief

S aSeS BB bBeB CC cCeC d

G0

First Set (2)

• First (SaSe) = {a}• First (SB) = {b} First(∪ C) • First (B bBe) = {b}• First (B C) = First(C)• First (C cCe) = {c}• First (C d) = {d}

Red : A Blue :

Step 2:

Step First Set

S B C a b c d

Step 1 {a} First(∪ B) {b} First(∪ C) {c, d}

Step 2 {a} ∪ {b} First(∪ C)

Page 15: Compiler First Set Follow Set Brief

S aSeS BB bBeB CC cCeC d

G0

First Set (2)

• First (SaSe) = {a}• First (SB) = {b} First(∪ C) • First (B bBe) = {b}• First (B C) = First(C) = {c, d}• First (C cCe) = {c}• First (C d) = {d}

Red : A Blue :

Step 2:

Step First Set

S B C a b c d

Step 1 {a} First(∪ B) {b} First(∪ C) {c, d}

Step 2 {a} ∪ {b} First(∪ C)

Page 16: Compiler First Set Follow Set Brief

S aSeS BB bBeB CC cCeC d

G0

First Set (2)

• First (SaSe) = {a}• First (SB) = {b} First(∪ C) • First (B bBe) = {b}• First (B C) = {c, d}• First (C cCe) = {c}• First (C d) = {d}

Red : A Blue :

Step First Set

S B C a b c d

Step 1 {a} First(∪ B) {b} First(∪ C) {c, d}

Step 2 {a} ∪ {b} First(∪ C) {b}∪{c, d} = {b,c,d} {c, d}

Step 2:

Page 17: Compiler First Set Follow Set Brief

S aSeS BB bBeB CC cCeC d

G0

First Set (2)

• First (SaSe) = {a}• First (SB) = {b} First(∪ C) = {b} {∪ c, d}• First (B bBe) = {b}• First (B C) = {c, d}• First (C cCe) = {c}• First (C d) = {d}

Red : A Blue :

Step 3:

Step First Set

S B C a b c d

Step 1 {a} First(∪ B) {b} First(∪ C) {c, d}

Step 2 {a} ∪ {b} First(∪ C) {b}∪{c, d} = {b,c,d} {c, d}

Page 18: Compiler First Set Follow Set Brief

S aSeS BB bBeB CC cCeC d

G0

First Set (2)

• First (SaSe) = {a}• First (SB) = {b, c, d}• First (B bBe) = {b}• First (B C) = {c, d}• First (C cCe) = {c}• First (C d) = {d}

Red : A Blue :

Step 3:

Step First Set

S B C a b c d

Step 1

{a} First(∪ B) {b} First(∪ C) {c, d}

Step 2

{a} ∪ {b} First(∪ C) {b}∪{c, d} = {b,c,d} {c, d}

Step 3

{a} ∪ {b}∪{c, d} = {a,b,c,d} {b}∪{c, d} = {b,c,d} {c, d}

Page 19: Compiler First Set Follow Set Brief

S aSeS BB bBeB CC cCeC d

G0

First Set (2)

• First (SaSe) = {a}• First (SB) = {b, c, d}• First (B bBe) = {b}• First (B C) = {c, d}• First (C cCe) = {c}• First (C d) = {d}

Red : A Blue :

Step 3:

Step First Set

S B C a b c d

Step 1 {a} First(∪ B) {b} First(∪ C) {c, d}

Step 2 {a} ∪ {b} First(∪ C) {b}∪{c, d} = {b,c,d} {c, d}

Step 3 {a} ∪ {b}∪{c, d} = {a,b,c,d} {b}∪{c, d} = {b,c,d} {c, d} {a}

{b}

{c} {d}

If no more change…The first set of a terminal symbol is itself

Page 20: Compiler First Set Follow Set Brief

Another Example….

Page 21: Compiler First Set Follow Set Brief

First Set (2)

S ABcA aA B bB

G0

Red : A Blue :

Page 22: Compiler First Set Follow Set Brief

First Set (2)S ABcA aA B bB

G0

Red : A Blue :

• First (SABc) = First(ABc)• First (Aa) = First(a) • First (A ) = First() First(∪ ) • First (B b) = First(b)• First (B ) = First() First(∪ )

Step 1:

Page 23: Compiler First Set Follow Set Brief

First Set (2)S ABcA aA B bB

G0

Red : A Blue :

• First (SABc) = First(ABc)• First (Aa) = {a} • First (A ) = {} • First (B b) = {b}• First (B ) = {}

Step 1:

Step First Set

S A B a b c

Step 1 First(ABc) {a, } {b, }

Page 24: Compiler First Set Follow Set Brief

First Set (2)S ABcA aA B bB

G0

Red : A Blue :

• First (SABc)= First(ABc) = {a, } = {a, } - {} First(∪ Bc) = {a} First(∪ Bc)• First (Aa) = {a} • First (A ) = {} • First (B b) = {b}• First (B ) = {}

Step 2:

Step First Set

S A B a b c

Step 1 First(ABc) {a, } {b, }

Step 2 {a} First(∪ Bc) {a, } {b, }

Page 25: Compiler First Set Follow Set Brief

First Set (2)S ABcA aA B bB

G0

Red : A Blue :

• First (SABc)= {a} First(∪ Bc) = {a} {∪ b, } = {a} {∪ b, } - {} First(∪ c) = {a} {∪ b,c} • First (Aa) = {a} • First (A ) = {} • First (B b) = {b}• First (B ) = {}

Step 3:

Step First Set

S A B a b c

Step 1

First(ABc) {a, } {b, }

Step 2

{a} First(∪ Bc) {a, } {b, }

Step 3

{a} ∪ {b, c}= {a,b,c}

{a, } {b, }

Page 26: Compiler First Set Follow Set Brief

First Set (2)S ABcA aA B bB

G0

Red : A Blue :

• First (SABc) = {a,b,c}• First (Aa) = {a} • First (A ) = {} • First (B b) = {b}• First (B ) = {}

Step 3:

Step First Set

S A B a b c

Step 1

First(ABc) {a, } {b, }

Step 2

{a} First(∪ Bc) {a, } {b, }

Step 3

{a} ∪ {b, c}= {a,b,c} {a, } {b, } {a} {b} {c}

If no more change…The first set of a terminal symbol is itself

Page 27: Compiler First Set Follow Set Brief

Follow Set

• Follow (A) = Follow(A) First(∪ ) , if ∉ First() Follow(A) First(∪ ) – {} Follow(∪ E) , if ∈ First()

• We will use some examples for this operation…

E…A

Page 28: Compiler First Set Follow Set Brief

E…AFollow (A) = Follow(A) First(∪ ) , if ∉ First()

Follow(A) First(∪ ) – {} Follow(∪ E) , if ∈ First()

• Step 0:

EPrefix(E)EV TailPrefixFPrefixTail+ETail

G0

Step Follow Set

E Prefix Tail

Step 0 {}

Page 29: Compiler First Set Follow Set Brief

E…AFollow (A) = Follow(A) First(∪ ) , if ∉ First()

Follow(A) First(∪ ) – {} Follow(∪ E) , if ∈ First()

Step 1:• From “EPrefix(E) “, We can get…

• Follow (Prefix) = Follow (Prefix) First(∪ () = Follow (Prefix) {∪ (} and• Follow (E) = Follow (E) First(∪ )) = Follow (E) {∪ )}

• From “EVTail “, 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)

EPrefix(E)EV TailPrefixFPrefixTail+ETail

G0

Step Follow Set

E Prefix Tail

Step 0

Step 1 Follow(E) {∪ )} Follow (∪ Tail) = {, )}

Follow (Prefix) {∪ (} ={(}

Follow (Tail) Follow (∪ E)= {}

Page 30: Compiler First Set Follow Set Brief

E…AFollow (A) = Follow(A) First(∪ ) , if ∉ First()

Follow(A) First(∪ ) – {} Follow(∪ E) , if ∈ First()

EPrefix(E)EV TailPrefixFPrefixTail+ETail

G0

Step Follow Set

E Prefix Tail

Step 0

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 2:• From “EPrefix(E) “, We can get…

• Follow (Prefix) = Follow (Prefix) First(∪ () = Follow (Prefix) {∪ (} and• Follow (E) = Follow (E) First(∪ )) = Follow (E) {∪ )}

• From “EVTail “, 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)

Page 31: Compiler First Set Follow Set Brief

E…AFollow (A) = Follow(A) First(∪ ) , if ∉ First()

Follow(A) First(∪ ) – {} Follow(∪ E) , if ∈ First()

Step 3:

EPrefix(E)EV TailPrefixFPrefixTail+ETail

G0

Step Follow Set

E Prefix Tail

Step 0

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 Follow(E) {∪ )} Follow (∪ Tail) = {, )}

Follow (Prefix) {∪ (} ={(}

Follow (Tail) Follow (∪ E)= {, }}

Page 32: Compiler First Set Follow Set Brief

Another Example….

Page 33: Compiler First Set Follow Set Brief

S aSeS BB bBeB CC cCeC d

G0

E…AFollow (A) = Follow(A) First(∪ ) , if ∉ First()

Follow(A) First(∪ ) – {} Follow(∪ E) , if ∈ First()

Page 34: Compiler First Set Follow Set Brief

Step 0:

S aSeS BB bBeB CC cCeC d

G0

E…AFollow (A) = Follow(A) First(∪ ) , if ∉ First()

Follow(A) First(∪ ) – {} Follow(∪ E) , if ∈ First()

Step Follow Set

S B C

Step 0 {}

Page 35: Compiler First Set Follow Set Brief

Step 1:

S aSeS BB bBeB CC cCeC d

G0

• S aSeÞ Follow (S) = Follow (S) {∪ e}

• S BÞ Follow (B ) = Follow (B) Follow (∪ S)

• B bBeÞ Follow (B) = Follow (B) {∪ e}

• B CÞ Follow (C) Þ = Follow (C) Follow (∪ B)

• C cCeÞ Follow (C) = Follow (C) {∪ e}

• C d=> No Non-Terminal

E…AFollow (A) = Follow(A) First(∪ ) , if ∉ First()

Follow(A) First(∪ ) – {} Follow(∪ E) , if ∈ First()

Step Follow Set

S B C

Step 0

Step 1 Follow(S) {∪ e}= {, e}

Follow (B) Follow∪ (S) {∪ e}={, e}

Follow (C) Follow∪ (B) {∪ e}= {e}

Page 36: Compiler First Set Follow Set Brief

Step 2:

S aSeS BB bBeB CC cCeC d

G0

• S aSeÞ Follow (S) = Follow (S) {∪ e}

• S BÞ Follow (B ) = Follow (B) Follow (∪ S)

• B bBeÞ Follow (B) = Follow (B) {∪ e}

• B CÞ Follow (C) Þ = Follow (C) Follow (∪ B)

• C cCeÞ Follow (C) = Follow (C) {∪ e}

• C d=> No Non-Terminal

E…AFollow (A) = Follow(A) First(∪ ) , if ∉ First()

Follow(A) First(∪ ) – {} Follow(∪ E) , if ∈ First()

Step Follow Set

S B C

Step 0

Step 1 Follow(S) {∪ e}= {, e}

Follow (B) Follow∪ (S) {∪ e}={, e}

Follow (C) Follow∪ (B) {∪ e}= {e}

Step 2 Follow(S) {∪ e}= {, e}

Follow (B) Follow∪ (S) {∪ e}={, e}

Follow (C) Follow∪ (B) {∪ e}={, e}

Page 37: Compiler First Set Follow Set Brief

Step 3:

S aSeS BB bBeB CC cCeC d

G0

E…AFollow (A) = Follow(A) First(∪ ) , if ∉ First()

Follow(A) First(∪ ) – {} Follow(∪ E) , if ∈ First()

Step Follow Set

S B C

Step 0

Step 1 Follow(S) {∪ e}= {, e}

Follow (B) Follow∪ (S) {∪ e}={, e}

Follow (C) Follow∪ (B) {∪ e}= {e}

Step 2 Follow(S) {∪ e}= {, e}

Follow (B) Follow∪ (S) {∪ e}={, e}

Follow (C) Follow∪ (B) {∪ e}={, e}

Step 3 Follow(S) {∪ e}= {, e}

Follow (B) Follow∪ (S) {∪ e}={, e}

Follow (C) Follow∪ (B) {∪ e}={, e}

Page 38: Compiler First Set Follow Set Brief

Another Example….

Page 39: Compiler First Set Follow Set Brief

S ABcA aA B bB

G0

E…AFollow (A) = Follow(A) First(∪ ) , if ∉ First()

Follow(A) First(∪ ) – {} Follow(∪ E) , if ∈ First()

Page 40: Compiler First Set Follow Set Brief

Step 0:

S ABcA aA B bB

G0

E…AFollow (A) = Follow(A) First(∪ ) , if ∉ First()

Follow(A) First(∪ ) – {} Follow(∪ E) , if ∈ First()

Step Follow Set

S A B

Step 0 {}

Page 41: Compiler First Set Follow Set Brief

Step 1:• S ABc

Þ Follow (A) = Follow (A) First (∪ Bc) = Follow (A) {∪ b,c}

• S ABcÞ Follow (B) = Follow (B) {∪ c}

• A aÞ No Non-Terminal

• A => No Non-Terminal

• B b=> No Non-Terminal

• B => No Non-Terminal

S ABcA aA B bB

G0

E…AFollow (A) = Follow(A) First(∪ ) , if ∉ First()

Follow(A) First(∪ ) – {} Follow(∪ E) , if ∈ First()

Step Follow Set

S A B

Step 0 {}

Step 1 {} Follow (A) {∪ b,c} = {b,c}

Follow (B) {∪ c} = {c}

Page 42: Compiler First Set Follow Set Brief

Step 2:• S ABc

Þ Follow (A) = Follow (A) First (∪ Bc) = Follow (A) {∪ b,c}

• S ABcÞ Follow (B) = Follow (B) {∪ c}

• A aÞ No Non-Terminal

• A => No Non-Terminal

• B b=> No Non-Terminal

• B => No Non-Terminal

S ABcA aA B bB

G0

E…AFollow (A) = Follow(A) First(∪ ) , if ∉ First()

Follow(A) First(∪ ) – {} Follow(∪ E) , if ∈ First()

Step Follow Set

S A B

Step 0 {}

Step 1 {} Follow (A) {∪ b,c} = {b,c}

Follow (B) {∪ c} = {c}

Step 2 {} Follow (A) {∪ b,c} = {b,c}

Follow (B) {∪ c} = {c}