FIRST Sets Dr. Mattox Beckman University of Illinois at - - PowerPoint PPT Presentation

first sets
SMART_READER_LITE
LIVE PREVIEW

FIRST Sets Dr. Mattox Beckman University of Illinois at - - PowerPoint PPT Presentation

Objectives FIRST Sets Examples FIRST Sets Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science Objectives FIRST Sets Examples Objectives Compute the FIRST sets for the nonterminal symbols of a


slide-1
SLIDE 1

Objectives FIRST Sets Examples

FIRST Sets

  • Dr. Mattox Beckman

University of Illinois at Urbana-Champaign Department of Computer Science

slide-2
SLIDE 2

Objectives FIRST Sets Examples

Objectives

◮ Compute the FIRST sets for the nonterminal symbols of a grammar.

slide-3
SLIDE 3

Objectives FIRST Sets Examples

The Problem

◮ Given a grammar for a language L, how can we recognize a sentence in L? ◮ Solution: Divide and conquer: Given a symbol E ...

◮ What symbols indicate that the symbol E is just starting? (FIRST Set) ◮ What symbols should we expect to see after we have fjnished parsing an E?

Misleadingly simple example: S→xEy E→zE E→q FIRST(E) ={z, q} FOLLOW(E)={y} ◮ Important because a parser can see only a few tokens at once.

slide-4
SLIDE 4

Objectives FIRST Sets Examples

Algorithm

We can compute the FIRST set by a simple iterative algorithm. For each symbol X:

  • 1. If X is a terminal, then FIRST(X) = {X}.
  • 2. If there is a production X → ǫ, then add ǫ to FIRST(X).
  • 3. If there is a production X → Y1Y2 · · · Yn, then add FIRST(Y1Y2 · · · Yn) to FIRST(X):

◮ If FIRST(Y1) does not contain ǫ, then FIRST(Y1Y2 · · · Yn) = FIRST(Y1). ◮ Otherwise, FIRST(Y1Y2 · · · Yn) = FIRST(Y1)/ǫ ∪ FIRST(Y2 · · · Yn). ◮ If all of Y1, Y2, . . . Yn have ǫ then add ǫ to FIRST(X).

slide-5
SLIDE 5

Objectives FIRST Sets Examples

Diagram

X → Y0 Y1 Y2 X Y0 Y1 Y2 ◮ If there is a production X → Y1Y2 · · · Yn, then add FIRST(Y1Y2 · · · Yn) to FIRST(X):

◮ If FIRST(Y1) does not contain ǫ, then FIRST(Y1Y2 · · · Yn) = FIRST(Y1). ◮ Otherwise, FIRST(Y1Y2 · · · Yn) = FIRST(Y1)/ǫ ∪ FIRST(Y2 · · · Yn). ◮ If all of Y1, Y2, . . . Yn have ǫ then add ǫ to FIRST(X).

slide-6
SLIDE 6

Objectives FIRST Sets Examples

Small Examples

Example 1

S → x A B FIRST set of S is {x}.

Example 3

B → A q B → r FIRST set of B is {y, z, q, r}.

Example 2

A → ǫ A → y A → z q FIRST set of A is {y, z, ǫ}.

Example 4

C → A A C → B FIRST set of C is {y, z, q, r, ǫ}.

slide-7
SLIDE 7

Objectives FIRST Sets Examples

FIRST Set Example

Grammar

S → if E then S ; S → print E; E → E + E E → P id P → ∗ P P → ǫ

Result

S={} E={} P={}

Action

Step 1: Create a list of symbols.

slide-8
SLIDE 8

Objectives FIRST Sets Examples

FIRST Set Example

Grammar

S → if E then S ; ⇐ S → print E; ⇐ E → E + E E → P id P → ∗ P ⇐ P → ǫ ⇐

Result

S={if, print } E={} P={ǫ, *}

Action

Step 2: Add terminals starting productions, and all ǫ.

slide-9
SLIDE 9

Objectives FIRST Sets Examples

