Compiler Construction Sohail Aslam Lecture 18 Computing FIRST

  • Slides: 26
Download presentation
Compiler Construction Sohail Aslam Lecture 18

Compiler Construction Sohail Aslam Lecture 18

Computing FIRST Sets For terminal symbols, FIRST(b) = {b} 2

Computing FIRST Sets For terminal symbols, FIRST(b) = {b} 2

Computing FIRST Sets For all productions X → A 1. . . An §

Computing FIRST Sets For all productions X → A 1. . . An § Add FIRST(A 1) {e} to FIRST(X), stop if e FIRST(A 1) § Add FIRST(A 2) {e} to FIRST(X), stop if e FIRST(A 2) 3

Computing FIRST Sets For all productions X → A 1. . . An §

Computing FIRST Sets For all productions X → A 1. . . An § Add FIRST(A 1) {e} to FIRST(X), stop if e FIRST(A 1) § Add FIRST(A 2) {e} to FIRST(X), stop if e FIRST(A 2) 4

Computing FIRST Sets For all productions X → A 1. . . An §

Computing FIRST Sets For all productions X → A 1. . . An § Add FIRST(A 1) {e} to FIRST(X), stop if e FIRST(A 1) § Add FIRST(A 2) {e} to FIRST(X), stop if e FIRST(A 2) 5

Computing FIRST Sets §. . . § Add FIRST(An) {e} to FIRST(X), stop if

Computing FIRST Sets §. . . § Add FIRST(An) {e} to FIRST(X), stop if e FIRST(An) § Add e to FIRST(X) 6

for each a ( T e ) FIRST(a) ← {a} for each A NT

for each a ( T e ) FIRST(a) ← {a} for each A NT FIRST(A) ← Ø while ( FIRST sets are still changing) for each A → b 1 b 2. . . bk P FIRST(A) ←FIRST(A) (FIRST(b 1) – {e}) i ← 1 while ( e FIRST(bi) and i ≤ k-1 ) FIRST(A) ←FIRST(A) (FIRST(bi+1) – {e}) i ← i+1 if (i == k and e FIRST(bk) 7

Computing FIRST Sets the expression grammar again 1 2 3 4 5 6 7

Computing FIRST Sets the expression grammar again 1 2 3 4 5 6 7 8 E E' T T' F → → | → | T E' + T E' e F T' * F T' e (E ) id 8

Computing FIRST Sets E E' T T' F → → → T E' +T

Computing FIRST Sets E E' T T' F → → → T E' +T E' | e F T' * F T' | e ( E ) | id FIRST(id) = { id } FIRST('(' ) = { ( } FIRST(‘+' ) = { + } 9

Computing FIRST Sets E E' T T' F → → → T E' +T

Computing FIRST Sets E E' T T' F → → → T E' +T E' | e F T' * F T' | e ( E ) | id FIRST(E ) = {FIRST(T ) {e} } FIRST(T ) = {FIRST(F ) {e} } FIRST(F ) = {FIRST( '(') {e} } ={(} 10

Computing FIRST Sets E E' T T' F → → → T E' +T

Computing FIRST Sets E E' T T' F → → → T E' +T E' | e F T' * F T' | e ( E ) | id FIRST(F ) = { '(' } + {FIRST( id ) {e} } = { ( , id} 11

Computing FIRST Sets E E' T T' F → → → T E' +T

Computing FIRST Sets E E' T T' F → → → T E' +T E' | e F T' * F T' | e ( E ) | id FIRST(E' ) = { +, e } FIRST(T' ) = { , e } 12

Computing FIRST Sets Thus, § FIRST(E ) = FIRST(T ) = FIRST(F ) =

Computing FIRST Sets Thus, § FIRST(E ) = FIRST(T ) = FIRST(F ) = { (, id } § FIRST(E' ) = { +, e } § FIRST(T' ) = { , e } 13

FOLLOW Sets Definition: FOLLOW(X) = { b | S → b. Xbw} 14

FOLLOW Sets Definition: FOLLOW(X) = { b | S → b. Xbw} 14

Computing FOLLOW Sets 1. Add $ to FOLLOW(S) where S is the start nonterminal

Computing FOLLOW Sets 1. Add $ to FOLLOW(S) where S is the start nonterminal 15

Computing FOLLOW Sets 2. If there is a production A → a. Bb, then

Computing FOLLOW Sets 2. If there is a production A → a. Bb, then everything in FIRST(b) – {e} is in FOLLOW(B) 16

Computing FOLLOW Sets 3. If there is a production A → a. B, or

Computing FOLLOW Sets 3. If there is a production A → a. B, or A → a. Bb, where in e FIRST(b) (i. e. , b → e), then everything in FOLLOW(A) is in FOLLOW(B) 17

for each A NT FOLLOW(A) ← Ø FOLLOW(S) ← {$} while ( FOLLOW sets

for each A NT FOLLOW(A) ← Ø FOLLOW(S) ← {$} while ( FOLLOW sets are still changing) for each A → b 1 b 2. . . bk P FOLLOW(bk) ←FOLLOW(bk) FOLLOW(A) T ← FOLLOW(A) for i ← k downto 2 if ( e FIRST(bi) ) FOLLOW(bi-1) ←FOLLOW(bi-1) (FIRST(bi) – {e}) T else FOLLOW(bi-1) ←FOLLOW(bi-1) 18

E E' T T' F → → → T E' +T E' | e

E E' T T' F → → → T E' +T E' | e F T' * F T' | e ( E ) | id Put $ in FOLLOW(E ) 19

E E' T T' F → → → T E' +T E' | e

E E' T T' F → → → T E' +T E' | e F T' * F T' | e ( E ) | id By rule (2) applied to production F → ( E ), ‘)’ is also in FOLLOW(E ) 20

E E' T T' F → → → T E' +T E' | e

E E' T T' F → → → T E' +T E' | e F T' * F T' | e ( E ) | id Thus, FOLLOW(E ) = { ), $} 21

E E' T T' F → → → T E' +T E' | e

E E' T T' F → → → T E' +T E' | e F T' * F T' | e ( E ) | id By rule (3) applied to production E →T E' , $ and ‘)’ are in FOLLOW(E' ) 22

E E' T T' F → → → T E' +T E' | e

E E' T T' F → → → T E' +T E' | e F T' * F T' | e ( E ) | id thus, FOLLOW(E ) = FOLLOW (E' ) = { ), $ } 23

E E' T T' F → → → T E' +T E' | e

E E' T T' F → → → T E' +T E' | e F T' * F T' | e ( E ) | id Similarly, FOLLOW(T ) = FOLLOW (T' ) = { +, ), $ } 24

LL(1) Table Construction Here now is the algorithm to construct a predictive parsing table.

LL(1) Table Construction Here now is the algorithm to construct a predictive parsing table. 26