You are on page 1of 42

Red : A

Blue :

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 :

G0

First Set (2)


EPrefix(E)
EV Tail
PrefixF
Prefix
Tail
Tail

Red : A

Blue :

First Set (2)

Step 1:

G0

EPrefix(E)
EV Tail
PrefixF
Prefix
Tail
Tail

First ( EPrefix (E))=First( Prefix (E) ) ? = { Waiting operation }


We dont know First(Prefix) at this moment.
So we keep the operation of First(Prefix(E)) until we know First(Pr
efix).

First
First
First
First
First

( EV Tail ) =
( PrefixF) =
( Prefix )=
( Tail+E) =
( Tail ) =

First( V ) = { V }
First( F) = { F}
First( ) - { } First( ) = { }
First( +) = { +}
First( ) - { } First( ) = { }

Red : A

Blue :

First Set (2)

EPrefix(E)
EV Tail
PrefixF
Prefix
Tail
Tail

G0

Step 1:

First ( EPrefix (E))=First( Prefix (E) ) ? = { Waiting operation }


We dont know First(Prefix) at this moment.
So we keep the operation of First(Prefix(E)) until we know First(Pr
efix).

First
First
First
First
First

( EV Tail ) =
( PrefixF) =
( Prefix )=
( Tail+E) =
( Tail ) =

{ V}
{ F}
{ }
{ +}
{ }

Step

Step 1

First Set
E

Pref
x

Tail

