Objectives FOLLOW Sets Examples Objectives FOLLOW Sets Examples Objectives FOLLOW Sets Dr. Mattox Beckman ◮ Compute the FOLLOW sets for the nonterminal symbols of a grammar. University of Illinois at Urbana-Champaign Department of Computer Science Objectives FOLLOW Sets Examples Objectives FOLLOW Sets Examples FOLLOW Sets Diagram Example 1 Example 2 ◮ Given a non terminal symbol S , what terminal symbols could come after strings that are derived from S ? The algorithm: X X 1. Put $ in FOLLOW ( S ) , where S is the start symbol. $ represents the “end of input.” α Y α Y Z β 2. If there is a production X → α Y β , then add FIRST ( β ) (but not ǫ ) to FOLLOW ( Y ) . 1. If there is a production X → α Y β , then add FIRST ( β ) (but not ǫ ) to FOLLOW ( Y ) . 3. If there is a production X → α Y , or if there is a production X → α Y β , where 2. If there is a production X → α Y , or if there is a production X → α Y β , where ǫ ∈ FIRST ( β ) then add FOLLOW ( X ) to FOLLOW ( Y ) . ǫ ∈ FIRST ( β ) then add FOLLOW ( X ) to FOLLOW ( Y ) .
Objectives FOLLOW Sets Examples Objectives FOLLOW Sets Examples Small Examples FOLLOW Set Example Example 1 Example 2 Grammar Result S={ $} S → if E then S ; S → x A y A → q B E={} S → print E ; FOLLOW set of A is { y } . FOLLOW set of B is also { y } . P={} E → E + E Example 3 Example 4 E → P id P P → ∗ P B → C E D B → C E D P → ǫ FIRST ( D ) = { a , b } FIRST ( D ) = { a , b , ǫ } FOLLOW set of D is {y}. FOLLOW set of D is {y}. Action FOLLOW set of E is {a,b}. FOLLOW set of E is {a,b,y}. Make a chart, add $ to S . Objectives FOLLOW Sets Examples Objectives FOLLOW Sets Examples FOLLOW Set Example FOLLOW Set Example Grammar Result Grammar Result S={$, ;} S={$, ;} S → if E then S ; ⇐ S → if E then S ; E={ then } E={ then , ;, +} S → print E ; S → print E ; ⇐ P={} P={} E → E + E E → E + E ⇐ E → P id P E → P id P P → ∗ P P → ∗ P P → ǫ P → ǫ Action Action Check productions: add then to FOLLOW ( E ) , and ; to FOLLOW ( S ) . Check productions: add ; and + to FOLLOW ( E ) .
Objectives FOLLOW Sets Examples Objectives FOLLOW Sets Examples FOLLOW Set Example FOLLOW Set Example Grammar Result Grammar Result S={$, ;} S={$, ;} S → if E then S ; S → if E then S ; E={ then , ;, +} E={ then , ;, +} S → print E ; S → print E ; P={ id } P={ id , then , ;, +} E → E + E E → E + E E → P id P ⇐ E → P id P ⇐ P → ∗ P P → ∗ P P → ǫ P → ǫ Action Action Check productions: add id to FOLLOW ( P ) . Check endings: P ends this rule, so add FOLLOW ( E ) to FOLLOW ( P ) . Objectives FOLLOW Sets Examples Objectives FOLLOW Sets Examples FOLLOW Set Example Another FOLLOW Set Example Grammar Result S ={ $} Grammar Result S → A x A={} S={$, ;} S → B y S → if E then S ; B={} E={ then , ;, +} S → z S → print E ; C={} P={ id , then , ;, +} A → 1 CB E → E + E A → 2 B E → P id P B → 3 B P → ∗ P B → C P → ǫ C → 4 C → ǫ Action Done. Action Create a table, and add $ to FOLLOW ( S ) .
Objectives FOLLOW Sets Examples Objectives FOLLOW Sets Examples Another FOLLOW Set Example Another FOLLOW Set Example Grammar Result Grammar Result S ={ $} S ={ $} S → A x ⇐ S → A x A={ x} A={x} S → B y S → B y ⇐ B={} B={ y} S → z S → z C={} C={} A → 1 CB A → 1 CB A → 2 B A → 2 B B → 3 B B → 3 B B → C B → C C → 4 C → 4 C → ǫ C → ǫ Action Action Add x to FOLLOW ( A ) . Add y to FOLLOW ( B ) . Objectives FOLLOW Sets Examples Objectives FOLLOW Sets Examples Another FOLLOW Set Example Another FOLLOW Set Example Grammar Result Grammar Result S ={ $} S ={ $} S → A x S → A x A={x} A={x} S → B y S → B y B={y} B={y} S → z ⇐ S → z C={} C={ 3, 4} A → 1 CB A → 1 CB ⇐ A → 2 B A → 2 B B → 3 B ⇐ B → 3 B B → C B → C C → 4 ⇐ C → 4 C → ǫ ⇐ C → ǫ Action Action These productions add nothing. Add FIRST ( B ) to FOLLOW ( C ) .
Objectives FOLLOW Sets Examples Objectives FOLLOW Sets Examples Another FOLLOW Set Example Another FOLLOW Set Example Grammar Result Grammar Result S ={ $} S ={ $} S → A x S → A x A={x} A={x} S → B y S → B y B={ x, y} B={x, y} S → z S → z C={3, 4} C={ x, 3, 4} A → 1 CB ⇐ A → 1 CB ⇐ A → 2 B ⇐ A → 2 B B → 3 B B → 3 B B → C B → C C → 4 C → 4 C → ǫ C → ǫ Action Action Add FOLLOW ( A ) to FOLLOW ( B ) . B can become ǫ , so add FOLLOW ( A ) to FOLLOW ( C ) . Objectives FOLLOW Sets Examples Another FOLLOW Set Example Grammar Result S ={ $} S → A x A={x} S → B y B={x, y} S → z C={x, y, 3, 4} A → 1 CB A → 2 B B → 3 B B → C ⇐ C → 4 C → ǫ Action Add FOLLOW ( B ) to FOLLOW ( C ) . Now we’re done.
Recommend
More recommend