FIRST Set Example

Grammar

S → if E then S ; S → print E; E → E + E E → P id ⇐ P → ∗ P P → ǫ

Result

S={if, print } E={*, id} P={ǫ, *}

Action

Step 3: Check productions. Add FIRST(Pid) to FIRST(E).

slide-10
SLIDE 10

Objectives FIRST Sets Examples

FIRST Set Example

Grammar

S → if E then S ; S → print E; E → E + E ⇐ E → P id P → ∗ P P → ǫ

Result

S={if, print } E={*, id} P={ǫ, *}

Action

Step 4: Check productions: E → E + E adds nothing. We’re done.

slide-11
SLIDE 11

Objectives FIRST Sets Examples

Another FIRST Set Example

Grammar

S → Ax S → By S → z A → 1CB A → 2B B → 3B B → C C → 4 C → ǫ

Result

S ={} A={} B={} C={}

Action

Create a chart.

slide-12
SLIDE 12

Objectives FIRST Sets Examples

Another FIRST Set Example

Grammar

S → Ax S → By S → z ⇐ A → 1CB ⇐ A → 2B ⇐ B → 3B ⇐ B → C C → 4 ⇐ C → ǫ ⇐

Result

S ={ z} A={ 1, 2} B={ 3} C={ ǫ, 4}

Action

Add initial terminals and ǫs.

slide-13
SLIDE 13

Objectives FIRST Sets Examples

Another FIRST Set Example

Grammar

S → Ax ⇐ S → By S → z A → 1CB A → 2B B → 3B B → C C → 4 C → ǫ

Result

S ={z, 1, 2} A={1, 2} B={3} C={ǫ, 4}

Action

Add FIRST(Ax) to FIRST(S).

slide-14
SLIDE 14

Objectives FIRST Sets Examples

Another FIRST Set Example

Grammar

S → Ax S → By ⇐ S → z A → 1CB A → 2B B → 3B B → C C → 4 C → ǫ

Result

S ={z, 1, 2, 3} A={1, 2} B={3} C={ǫ, 4}

Action

Add FIRST(By) to FIRST(S). Note that there is still more to be added to FIRST(B)! We will have to revisit this step later.

slide-15
SLIDE 15

Objectives FIRST Sets Examples

Another FIRST Set Example

Grammar

S → Ax S → By S → z A → 1CB A → 2B B → 3B B → C ⇐ C → 4 C → ǫ

Result

S ={z, 1, 2, 3} A={1, 2} B={3, 4, ǫ} C={ǫ, 4}

Action

Add FIRST(C) to FIRST(B). At this point we should iterate again to see if anything changes.

slide-16
SLIDE 16

Objectives FIRST Sets Examples

Another FIRST Set Example

Grammar

S → Ax ⇐ S → By S → z A → 1CB A → 2B B → 3B B → C C → 4 C → ǫ

Result

S ={z, 1, 2, 3} A={1, 2} B={3, 4, ǫ } C={ǫ, 4}

Action

Add FIRST(Ax) to FIRST(S) again. Nothing happens ...

slide-17
SLIDE 17

Objectives FIRST Sets Examples

Another FIRST Set Example

Grammar

S → Ax S → By ⇐ S → z A → 1CB A → 2B B → 3B B → C C → 4 C → ǫ

Result

S ={z, 1, 2, 3, 4, y} A={1, 2} B={3, 4, ǫ } C={ǫ, 4}

Action

Add FIRST(By) to FIRST(S) again. The 4 gets propagated. Since B could be ǫ we need to add y.

slide-18
SLIDE 18

Objectives FIRST Sets Examples

Another FIRST Set Example

Grammar

S → Ax S → By S → z A → 1CB A → 2B B → 3B B → C ⇐ C → 4 C → ǫ

Result

S ={z, 1, 2, 3, 4, y} A={1, 2} B={3, 4, ǫ } C={ǫ, 4}

Action

Add FIRST(C) to FIRST(B) again. We are done.