objectives follow sets
play

Objectives Follow Sets Explain the purpose of the follow set. Dr. - PowerPoint PPT Presentation

Follow Sets Follow Sets Objectives Follow Sets Explain the purpose of the follow set. Dr. Mattox Beckman Be able to compute a follow set. University of Illinois at Urbana-Champaign Department of Computer Science Follow Sets Follow


  1. Follow Sets Follow Sets Objectives Follow Sets ◮ Explain the purpose of the follow set. Dr. Mattox Beckman ◮ Be able to compute a follow set. University of Illinois at Urbana-Champaign Department of Computer Science Follow Sets Follow Sets 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 1. If there is a production X → α Y β , then add FIRST ( β ) (but not ǫ ) to FOLLOW ( Y ) . FOLLOW ( Y ) . 3. If there is a production X → α Y , or if there is a production 2. If there is a production X → α Y , or if there is a production X → α Y β , where ǫ ∈ FIRST ( β ) then add FOLLOW ( X ) to X → α Y β , where ǫ ∈ FIRST ( β ) then add FOLLOW ( X ) to FOLLOW ( Y ) . FOLLOW ( Y ) .

  2. Follow Sets Follow Sets Small Examples Follow Set Example Example 1 Example 2 Grammar Result S={ $} S → x A y A → q B S → if E then S ; E={} Follow set of A is { y } Follow set of B is also { y } S → print E ; P={} E → E + E Example 3 Example 4 E → P id P B → C E D B → C E D P → ∗ P First ( D ) = { a , b } First ( D ) = { a , b , ǫ } P → ǫ 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 . Follow Sets Follow Sets 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 )

  3. Follow Sets Follow Sets 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 ) . Follow Sets Follow Sets Follow Set Example Another Follow Set Example Grammar Result S ={ $} S → A x Grammar Result A={} S → B y S={$, ;} 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 Action C → ǫ Done. Action Create a table, and add $ to Follow ( S ) .

  4. Follow Sets Follow Sets 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 ) . Follow Sets Follow Sets 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 )

  5. Follow Sets Follow Sets 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 ) . Follow Sets 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.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend