# 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.

## Presentation on theme: "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."— Presentation transcript:

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 : 

First Set (2) E  Prefix(E) E  V Tail Prefix  F Prefix  Tail  Tail  G0G0 Red : A Blue : 

First Set (2) 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( ) = { } E  Prefix(E) E  V Tail Prefix  F Prefix  Tail  Tail  G0G0 Red : A Blue :  Step 1:

First Set (2) 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  )= { } E  Prefix(E) E  V Tail Prefix  F Prefix  Tail  Tail  G0G0 Red : A Blue :  StepFirst Set EPrefixTail()VF+ Step 1 First( Prefix(E) ) ∪ {V} {F, }{+, } Step 1:

First Set (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, }. First ( E  V Tail )= { V } First ( Prefix  F )= { F } First ( Prefix  )= { } First ( Tail  + E )= { + } First ( Tail  )= { } E  Prefix(E) E  V Tail Prefix  F Prefix  Tail  Tail  G0G0 Red : A Blue :  { F, } Step 2: StepFirst Set EPrefixTail()VF+ Step 1 First( Prefix(E) ) ∪ {V} {F, }{+, }

First Set (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  )= { } E  Prefix(E) E  V Tail Prefix  F Prefix  Tail  Tail  G0G0 Red : A Blue :  Step 2: StepFirst Set EPrefixTail()VF+ Step 1 First( Prefix(E) ) ∪ {V} {F, }{+, }