First(Prefix(E)){V

{F,

{+,

Red : A

Blue :

First Set (2)

EPrefix(E)
EV Tail
PrefixF
Prefix
Tail
Tail

G0

Step 2:

First ( EPrefix (E))=First( Prefix (E)) ? = { Waiting operati


on }

In this step, we know First( Prefix ).


So we replace the First( Prefix ) with { F, }.

First
First
First
First
First

( EV Tail) = { V }
( PrefixF) = { F }
( Prefix )= { }
( Tail+ E) = { + }
( Tail ) = Step
{ }
Step 1

{F, }

First Set
E

Pref
x

Tail

First(Prefix(E)){V

{F,

{+,

Red : A

Blue :

First Set (2)

EPrefix(E)
EV Tail
PrefixF
Prefix
Tail
Tail

G0

Step 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 ) = { }
Step

Step 1

First Set
E

Pref
x

Tail

First(Prefix(E)){V

{F,

{+,

Red : A

Blue :

Step 2:

First
First
First
First
First
First

First Set (2)

G0

EPrefix(E)
EV Tail
PrefixF
Prefix
Tail
Tail

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


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

Step 1

First Set
E

Pref
x

Tail

First(Prefix(E))
{V}

{F,
}

{+,
}

Red : A

Blue :

Step 3:

First
First
First
First
First
First

First Set (2)

G0

EPrefix(E)
EV Tail
PrefixF
Prefix
Tail
Tail

( EPrefix (E))= { F, (}
( EV Tail) = { V }
( PrefixF) = { F }
( Prefix )=If{ no
} more change
( Tail+ E) =The
{ + } first set of a termin
( Tail ) =symbol
{ }
is itself
Step

First Set
E

Pref
x

Tail

Step 1

First(Prefix(E)){
V}

{F,
}

{+,
}

Step 2

{F, (} {V} ={F,

{F,

{+,

Another Example.

Red : A

Blue :

G0

First Set (2)


S aSe
SB
B bBe
BC
C cCe
C d

Red : A

Blue :

Step 1:

First
First
First
First
First
First

First Set (2)

( SaSe) =
( SB) =
( B bBe)
( B C) =
( C cCe) =
( C d ) =

First( a) ={ a}
First( B)
= First( b)={ b}
First( C )
First( c ) ={ c}
First( d)={ d}

G0

S aSe
SB
B bBe
BC
C cCe
C d

Red : A

Blue :

Step 1:

First
First
First
First
First
First

G0

First Set (2)

( SaSe) =
( SB) =
( B bBe)
( B C) =
( C cCe) =
( C d ) =

S aSe
SB
B bBe
BC
C cCe
C d

{ a}
First( B)
= { b}
First( C )
{ c}
{ d}
Step

First Set
S

Step 1

C
{c,

Red : A

Blue :

Step 2:

First
First
First
First
First
First

G0

First Set (2)

( SaSe) =
( SB) =
( B bBe)
( B C) =
( C cCe) =
( C d ) =

S aSe
SB
B bBe
BC
C cCe
C d

{ a}
First( B) = { b}First( C )
= { b}
First( C )
{ c}
{ d}
Step

First Set
S

Step 1

C
{c,

Red : A

Blue :

Step 2:

First
First
First
First
First
First

G0

First Set (2)

( SaSe) =
( SB) =
( B bBe)
( B C) =
( C cCe) =
( C d ) =

S aSe
SB
B bBe
BC
C cCe
C d

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

Step

Step 1

First Set
S

{a}First(B)

{b}First(C)

{c,
d}

a b c

Red : A

Blue :

Step 2:

First
First
First
First
First
First

G0

First Set (2)

( SaSe) =
( SB) =
( B bBe)
( B C) =
( C cCe) =
( C d ) =

S aSe
SB
B bBe
BC
C cCe
C d

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

Step

Step 1

First Set
S

{a}First(B)

{b}First(C)

{c,
d}

a b c

Red : A

Blue :

Step 2:

First
First
First
First
First
First

G0

First Set (2)

( SaSe) =
( SB) =
( B bBe)
( B C) =
( C cCe) =
( C d ) =

S aSe
SB
B bBe
BC
C cCe
C d

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

Step

Step 1

First Set
S

{a}First(B)

{b}First(C)

{c,
d}

a b c

Red : A

Blue :

Step 3:

First
First
First
First
First
First

G0

First Set (2)

( SaSe) =
( SB) =
( B bBe)
( B C) =
( C cCe) =
( C d ) =

S aSe
SB
B bBe
BC
C cCe
C d

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

Step

Step 1

First Set
S

{a}First(B)

{b}First(C)

{c,
d}

a b c

Red : A

Blue :

Step 3:

First
First
First
First
First
First

G0

First Set (2)

( SaSe) =
( SB) =
( B bBe)
( B C) =
( C cCe) =
( C d ) =

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

Ste
p

First Set
S

Step
1

S aSe
SB
B bBe
BC
C cCe
C d

{a}First(B)

B
{b}First(C)

C
{c,
d}

a b c

Red : A

Blue :

Step 3:

First
First
First
First
First
First

G0

First Set (2)

( SaSe) =
( SB) =
( B bBe)
( B C) =
( C cCe) =
( C d ) =

S aSe
SB
B bBe
BC
C cCe
C d

{ a}
{ b, c, d }
= { b}
{ c,Ifd }no more change
{ cThe
}
first set of a termin
{ dsymbol
}
is itself

Step

First Set
S

Step 1

{a}First(B)

{b}First(C)

{c,
d}

Step 2

{a} {b}First(C)

{b}{c, d} =

{c,

Another Example.

Red : A

Blue :

G0

First Set (2)


S ABc
Aa
A
Bb
B

Red : A

Blue :

Step 1:

First
First
First
First
First

First Set (2)

( SA Bc) =
( Aa) =
( A ) =
( B b) =
( B ) =

First( ABc )
First( a)
First( )First( )
First( b)
First( )First( )

G0

S ABc
Aa
A
Bb
B

Red : A

Blue :

Step 1:

First
First
First
First
First

First Set (2)

( SA Bc) =
( Aa) =
( A ) =
( B b) =
( B ) =

S ABc
Aa
A
Bb
B

G0

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

Step

First Set
S

Step 1
First(ABc)

A
{a, }

B
{b, }

Red : A

Blue :

First Set (2)

G0

S ABc
Aa
A
Bb
B

Step 2:

First ( SABc )= First( ABc ) = { a, }


= { a, } - { } First( Bc )
= { a} First( Bc )
First ( Aa) = { a}
First ( A ) = { }
First ( B b) = { b}
First ( B ) = { } Step
First Set
S
Step 1
First(ABc)
Step 2

{a}

A
{a, }

B
{b, }

Red : A

Blue :

First Set (2)

Step 3:

First ( SABc )

First
First
First
First

( Aa)
( A )
( B b)
( B )

=
=
=
=

G0

S ABc
Aa
A
Bb
B

= { a } First( Bc )
= { a } { b, }
= { a } { b, } - { } First( c )
= { a } { b,c }
{ a}
{ }
{ b} Step
First Set
S
A
B
a
b
c
{ }
Step
1
Step

First(ABc)
{a}

{a,
}

{b,
}

{a,

{b,

Red : A

Blue :

Step 3:

First
First
First
First
First

First Set (2)

( SA Bc) =
( Aa) =
( A ) =
( B b) =
( B ) =

S ABc
Aa
A
Bb
B

G0

{ a,b,c }
{ a}
{ }
{ b}
If no more change
{ The
}
first set of a termin
symbol is itself
Ste
p

First Set
S

Step
1
Step

First(ABc)
{a} First(Bc)

{a, }

{b, }

{a, }

{b, }

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

EA
Follow (A) =
First()

Follow(A) First() , if First()


Follow(A) First() {} Follow(E) , if

Step 0:
Step

Step 0

Follow Set
E

Pref
x

Tail

{}

G0

EPrefix(E)
EV Tail
PrefixF
Prefix
Tail
Tail

EA
Follow (A) =

Follow(A) First() , if First()

G0

Follow(A) First() {} Follow(E) , if First()

Step 1:

EPrefix(E)
EV Tail
PrefixF
Prefix
Tail
Tail

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)
Step

Follow Set
E

Prefx

Tail

Step 0

Step 1

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

Follow (Prefix)
{(}
={(}

Follow (Tail)
Follow (E)
= {}

EA
Follow (A) =

Follow(A) First() , if First()


G0

Follow(A) First() {} Follow(E) , if First()

Step 2:

EPrefix(E)
EV Tail
PrefixF
Prefix
Tail
Tail

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)
Step

Follow Set
E

Prefx

Tail

Step 0

Step 1

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

Follow (Prefix)
{(}
={(}

Follow (Tail)
Follow (E)
= {}

Step 2

Follow(E) {)}

Follow (Prefix)

Follow (Tail)

EA
Follow (A) =

G0

Follow(A) First() , if First()


Follow(A) First() {} Follow(E) , if First()

Step 3:
Step

Follow Set
E

Prefx

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)
= {, }}

EPrefix(E)
EV Tail
PrefixF
Prefix
Tail
Tail

Another Example.

EA
Follow (A) =
First()

Follow(A) First() , if First()


Follow(A) First() {} Follow(E) , if

G0

S aSe
SB
B bBe
BC
C cCe
C d

EA
Follow (A) =
First()

Follow(A) First() , if First()


Follow(A) First() {} Follow(E) , if

Step 0:
Step

Step 0

Follow Set
S

{}

G0

S aSe
SB
B bBe
BC
C cCe
C d

EA
Follow (A) =
First()

Follow(A) First() , if First()


Follow(A) First() {} Follow(E) , if

G0

Step 1:

S aSe
Follow (S) = Follow (S) {e}
SB
Follow (B )
= Follow (B) Follow (S)
B bBe
Follow (B) = Follow (B) {e}
BC

Step
Follow Set
Follow (C)
S (C) Follow B(B)
= Follow
CStep
0cCe

Follow
Step
1
Follow(S)
Follow
Follow (B)
(C) =
(C)Follow(S)
{e}
{e}
{e}
Cd
= {, e}
={, e}
=> No Non-Terminal

S aSe
SB
B bBe
BC
C cCe
C d

Follow (C) Follow(B)


{e}
= {e}

EA
Follow (A) =
First()

Follow(A) First() , if First()


Follow(A) First() {} Follow(E) , if

G0

Step 2:

S aSe
Follow (S) = Follow (S) {e}
SB
Follow (B )
= Follow (B) Follow (S)
B bBe
Follow (B) = Follow (B) {e}
BC

Step
Follow Set
Follow (C)
S (C) Follow B(B)
= Follow
CStep
0cCe

Follow
Step
1
Follow(S)
Follow
Follow (B)
(C) =
(C)Follow(S)
{e}
{e}
{e}
Cd
= {, e}
={, e}
=> 2No Follow(S)
Non-Terminal
Step

Follow (B) Follow(S)

S aSe
SB
B bBe
BC
C cCe
C d

Follow (C) Follow(B)


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

EA
Follow (A) =
First()

Follow(A) First() , if First()


Follow(A) First() {} Follow(E) , if

Step 3:

Step

G0

S aSe
SB
B bBe
BC
C cCe
C d

Follow Set
S

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.

EA
Follow (A) =
First()

Follow(A) First() , if First()


Follow(A) First() {} Follow(E) , if

G0

S ABc
Aa
A
Bb
B

EA
Follow (A) =
First()

Follow(A) First() , if First()


Follow(A) First() {} Follow(E) , if

Step 0:
Step

Step 0

Follow Set
S

{}

G0

S ABc
Aa
A
Bb
B

EA
Follow (A) =
First()

Follow(A) First() , if First()


Follow(A) First() {} Follow(E) , if

Step 1:

S ABc
Follow (A)
= Follow (A) First (Bc)
= Follow (A) {b,c}
S ABc
Follow (B) = Follow (B) {c}
Aa
No Non-Terminal
Step
A
S
=> No Non-Terminal
Step 0
{}
Bb
Step 1
{}
=> No Non-Terminal
B

Follow Set
A

Follow (A)
{b,c} = {b,c}

Follow (B)
{c} = {c}

G0

S ABc
Aa
A
Bb
B

EA
Follow (A) =
First()

Follow(A) First() , if First()


Follow(A) First() {} Follow(E) , if

Step 2:

S ABc
Follow (A)
= Follow (A) First (Bc)
= Follow (A) {b,c}
S ABc
Follow (B) = Follow (B) {c}
Aa
No Non-Terminal
Step
A
S
=> No Non-Terminal
Step 0
{}
Bb
Step 1
{}
=> No Non-Terminal
B

Follow Set
A

Follow (A)
{b,c} = {b,c}

Follow (B)
{c} = {c}

G0

S ABc
Aa
A
Bb
B

You might also like