1 week 6 questions / concerns what’s due: lab2 part b due on friday hw#5 due on thursday coming...

39
1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b Compute First and Follow Compute LL(1) table.

Upload: tabitha-boone

Post on 20-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

1

Week 6

• Questions / Concerns• What’s due:

• Lab2 part b due on Friday• HW#5 due on Thursday

• Coming up:• Project posted. You can work in pairs.

• Lab2 part b• Compute First and Follow• Compute LL(1) table.

Page 2: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

2

Lab2 part b

• Incorporate Symbol table• Add variables, parameters, function names and types into symbol

table.

• Add appropriate error messages.• “Missing ;”• “Expecting a (“

• Add statements after each rule has been parsed.• “S-> aSb”• “DataDecl -> TYPE ID ;”

Page 3: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

3

Symbol Table

PROGRAM -> program id ( IDLIST ) ; DECLS SUBPROG_DECLS COMPOUND_STAT .

 

IDLIST -> id IDLIST2

IDLIST2 -> , id IDLIST2 | λ

 

DECLS -> var IDLIST : TYPE ; DECLS

| λ

 

TYPE -> integer

| real

parameters

Variables

Page 4: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

4

LL(1)

• Another top-down parser• It’s a table-driven parser.• LL(1)

• L – first L, the input is from left to right • L – second L, leftmost derivation (top-down)• 1 – one token look ahead

• Grammar pre-req:• No left recursion• Unit productions are okay, but should minimize• MUST left factor to ensure one-token look ahead.

• Procedure:• Compute First and Follow sets from the grammar

Page 5: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

5

In-Class Exercise #7 Part 1

• Please compute First sets for the following grammar:

S -> ABCd

A -> e | f | B -> g | h | C -> p | q

S

A

B

C

Page 6: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

6

First set

• Grammar with no lambda

S -> A B

S -> c

A -> a bA

A -> b

B -> d B

B -> e

First set is just the first token.

If the first symbol is a non-terminal, then include all the first tokens from that non-terminal.

Page 7: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

7

First set

• S -> A B

S -> c

A -> a bA

A -> b

B -> d B

B -> e

S c, First(A)

A a, b

B d, e

S c, a, b

A a, b

B d, e

Page 8: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

8

First set

• What about ?

S -> A B

S -> c

A -> a bA

A -> b

A -> B -> d B

B -> e

S c, First(A) -

A a, b,

B d, e

S c, a, b

A a, b,

B d, e

Only add to S if S itself also goes to as the result of using that rule

Page 9: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

9

First set

• What about ?

S -> A B

S -> c

A -> a bA

A -> b

A -> B -> d B

B -> e

S c, First(A)-, First(B)-

A a, b,

B d, e

S c, a, b, d, e

A a, b,

B d, e

Only add to S if S itself also goes to as the result of using that rule

Page 10: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

10

First Set

S -> ABc

A -> a

A -> B -> b

B ->

S a, b, c

A a,

B b,

Page 11: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

11

Follow set

• If any one grammar rule in your grammar has lambda productions, then you need to compute the follow set for the entire grammar.

• Follow set is just a set of tokens that follow a particular non-terminal.

• There are 4 different cases to consider in computing the Follow set. Not all 4 cases are applicable in every case.

Page 12: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

12

Follow Set

• Case 1: If S is the start symbol, then add $ (end of input) to the Follow set of S.Nothing should follow S after S is done parsing.

• Case 2: If you have a token following a non-terminal on the right hand side of the rule, then add that token to the follow set of the non-terminal.

… -> .. Ab - b follows A. Add b to the Follow set of A.

Note: We don’t care what’s on the left hand side of the rule, just that b follows the non-terminal A

Page 13: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

13

Follow Set

• Case 3: If you have a non-terminal following a non-terminal on the right hand side of the rule, then add that First set of the second non-terminal to the follow set of the first non-terminal.

… -> .. AB - B follows A. Add First(B) - to the Follow set of A.

Note: We don’t care what’s on the left hand side of the rule, just that B follows the non-terminal A.

We don’t have in the Follow set.

Page 14: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

14

Follow Set

• Case 4: If you have this following pattern:

A -> ….. B

This could be an unit production or just any rule that ends with a non-terminal.

In this case, anything that follows A also follows B

Page 15: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

15

Follow Set

• Case 4: If you have this following pattern:

A -> ….. B

A

B

Whatever follows A , also follows B because there is nothing else after B on the lower level of the parser tree

Page 16: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

16

Follow set: Case 1

• S -> A B c

S -> c

A -> a B

A -> b

B -> d B

B -> e

S $

A

B

Page 17: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

17

Follow set: Case 2

• S -> A B c

S -> c

A -> a B

A -> b

B -> d B

B -> e

S $

A

B c

Page 18: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

18

Follow set: Case 3

• S -> A B c

S -> c

A -> a B

A -> b

B -> d B

B -> e

S $

A First(B)

B c

S $

A d, e

B c

Page 19: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

19

Follow set: Case 4

• S -> A B c

S -> c

A -> a B

A -> b

B -> dB

B -> e

S $

A d, e

B c

S $

A d, e

B c, d, e

Page 20: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

20

Follow set: Case 1

S -> ABc

A -> a

A -> B -> b

B ->

S $

A

B

Page 21: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

21

Follow set: Case 2

S -> ABc

A -> a

A -> B -> b

B ->

S $

A

B c

BUT, B could be a lambda

Page 22: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

22

Follow set: Case 2

S -> ABc

A -> a

A -> B -> b

B ->

S $

A c

B c

Page 23: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

23

Follow set: Case 3

S -> ABc

A -> a

A -> B -> b

B ->

S $

A c, First(B)

B c

S $

A c, b

B c

No in Follow set

Page 24: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

24

Follow set: Case 4

S -> ABc

A -> a

A -> B -> b

B ->

S $

A c, b

B c

No case 4 for this grammar

Page 25: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

25

In-Class Exercise #7 Part 2

• Please compute first sets for the following grammar:

S -> ABCd

A -> e | f | B -> g | h | C -> p | q

S

A

B

C

S e, f, g, h, p, q

A e, f,

B g, h,

C p,q

Follow

First

Page 26: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

26

In-Class Exercise #8: Compute First & Follow

Prog -> { Stmts }

Stmts -> Stmt Stmts

Stmts -> Stmt -> id = Expr ;

Stmt -> if ( Expr ) Stmt

Expr -> id Etail

Etail -> + Expr

Etail -> - Expr

Etail ->

First Follow

Prog

Stmts

Stmt

Expr

Etail

Page 27: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

27

In-Class Exercise #8: Compute First & Follow

Prog -> { Stmts }

Stmts -> Stmt Stmts

Stmts -> Stmt -> id = Expr ;

Stmt -> if ( Expr ) Stmt

Expr -> id Etail

Etail -> + Expr

Etail -> - Expr

Etail -> == Expr

Etail ->

First Follow

Prog {

Stmts id, if,

Stmt id, if

Expr id

Etail +, -, ==,

Page 28: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

28

In-Class Exercise #8: Compute First & Follow

Prog -> { Stmts }

Stmts -> Stmt Stmts

Stmts -> Stmt -> id = Expr ;

Stmt -> if ( Expr ) Stmt

Expr -> id Etail

Etail -> + Expr

Etail -> - Expr

Etail -> == Expr

Etail ->

First Follow

Prog { $

Stmts id, if, }

Stmt id, if

Expr id ; )

Etail +, -, ==,

Case 1 & 2

Page 29: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

29

In-Class Exercise #8: Compute First & Follow

Prog -> { Stmts }

Stmts -> Stmt Stmts

Stmts -> Stmt -> id = Expr ;

Stmt -> if ( Expr ) Stmt

Expr -> id Etail

Etail -> + Expr

Etail -> - Expr

Etail -> == Expr

Etail ->

First Follow

Prog { $

Stmts id, if, }

Stmt id, if id, if

Expr id ; )

