18
Example of Constructing a Predictive Parsing Table

Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

Embed Size (px)

Citation preview

Page 1: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

Example of Constructing aPredictive Parsing Table

Page 2: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

e → t e'e' → + t e'

| 𝜀

t → f t't' → * f t'

| 𝜀

f → ( e )| x| y

Grammar: first and follow sets:

Non-Terminal

first follow

e ‘(‘, ‘x’, ‘y’ $, ‘)’

e' ‘+’, 𝜀 $, ‘)’

t ‘(‘, ‘x’, ‘y’ ‘+’, $, ‘)’

t' ‘*’, 𝜀 ‘+’, $, ‘)’

f ‘(‘, ‘x’, ‘y’ ‘*’, ‘+’, ‘)’, $

Page 3: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

Non-Terminal

Input Symbol

x y + * ( ) $

e

e'

t

t'

f

Start with an empty parsing table; the rows arenon-terminals and the columns are terminals.

Page 4: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

e → t e'

first( t e' ) = ‘(‘, ‘x’, ‘y’

Current production:

Non-Terminal

Input Symbol

x y + * ( ) $

e e→ t e'

e'

t

t'

f

for each production n → 𝛼for each a ∊ first(𝛼)

add n → 𝛼 to T[n , a]if 𝜀 ∊ first(𝛼) then

for each b ∊ follow(n)add n → 𝛼 to T[n , a]

Page 5: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

e → t e'

first( t e' ) = ‘(‘, ‘x’, ‘y’

Current production:

Non-Terminal

Input Symbol

x y + * ( ) $

e e→ t e' e→ t e'

e'

t

t'

f

for each production n → 𝛼for each a ∊ first(𝛼)

add n → 𝛼 to T[n , a]if 𝜀 ∊ first(𝛼) then

for each b ∊ follow(n)add n → 𝛼 to T[n , a]

Page 6: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

e → t e'

first( t e' ) = ‘(‘, ‘x’, ‘y’

Current production:

Non-Terminal

Input Symbol

x y + * ( ) $

e e→ t e' e→ t e' e→ t e'

e'

t

t'

f

for each production n → 𝛼for each a ∊ first(𝛼)

add n → 𝛼 to T[n , a]if 𝜀 ∊ first(𝛼) then

for each b ∊ follow(n)add n → 𝛼 to T[n , a]

Page 7: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

e' → + t e'

first( + t e' ) = ‘+’

Current production:

Non-Terminal

Input Symbol

x y + * ( ) $

e e→ t e' e→ t e' e→ t e'

e' e'→ + t e'

t

t'

f

for each production n → 𝛼for each a ∊ first(𝛼)

add n → 𝛼 to T[n , a]if 𝜀 ∊ first(𝛼) then

for each b ∊ follow(n)add n → 𝛼 to T[n , a]

Page 8: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

e' → 𝜀

follow( e' ) = $, ‘)’

Current production:

Non-Terminal

Input Symbol

x y + * ( ) $

e e→ t e' e→ t e' e→ t e'

e' e'→ + t e' e' → 𝜀

t

t'

f

for each production n → 𝛼for each a ∊ first(𝛼)

add n → 𝛼 to T[n , a]if 𝜀 ∊ first(𝛼) then

for each b ∊ follow(n)add n → 𝛼 to T[n , a]

Page 9: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

e' → 𝜀

follow( e' ) = $, ‘)’

Current production:

Non-Terminal

Input Symbol

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 → 𝛼for each a ∊ first(𝛼)

add n → 𝛼 to T[n , a]if 𝜀 ∊ first(𝛼) then

for each b ∊ follow(n)add n → 𝛼 to T[n , a]

Page 10: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

t → f t'

first( f t' ) = ‘(‘, ‘x’, ‘y’

Current production:

Non-Terminal

Input Symbol

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 → 𝛼for each a ∊ first(𝛼)

add n → 𝛼 to T[n , a]if 𝜀 ∊ first(𝛼) then

for each b ∊ follow(n)add n → 𝛼 to T[n , a]

Page 11: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

t → f t'

first( f t' ) = ‘(‘, ‘x’, ‘y’

Current production:

Non-Terminal

Input Symbol

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 → 𝛼for each a ∊ first(𝛼)

add n → 𝛼 to T[n , a]if 𝜀 ∊ first(𝛼) then

for each b ∊ follow(n)add n → 𝛼 to T[n , a]

Page 12: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

t → f t'

first( f t' ) = ‘(‘, ‘x’, ‘y’

Current production:

Non-Terminal

Input Symbol

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 → 𝛼for each a ∊ first(𝛼)

add n → 𝛼 to T[n , a]if 𝜀 ∊ first(𝛼) then

for each b ∊ follow(n)add n → 𝛼 to T[n , a]

Page 13: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

t' → 𝛼

follow( t' ) = ‘+’, $, ‘)’

Current production:

Non-Terminal

Input Symbol

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 → 𝛼for each a ∊ first(𝛼)

add n → 𝛼 to T[n , a]if 𝜀 ∊ first(𝛼) then

for each b ∊ follow(n)add n → 𝛼 to T[n , a]

Page 14: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

t' → 𝛼

follow( t' ) = ‘+’, $, ‘)’

Current production:

Non-Terminal

Input Symbol

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 → 𝛼for each a ∊ first(𝛼)

add n → 𝛼 to T[n , a]if 𝜀 ∊ first(𝛼) then

for each b ∊ follow(n)add n → 𝛼 to T[n , a]

Page 15: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

t' → 𝛼

follow( t' ) = ‘+’, $, ‘)’

Current production:

Non-Terminal

Input Symbol

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 → 𝛼for each a ∊ first(𝛼)

add n → 𝛼 to T[n , a]if 𝜀 ∊ first(𝛼) then

for each b ∊ follow(n)add n → 𝛼 to T[n , a]

Page 16: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

f → ( e )

first( ( e ) ) = ‘(’

Current production:

Non-Terminal

Input Symbol

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 → 𝛼for each a ∊ first(𝛼)

add n → 𝛼 to T[n , a]if 𝜀 ∊ first(𝛼) then

for each b ∊ follow(n)add n → 𝛼 to T[n , a]

Page 17: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

f → x

first( x ) = ‘x’

Current production:

Non-Terminal

Input Symbol

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 → 𝛼for each a ∊ first(𝛼)

add n → 𝛼 to T[n , a]if 𝜀 ∊ first(𝛼) then

for each b ∊ follow(n)add n → 𝛼 to T[n , a]

Page 18: Example of Constructing a Predictive Parsing Table with an empty parsing table; the rows are non-terminals and the columns are terminals. e → t e' first( t e') = ‘(‘, ‘x’,

f → y

first( y ) = ‘y’

Current production:

Non-Terminal

Input Symbol

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 )

for each production n → 𝛼for each a ∊ first(𝛼)

add n → 𝛼 to T[n , a]if 𝜀 ∊ first(𝛼) then

for each b ∊ follow(n)add n → 𝛼 to T[n , a]