You are on page 1of 18

Example of Constructing a

Predictive Parsing Table


Grammar: first and follow sets:
Non-
e → t e' Terminal
first follow

e' → + t e' e ‘(‘, ‘x’, ‘y’ $, ‘)’


‘+’, 𝜀
𝜀
e' $, ‘)’
| t ‘(‘, ‘x’, ‘y’ ‘+’, $, ‘)’
‘*’, 𝜀
t → f t' t'
f ‘(‘, ‘x’, ‘y’
‘+’, $, ‘)’
‘*’, ‘+’, ‘)’, $
t' → * f t'
| 𝜀
f → (e)
| x
| y
Start with an empty parsing table; the rows are
non-terminals and the columns are terminals.
Non- Input Symbol
Terminal x y + * ( ) $
e
e'
t
t'
f
for each production n → 𝛼 Current production:
for each a ∊ first(𝛼)
add n → 𝛼 to T[n , a] e → t e'
if 𝜀 ∊ first(𝛼) then
for each b ∊ follow(n)
first( t e' ) = ‘(‘, ‘x’, ‘y’
add n → 𝛼 to T[n , a]

Non- Input Symbol


Terminal x y + * ( ) $
e e→ t e'
e'
t
t'
f
for each production n → 𝛼 Current production:
for each a ∊ first(𝛼)
add n → 𝛼 to T[n , a] e → t e'
if 𝜀 ∊ first(𝛼) then
for each b ∊ follow(n)
first( t e' ) = ‘(‘, ‘x’, ‘y’
add n → 𝛼 to T[n , a]

Non- Input Symbol


Terminal x y + * ( ) $
e e→ t e' e→ t e'
e'
t
t'
f
for each production n → 𝛼 Current production:
for each a ∊ first(𝛼)
add n → 𝛼 to T[n , a] e → t e'
if 𝜀 ∊ first(𝛼) then
for each b ∊ follow(n)
first( t e' ) = ‘(‘, ‘x’, ‘y’
add n → 𝛼 to T[n , a]

Non- Input Symbol


Terminal x y + * ( ) $
e e→ t e' e→ t e' e→ t e'
e'
t
t'
f
for each production n → 𝛼 Current production:
for each a ∊ first(𝛼)
add n → 𝛼 to T[n , a] e' → + t e'
if 𝜀 ∊ first(𝛼) then
for each b ∊ follow(n)
first( + t e' ) = ‘+’
add n → 𝛼 to T[n , a]

Non- Input Symbol


Terminal x y + * ( ) $
e e→ t e' e→ t e' e→ t e'
e' e'→ + t e'
t
t'
f
for each production n → 𝛼 Current production:
for each a ∊ first(𝛼)
add n → 𝛼 to T[n , a] e' → 𝜀
if 𝜀 ∊ first(𝛼) then
for each b ∊ follow(n)
follow( e' ) = $, ‘)’
add n → 𝛼 to T[n , a]

Non- Input Symbol


Terminal x y + * ( ) $
e e→ t e' e→ t e' e→ t e'
e' e'→ + t e' e' → 𝜀
t
t'
f
for each production n → 𝛼 Current production:
for each a ∊ first(𝛼)
add n → 𝛼 to T[n , a] e' → 𝜀
if 𝜀 ∊ first(𝛼) then
for each b ∊ follow(n)
follow( e' ) = $, ‘)’
add n → 𝛼 to T[n , a]

Non- Input Symbol


Terminal x y + * ( ) $
e e→ t e' e→ t e' e→ t e'
e' e'→ + t e' e' → 𝜀 e' → 𝜀
t
t'
f
for each production n → 𝛼 Current production:
for each a ∊ first(𝛼)
add n → 𝛼 to T[n , a] t → f t'
if 𝜀 ∊ first(𝛼) then
for each b ∊ follow(n)
first( f t' ) = ‘(‘, ‘x’, ‘y’
add n → 𝛼 to T[n , a]

Non- Input Symbol


Terminal x y + * ( ) $
e e→ t e' e→ t e' e→ t e'
e' e'→ + t e' e' → 𝜀 e' → 𝜀
t t→ f t'
t'
f
for each production n → 𝛼 Current production:
for each a ∊ first(𝛼)
add n → 𝛼 to T[n , a] t → f t'
if 𝜀 ∊ first(𝛼) then
for each b ∊ follow(n)
first( f t' ) = ‘(‘, ‘x’, ‘y’
add n → 𝛼 to T[n , a]

Non- Input Symbol


Terminal x y + * ( ) $
e e→ t e' e→ t e' e→ t e'
e' e'→ + t e' e' → 𝜀 e' → 𝜀
t t→ f t' t→ f t'
t'
f
for each production n → 𝛼 Current production:
for each a ∊ first(𝛼)
add n → 𝛼 to T[n , a] t → f t'
if 𝜀 ∊ first(𝛼) then
for each b ∊ follow(n)
first( f t' ) = ‘(‘, ‘x’, ‘y’
add n → 𝛼 to T[n , a]

Non- Input Symbol


Terminal x y + * ( ) $
e e→ t e' e→ t e' e→ t e'
e' e’→ + t e' e' → 𝜀 e' → 𝜀
t t→ f t' t→ f t' t→ f t'
t'
f
for each production n → 𝛼 Current production:
for each a ∊ first(𝛼)
add n → 𝛼 to T[n , a] t' → 𝛼
if 𝜀 ∊ first(𝛼) then
for each b ∊ follow(n)
follow( t' ) = ‘+’, $, ‘)’
add n → 𝛼 to T[n , a]

Non- Input Symbol


Terminal x y + * ( ) $
e e→ t e' e→ t e' e→ t e'
e' e’→ + t e' e' → 𝜀 e' → 𝜀
t t→ f t' t→ f t' t→ f t'
t' t' → 𝜀 t'→ * f t'
f
for each production n → 𝛼 Current production:
for each a ∊ first(𝛼)
add n → 𝛼 to T[n , a] t' → 𝛼
if 𝜀 ∊ first(𝛼) then
for each b ∊ follow(n)
follow( t' ) = ‘+’, $, ‘)’
add n → 𝛼 to T[n , a]

Non- Input Symbol


Terminal x y + * ( ) $
e e→ t e' e→ t e' e→ t e'
e' e'→ + t e' e' → 𝜀 e' → 𝜀
t t→ f t' t→ f t' t→ f t'
t' t' → 𝜀 t'→ * f t' t' → 𝜀
f
for each production n → 𝛼 Current production:
for each a ∊ first(𝛼)
add n → 𝛼 to T[n , a] t' → 𝛼
if 𝜀 ∊ first(𝛼) then
for each b ∊ follow(n)
follow( t' ) = ‘+’, $, ‘)’
add n → 𝛼 to T[n , a]

Non- Input Symbol


Terminal x y + * ( ) $
e e→ t e' e→ t e' e→ t e'
e' e'→ + t e' e' → 𝜀 e' → 𝜀
t t→ f t' t→ f t' t→ f t'
t' t' → 𝜀 t'→ * f t' t' → 𝜀 t' → 𝜀
f
for each production n → 𝛼 Current production:
for each a ∊ first(𝛼)
add n → 𝛼 to T[n , a] f → (e)
if 𝜀 ∊ first(𝛼) then
for each b ∊ follow(n)
first( ( e ) ) = ‘(’
add n → 𝛼 to T[n , a]

Non- Input Symbol


Terminal x y + * ( ) $
e e→ t e' e→ t e' e→ t e'
e' e'→ + t e' e' → 𝜀 e' → 𝜀
t t→ f t' t→ f t' t→ f t'
t' t' → 𝜀 t'→ * f t' t' → 𝜀 t' → 𝜀
f f→(e)
for each production n → 𝛼 Current production:
for each a ∊ first(𝛼)
add n → 𝛼 to T[n , a] f → x
if 𝜀 ∊ first(𝛼) then
for each b ∊ follow(n)
first( x ) = ‘x’
add n → 𝛼 to T[n , a]

Non- Input Symbol


Terminal x y + * ( ) $
e e→ t e' e→ t e' e→ t e'
e' e'→ + t e' e' → 𝜀 e' → 𝜀
t t→ f t' t→ f t' t→ f t'
t' t' → 𝜀 t'→ * f t' t' → 𝜀 t' → 𝜀
f f→x f→(e)
for each production n → 𝛼 Current production:
for each a ∊ first(𝛼)
add n → 𝛼 to T[n , a] f → y
if 𝜀 ∊ first(𝛼) then
for each b ∊ follow(n)
first( y ) = ‘y’
add n → 𝛼 to T[n , a]

Non- Input Symbol


Terminal x y + * ( ) $
e e→ t e' e→ t e' e→ t e'
e' e'→ + t e' e' → 𝜀 e' → 𝜀
t t→ f t' t→ f t' t→ f t'
t' t' → 𝜀 t'→ * f t' t' → 𝜀 t' → 𝜀
f f→x f→y f→(e)

You might also like