First Set (2) First ( E  Prefix (E) )= {F, (} First ( E  V Tail )= { V } First ( Prefix  F )= { F } First ( Prefix  )= { } First ( Tail  + E )= { + } First ( Tail  )= { } E  Prefix(E) E  V Tail Prefix  F Prefix  Tail  Tail  G0G0 Red : A Blue :  StepFirst Set EPrefixTail()VF+ Step 1 First( Prefix(E) ) ∪ {V} {F, }{+, } Step 2 {F, (} ∪ {V} ={F, (, V} {F, }{+, } Step 2:

First Set (2) First ( E  Prefix (E) )= { F, ( } First ( E  V Tail )= { V } First ( Prefix  F )= { F } First ( Prefix  )= { } First ( Tail  + E )= { + } First ( Tail  )= { } E  Prefix(E) E  V Tail Prefix  F Prefix  Tail  Tail  G0G0 StepFirst Set EPrefixTail()VF+ 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:

Another Example….

First Set (2) S  aSe S  B B  bBe B  C C  cCe C  d G0G0 Red : A Blue : 

S  aSe S  B B  bBe B  C C  cCe C  d G0G0 First Set (2) First ( S  a Se )= First( a ) ={ a } First ( S  B )= First( B ) First ( B  bBe )= First( b )={ b } First ( B  C )= First( C ) First ( C  c Ce )= First( c ) ={ c } First ( C  d )= First( d )={ d } Red : A Blue :  Step 1:

S  aSe S  B B  bBe B  C C  cCe C  d G0G0 First Set (2) First ( S  a Se )= { a } First ( S  B )= First( B ) First ( B  bBe )= { b } First ( B  C )= First( C ) First ( C  c Ce )= { c } First ( C  d )= { d } Red : A Blue :  StepFirst Set SBCabcd Step 1 {a} ∪ First( B ) {b} ∪ First( C ) {c, d} Step 1:

S  aSe S  B B  bBe B  C C  cCe C  d G0G0 First Set (2) First ( S  a Se )= { a } First ( S  B )= First( B ) = { b } ∪ First( C ) First ( B  bBe )= { b } First ( B  C )= First( C ) First ( C  c Ce )= { c } First ( C  d )= { d } Red : A Blue :  StepFirst Set SBCabcd Step 1 {a} ∪ First( B ) {b} ∪ First( C ) {c, d} Step 2:

S  aSe S  B B  bBe B  C C  cCe C  d G0G0 First Set (2) First ( S  a Se )= { a } First ( S  B )= { b } ∪ First( C ) First ( B  bBe )= { b } First ( B  C )= First( C ) First ( C  c Ce )= { c } First ( C  d )= { d } Red : A Blue :  Step 2: StepFirst Set SBCabcd Step 1 {a} ∪ First( B ) {b} ∪ First( C ) {c, d} Step 2 {a} ∪ {b} ∪ First( C )

S  aSe S  B B  bBe B  C C  cCe C  d G0G0 First Set (2) First ( S  a Se )= { a } First ( S  B )= { b } ∪ First( C ) First ( B  bBe )= { b } First ( B  C )= First( C ) = { c, d } First ( C  c Ce )= { c } First ( C  d )= { d } Red : A Blue :  Step 2: StepFirst Set SBCabcd Step 1 {a} ∪ First( B ) {b} ∪ First( C ) {c, d} Step 2 {a} ∪ {b} ∪ First( C )

S  aSe S  B B  bBe B  C C  cCe C  d G0G0 First Set (2) First ( S  a Se )= { a } First ( S  B )= { b } ∪ First( C ) First ( B  bBe )= { b } First ( B  C )= { c, d } First ( C  c Ce )= { c } First ( C  d )= { d } Red : A Blue :  StepFirst Set SBCabcd 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:

S  aSe S  B B  bBe B  C C  cCe C  d G0G0 First Set (2) First ( S  a Se )= { a } First ( S  B )= { b } ∪ First( C ) = { b } ∪ { c, d } First ( B  bBe )= { b } First ( B  C )= { c, d } First ( C  c Ce )= { c } First ( C  d )= { d } Red : A Blue :  Step 3: StepFirst Set SBCabcd Step 1 {a} ∪ First( B ) {b} ∪ First( C ) {c, d} Step 2 {a} ∪ {b} ∪ First( C ) {b} ∪ {c, d} = {b,c,d} {c, d}

S  aSe S  B B  bBe B  C C  cCe C  d G0G0 First Set (2) First ( S  a Se )= { a } First ( S  B )= { b, c, d } First ( B  bBe )= { b } First ( B  C )= { c, d } First ( C  c Ce )= { c } First ( C  d )= { d } Red : A Blue :  Step 3: StepFirst Set SBCabcd 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}

S  aSe S  B B  bBe B  C C  cCe C  d G0G0 First Set (2) First ( S  a Se )= { a } First ( S  B )= { b, c, d } First ( B  bBe )= { b } First ( B  C )= { c, d } First ( C  c Ce )= { c } First ( C  d )= { d } Red : A Blue :  Step 3: StepFirst Set SBCabcd 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

Another Example….

First Set (2) S  ABc A  a A  B  b B  G0G0 Red : A Blue : 

First Set (2) S  ABc A  a A  B  b B  G0G0 Red : A Blue :  First ( S  A Bc )= First( ABc ) First ( A  a )= First( a ) First ( A  )= First( ) ∪ First( ) First ( B  b )= First( b ) First ( B  )= First( ) ∪ First( ) Step 1:

First Set (2) S  ABc A  a A  B  b B  G0G0 Red : A Blue :  First ( S  A Bc )= First( ABc ) First ( A  a )= { a } First ( A  )= { } First ( B  b )= { b } First ( B  )= { } Step 1: StepFirst Set SABabc Step 1 First( ABc ) {a, }{b, }

First Set (2) S  ABc A  a A  B  b B  G0G0 Red : A Blue :  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 2: StepFirst Set SABabc Step 1 First( ABc ) {a, }{b, } Step 2 {a} ∪ First( Bc ) {a, }{b, }

First Set (2) S  ABc A  a A  B  b B  G0G0 Red : A Blue :  First ( S  ABc )= { a } ∪ First( Bc ) = { a } ∪ { b, } = { a } ∪ { b, } - { } ∪ First( c ) = { a } ∪ { b,c } First ( A  a )= { a } First ( A  )= { } First ( B  b )= { b } First ( B  )= { } Step 3: StepFirst Set SABabc Step 1 First( ABc ) {a, }{b, } Step 2 {a} ∪ First( Bc ) {a, }{b, } Step 3 {a} ∪ {b, c}= {a,b,c} {a, }{b, }

First Set (2) S  ABc A  a A  B  b B  G0G0 Red : A Blue :  First ( S  A Bc )= { a,b,c } First ( A  a )= { a } First ( A  )= { } First ( B  b )= { b } First ( B  )= { } Step 3: StepFirst Set SABabc 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

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E…A

E  …A  Follow (A) = Follow(A) ∪ First(  ), if ∉ First(  ) Follow(A) ∪ First(  ) – { } ∪ Follow(E), if ∈ First(  ) Step 0: E  Prefix(E) E  V Tail Prefix  F Prefix  Tail  Tail  G0G0 StepFollow Set EPrefixTail Step 0 { } 

Another Example….

S  aSe S  B B  bBe B  C C  cCe C  d G0G0 E  …A  Follow (A) = Follow(A) ∪ First(  ), if ∉ First(  ) Follow(A) ∪ First(  ) – { } ∪ Follow(E), if ∈ First(  )

Step 0: S  aSe S  B B  bBe B  C C  cCe C  d G0G0 E  …A  Follow (A) = Follow(A) ∪ First(  ), if ∉ First(  ) Follow(A) ∪ First(  ) – { } ∪ Follow(E), if ∈ First(  ) StepFollow Set SBC Step 0 { } 

Step 3: S  aSe S  B B  bBe B  C C  cCe C  d G0G0 E  …A  Follow (A) = Follow(A) ∪ First(  ), if ∉ First(  ) Follow(A) ∪ First(  ) – { } ∪ Follow(E), if ∈ First(  ) StepFollow Set SBC 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}

Another Example….

S  ABc A  a A  B  b B  G0G0 E  …A  Follow (A) = Follow(A) ∪ First(  ), if ∉ First(  ) Follow(A) ∪ First(  ) – { } ∪ Follow(E), if ∈ First(  )

Step 0: S  ABc A  a A  B  b B  G0G0 E  …A  Follow (A) = Follow(A) ∪ First(  ), if ∉ First(  ) Follow(A) ∪ First(  ) – { } ∪ Follow(E), if ∈ First(  ) StepFollow Set SAB Step 0 { } 

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  ABc A  a A  B  b B  G0G0 E  …A  Follow (A) = Follow(A) ∪ First(  ), if ∉ First(  ) Follow(A) ∪ First(  ) – { } ∪ Follow(E), if ∈ First(  ) StepFollow Set SAB Step 0 { }  Step 1 { } Follow (A) ∪ {b,c} = {b,c}Follow (B) ∪ {c} = {c}