

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Computing FIRST Sets
Here is the algorithm for computing the FIRST sets.
FIRST(b) = {b}
Add FIRST(A 1 ) − {ε} to FIRST(X), stop if ε ∉ FIRST(A 1 ) Add FIRST(A 2 ) − {ε} to FIRST(X), stop if ε ∉ FIRST(A 2 ) ..... Add FIRST(A (^) n) − {ε} to FIRST(X), stop if ε ∉ FIRST(A (^) n) Add ε to FIRST(X)
This strategy is encoded in the following procedure
for each a ∈ ( T ∪ ε ) FIRST( a )? { a } for each A ∈ NT FIRST( A )? Ø while ( FIRST sets are still changing) for each A? β 1 β 2 ... βk ∈ P FIRST( A )? FIRST( A ) ∪ (FIRST( β 1 ) – {ε}) i? 1 while ( ε ∈ FIRST( βi ) and i = k-1 ) FIRST( A )? FIRST( A ) ∪ (FIRST( βi+1 ) – {ε}) i? i+ if (i == k and ε ∈ FIRST( βk ) FIRST( A )? FIRST( A ) ∪ {ε}
Example : consider the expression grammar again
1 E? T E' 2 E'? + T E' 3 | ε 4 T? F T' 5 T'? * F T' 6 | ε 7 F? ( E ) 8 | Id
FIRST(id) = { id } FIRST('(' ) = { ( } FIRST(‘+' ) = { + }
FIRST( E ) = {FIRST( T ) − { ε } } FIRST( T ) = {FIRST( F ) − { ε } } FIRST( F ) = {FIRST( '(') − { ε } } = { ( }
FIRST( F ) = { '(' } + {FIRST( id ) − { ε } } = { ( , id}
FIRST( E' ) = { +, ε } FIRST( T' ) = { ∗ , ε }
Thus, FIRST( E ) = FIRST( T ) = FIRST( F ) = { (, id } FIRST( E' ) = { +, ε } FIRST( T' ) = { ∗ , ε }
FOLLOW Sets
Definition:
FOLLOW(X) = { b | S? ∗^ β Xb ω }
Computing FOLLOW Sets: