1 / 39

Week 6

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. Lab2 part b. Incorporate Symbol table

felice
Télécharger la présentation

Week 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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. Symbol Table PROGRAM -> program id ( IDLIST) ;DECLS SUBPROG_DECLS COMPOUND_STAT . IDLIST -> idIDLIST2 IDLIST2 -> ,idIDLIST2 | λ DECLS -> var IDLIST :TYPE ; DECLS | λ TYPE -> integer |real parameters Variables

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

  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. First set • S -> A B S -> c A -> a bA A -> b B -> d B B -> e

  8. First set • What about ? S -> A B S -> c A -> a bA A -> b A ->  B -> d B B -> e Only add  to S if S itself also goes to  as the result of using that rule

  9. First set • What about ? S -> AB S -> c A -> a bA A -> b A ->  B -> d B B -> e Only add  to S if S itself also goes to  as the result of using that rule

  10. First Set S -> ABc A -> a A ->  B -> b 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.

  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. 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. 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. 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. Follow set: Case 1 • S -> A B c S -> c A -> a B A -> b B -> d B B -> e

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

  18. Follow set: Case 3 • S -> A B c S -> c A -> a B A -> b B -> d B B -> e

  19. Follow set: Case 4 • S -> A B c S -> c A-> a B A -> b B ->dB B -> e

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

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

  22. Follow set: Case 2 S -> ABc A -> a A ->  B -> b B -> 

  23. Follow set: Case 3 S -> ABc A -> a A ->  B -> b B ->  No  in Follow set

  24. Follow set: Case 4 S -> ABc A -> a A ->  B -> b B ->  No case 4 for this grammar

  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 Follow First

  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 -> 

  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 -> 

  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 ->  Case 1 & 2

  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 ->  Case 3

  30. In-Class Exercise #8: Compute First & Follow Prog -> { Stmts } Stmts -> StmtStmts Stmts ->  Stmt -> id = Expr ; Stmt -> if ( Expr ) Stmt Expr -> id Etail Etail -> + Expr Etail -> - Expr Etail ->  Case 4

  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 ->  • Insert grammar rules into table for all first set tokens (except for )

  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 ->  • Insert grammar rules into table for all first set tokens (except for )

  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 ->  • 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

  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 ->  • All rules used at least once in the table? YES • No cells with more than one rule? YES • Is this table LL(1)? YES

  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

  36. LL(1) Table Example 2 S -> ABA A -> a A ->  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. LL(1) Table Example 2 S -> ABA A -> a A ->  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. In-Class Exercise #8: Compute First & Follow Prog -> { Stmts } Stmts -> StmtStmts Stmts ->  Stmt -> id = Expr ; Stmt -> if ( Expr ) Stmt Expr -> id Etail Etail -> + Expr Etail -> - Expr Etail -> 

  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 -> 

More Related