Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 Compute First and Follow Compute LL(1) table.

2 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 ;”

3 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

4 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

5 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

6 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.

7 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

8 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

9 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

10 10 First Set S -> ABc A -> a A -> B -> b B -> S a, b, c A a, B b,

11 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.

12 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

13 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.

14 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

15 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

16 16 Follow set: Case 1 S -> A B c S -> c A -> a B A -> b B -> d B B -> e S $ A B

17 17 Follow set: Case 2 S -> A B c S -> c A -> a B A -> b B -> d B B -> e S $ A Bc

18 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) Bc S $ Ad, e B c

19 19 Follow set: Case 4 S -> A B c S -> c A -> a B A -> b B -> dB B -> e S $ Ad, e B c S $ A B c, d, e

20 20 Follow set: Case 1 S -> ABc A -> a A -> B -> b B -> S $ A B

21 21 Follow set: Case 2 S -> ABc A -> a A -> B -> b B -> S $ A Bc BUT, B could be a lambda

22 22 Follow set: Case 2 S -> ABc A -> a A -> B -> b B -> S $ A c Bc

23 23 Follow set: Case 3 S -> ABc A -> a A -> B -> b B -> S $ A c, First(B) Bc S $ A c, b Bc No in Follow set

24 24 Follow set: Case 4 S -> ABc A -> a A -> B -> b B -> S $ A c, b Bc No case 4 for this grammar

25 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 Se, f, g, h, p, q A e, f, B g, h, Cp,q Follow First

26 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 -> FirstFollow Prog Stmts Stmt Expr Etail

27 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 -> FirstFollow Prog { Stmts id, if, Stmt id, if Expr id Etail +, -, ==,

28 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 -> FirstFollow Prog {$ Stmts id, if, } Stmt id, if Expr id ; ) Etail +, -, ==, Case 1 & 2

29 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 -> FirstFollow Prog {$ Stmts id, if, } Stmt id, if Expr id ; ) Etail +, -, ==, Case 3

30 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 -> FirstFollow Prog {$ Stmts id, if, } Stmt id, ifid, if, } Expr id ; ) Etail +, -, ==, ; ) Case 4

31 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 SetFollow Set S a, b, c $ A a, c, b B b, c abc$ S A B -Insert grammar rules into table for all first set tokens (except for )

32 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 SetFollow Set S a, b, c $ A a, c, b B b, c abc$ SS->ABc AA-> a BB-> b -Insert grammar rules into table for all first set tokens (except for )

33 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 SetFollow 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 abc$ SS->ABc AA-> a A-> BB-> b B->

34 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 SetFollow 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 abc$ SS->ABc AA-> a A-> BB-> b B->

35 35 Use the LL(1) table to parse Stack Input Action ========================================================= $S abc$ [S,a] -> ABc $cBAabc$[A,a] -> a $cBaabc$[a,a] -> remove $cBbc$[B,b] -> b $cbbc$[b,b] -> remove $cc$[c,c] -> remove $$[$,$] -> DONE abc$ SS->ABc AA-> a A-> BB-> b B->

36 36 LL(1) Table Example 2 S -> ABA A -> a A -> B -> b B -> First SetFollow Set S a, b, $ A a, b, a, $ B b, a, $ ab$ S S-> ABA A A-> 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

37 37 LL(1) Table Example 2 S -> ABA A -> a A -> B -> b B -> First SetFollow Set S a, b, $ A a, b, a, $ B b, a, $ ab$ S S-> ABA A A-> 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

38 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 -> FirstFollow Prog {$ Stmts id, if, } Stmt id, ifid, if, } Expr id ; ) Etail +, -, ==, ; )

39 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 -> FirstFollow Prog {$ Stmts id, if, } Stmt id, ifid, if, } Expr id ; ) Etail +, -, ==, ; ) {}=idif();+-==$ Prog {Stmts} Stmts Stmt Stmts Stmtid = Expr; if (Expr ) Stmt Exprid Etail Etail + Expr-Expr==Expr


Download ppt "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."

Similar presentations


Ads by Google