1 week 6 questions / concerns what’s due: lab2 part b due on friday hw#5 due on thursday coming...
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 -> id IDLIST2
IDLIST2 -> , id IDLIST2 | λ
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
S
A
B
C
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
S c, First(A)
A a, b
B d, e
S c, a, b
A a, b
B d, e
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
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
First Set
S -> ABc
A -> a
A -> B -> b
B ->
S a, b, c
A a,
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
S $
A
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
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
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
20
Follow set: Case 1
S -> ABc
A -> a
A -> B -> b
B ->
S $
A
B
21
Follow set: Case 2
S -> ABc
A -> a
A -> B -> b
B ->
S $
A
B c
BUT, B could be a lambda
22
Follow set: Case 2
S -> ABc
A -> a
A -> B -> b
B ->
S $
A c
B c
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
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
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
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
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 +, -, ==,
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
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
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
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 )
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 )
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->
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->
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->
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
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
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 +, -, ==, ; )
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