Etail +, -, ==,

Case 3

Page 30: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

30

In-Class Exercise #8: Compute First & Follow

Prog -> { Stmts }

Stmts -> Stmt Stmts

Stmts -> Stmt -> id = Expr ;

Stmt -> if ( Expr ) Stmt

Expr -> id Etail

Etail -> + Expr

Etail -> - Expr

Etail ->

First Follow

Prog { $

Stmts id, if, }

Stmt id, if id, if, }

Expr id ; )

Etail +, -, ==, ; )

Case 4

Page 31: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

31

LL(1) table

• Using First, Follow sets & the grammar rules to construct the LL(1) table.

S -> ABc

A -> a

A -> B -> b

B -> •

First Set Follow Set

S a, b, c $

A a, c, b

B b, c

a b c $

S

A

B

- Insert grammar rules into table for all first set tokens (except for )

Page 32: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

32

LL(1) table

• Using First, Follow sets & the grammar rules to construct the LL(1) table.

S -> ABc

A -> a

A -> B -> b

B -> •

First Set Follow Set

S a, b, c $

A a, c, b

B b, c

a b c $

S S->ABc S->ABc S->ABc

A A-> a

B B-> b

- Insert grammar rules into table for all first set tokens (except for )

Page 33: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

33

LL(1) table

