notes on first and follow written by david walker edited by phil sweany

13
Notes on First and Follow Written by David Walker Edited by Phil Sweany

Upload: marshall-francis

Post on 13-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Notes on First and Follow Written by David Walker Edited by Phil Sweany

Notes on First and Follow

Written by David Walker

Edited by Phil Sweany

Page 2: Notes on First and Follow Written by David Walker Edited by Phil Sweany

Computing Nullable Sets

• Non-terminal X is Nullable only if the following constraints are satisfied (computed using iterative analysis)– base case:

• if (X := ) then X is Nullable

– inductive case:• if (X := ABC...) and A, B, C, ... are all Nullable then

X is Nullable

Page 3: Notes on First and Follow Written by David Walker Edited by Phil Sweany

Computing First Sets

• First(X) is computed iteratively– base case:

• if T is a terminal symbol then First (T) = {T}

– inductive case:• if X is a non-terminal and (X:= ABC...) then

– First (X) = First (X) U First (ABC...)

where First(ABC...) = F1 U F2 U F3 U ... and» F1 = First (A)» F2 = First (B), if A is Nullable» F3 = First (C), if A is Nullable & B is Nullable» ...

Page 4: Notes on First and Follow Written by David Walker Edited by Phil Sweany

Computing Follow Sets

• Follow(X) is computed iteratively– base case:

• initially, we assume nothing in particular follows X– (Follow (X) is initially { })

– inductive case:• if (Y := s1 X s2) for any strings s1, s2 then

– Follow (X) = First (s2) U Follow (X)

• if (Y := s1 X s2) for any strings s1, s2 then– Follow (X) = Follow(Y) U Follow (X), if s2 is Nullable

Page 5: Notes on First and Follow Written by David Walker Edited by Phil Sweany

building a predictive parser

Z ::= X Y ZZ ::= d

Y ::= cY ::=

X ::= aX ::= b Y e

nullable first follow

Z

Y

X

Page 6: Notes on First and Follow Written by David Walker Edited by Phil Sweany

building a predictive parser

Z ::= X Y ZZ ::= d

Y ::= cY ::=

X ::= aX ::= b Y e

nullable first follow

Z no

Y yes

X no

base case

Page 7: Notes on First and Follow Written by David Walker Edited by Phil Sweany

building a predictive parser

Z ::= X Y ZZ ::= d

Y ::= cY ::=

X ::= aX ::= b Y e

nullable first follow

Z no

Y yes

X no

after one round of induction, we realize we have reached a fixed point

Page 8: Notes on First and Follow Written by David Walker Edited by Phil Sweany

building a predictive parser

Z ::= X Y ZZ ::= d

Y ::= cY ::=

X ::= aX ::= b Y e

nullable first follow

Z no d

Y yes c

X no a,b

base case (note, first of Y should include empty string)

Page 9: Notes on First and Follow Written by David Walker Edited by Phil Sweany

building a predictive parser

Z ::= X Y ZZ ::= d

Y ::= cY ::=

X ::= aX ::= b Y e

nullable first follow

Z no d,a,b

Y yes c

X no a,b

after one round of induction, no fixed point

Page 10: Notes on First and Follow Written by David Walker Edited by Phil Sweany

building a predictive parser

Z ::= X Y ZZ ::= d

Y ::= cY ::=

X ::= aX ::= b Y e

nullable first follow

Z no d,a,b

Y yes c

X no a,b

after two rounds of induction, no more changes ==> fixed point

Page 11: Notes on First and Follow Written by David Walker Edited by Phil Sweany

building a predictive parser

Z ::= X Y ZZ ::= d

Y ::= cY ::=

X ::= aX ::= b Y e

nullable first follow

Z no d,a,b { }

Y yes c { }

X no a,b { }

base case

Page 12: Notes on First and Follow Written by David Walker Edited by Phil Sweany

building a predictive parser

Z ::= X Y ZZ ::= d

Y ::= cY ::=

X ::= aX ::= b Y e

nullable first follow

Z no d,a,b { }

Y yes c e,d,a,b

X no a,b c,d,a,b

after one round of induction, no fixed point

Page 13: Notes on First and Follow Written by David Walker Edited by Phil Sweany

building a predictive parser

Z ::= X Y ZZ ::= d

Y ::= cY ::=

X ::= aX ::= b Y e

nullable first follow

Z no d,a,b { }

Y yes c e,d,a,b

X no a,b c,d,a,b

after two rounds of induction, fixed point (but notice, computing Follow(X) before Follow (Y) would have required 3rd round)