390 likes | 603 Vues
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
E N D
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 • 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 ;”
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
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
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
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.
First set • S -> A B S -> c A -> a bA A -> b B -> d B B -> e
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
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
First Set S -> ABc A -> a A -> B -> b B ->
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.
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
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.
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
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
Follow set: Case 1 • S -> A B c S -> c A -> a B A -> b B -> d B B -> e
Follow set: Case 2 • S -> A B c S -> c A -> a B A -> b B -> d B B -> e
Follow set: Case 3 • S -> A B c S -> c A -> a B A -> b B -> d B B -> e
Follow set: Case 4 • S -> A B c S -> c A-> a B A -> b B ->dB B -> e
Follow set: Case 1 S -> ABc A -> a A -> B -> b B ->
Follow set: Case 2 S -> ABc A -> a A -> B -> b B -> BUT, B could be a lambda
Follow set: Case 2 S -> ABc A -> a A -> B -> b B ->
Follow set: Case 3 S -> ABc A -> a A -> B -> b B -> No in Follow set
Follow set: Case 4 S -> ABc A -> a A -> B -> b B -> No case 4 for this grammar
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
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 ->
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 ->
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
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
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
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 )
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 )
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
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
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
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
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
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 ->
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 ->