• Using First, Follow sets & the grammar rules to construct the LL(1) table.

S -> ABc

A -> a

A -> B -> b

B -> •

First Set Follow Set

S a, b, c $

A a, c, b

B b, c

- Insert grammar rules into table for all first set tokens (except for )

- If first set contains , insert rule for all tokens in the follow set

a b c $

S S->ABc S->ABc S->ABc

A A-> a A-> A->

B B-> b B->

Page 34: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

34

Is this LL(1)?

• Using First, Follow sets & the grammar rules to construct the LL(1) table.

S -> ABc

A -> a

A -> B -> b

B -> •

First Set Follow Set

S a, b, c $

A a, c, b

B b, c

- All rules used at least once in the table? YES- No cells with more than one rule? YES- Is this table LL(1)? YES

a b c $

S S->ABc S->ABc S->ABc

A A-> a A-> A->

B B-> b B->

Page 35: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

35

Use the LL(1) table to parse

Stack Input Action=========================================================$S abc$ [S,a] -> ABc$cBA abc$ [A,a] -> a$cBa abc$ [a,a] -> remove $cB bc$ [B,b] -> b$cb bc$ [b,b] -> remove$c c$ [c,c] -> remove$ $ [$,$] -> DONE

a b c $

S S->ABc S->ABc S->ABc

A A-> a A-> A->

B B-> b B->

Page 36: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

36

LL(1) Table Example 2

S -> ABA

A -> a

A -> B -> b

B -> •

First Set Follow Set

S a, b, $

A a, b, a, $

B b, a, $

a b $

S S-> ABA S-> ABA S-> ABA

A A-> aA ->

A -> A ->

B B -> B-> b B ->

S itself could go to also

This grammar is not LL(1) because based on one token “a”, we can’t tell if it belongs to the first A or second A in S-> ABA

Page 37: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

37

LL(1) Table Example 2

S -> ABA

A -> a

A -> B -> b

B -> •

First Set Follow Set

S a, b, $

A a, b, a, $

B b, a, $

a b $

S S-> ABA S-> ABA S-> ABA

A A-> aA ->

A -> A ->

B B -> B-> b B ->

This grammar is not LL(1) because based on one token “a”, we can’t tell if it belongs to the first A or second A in S-> ABA

Page 38: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

38

In-Class Exercise #8: Compute First & Follow

Prog -> { Stmts }

Stmts -> Stmt Stmts

Stmts -> Stmt -> id = Expr ;

Stmt -> if ( Expr ) Stmt

Expr -> id Etail

Etail -> + Expr

Etail -> - Expr

Etail ->

First Follow

Prog { $

Stmts id, if, }

Stmt id, if id, if, }

Expr id ; )

Etail +, -, ==, ; )

Page 39: 1 Week 6 Questions / Concerns What’s due: Lab2 part b due on Friday HW#5 due on Thursday Coming up: Project posted. You can work in pairs. Lab2 part b

39

In-Class Exercise #8: Compute First & Follow

Prog -> { Stmts }

Stmts -> Stmt Stmts

Stmts -> Stmt -> id = Expr ;

Stmt -> if ( Expr ) Stmt

Expr -> id Etail

Etail -> + Expr

Etail -> - Expr

Etail -> == Expr

Etail ->

First Follow

Prog { $

Stmts id, if, }

Stmt id, if id, if, }

Expr id ; )

Etail +, -, ==, ; )

{ } = id if ( ) ; + - == $Prog {Stmts}

Stmts Stmt Stmts

Stmt Stmts

Stmt id = Expr;

if (Expr ) Stmt

Expr id Etail

Etail + Expr -Expr ==Expr