
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

















CMSC430 Spring 2007 (^1)
Informal definition:
S ⇒ γ 0 ⇒ γ 1 ⇒ γ 2 ⇒ … ⇒ γ n–1 ⇒ γ n ⇒ sentence
1.isolate the handle of each right-sentential form γ i, and 2.determine the production by which to reduce,
CMSC430 Spring 2007 (^2)
Shift-reduce parsers
construct DFA for recognizing viable prefix, useFOLLOW to
CMSC430 Spring 2007 (^3)
Tablescan be built by hand
Scanner Table-driven Parser
Tables
Parser Generator
source code
grammar
IR
CMSC430 Spring 2007 (^4)
stack.push(INVALID); stack.push( s 0 ); not_found = true; token = scanner.next_token(); do while (not_found) { s = stack.top();
if ( ACTION[s,token] == “reduce A →β” ) then { stack.popnum(2|β|); // pop 2|β| symbols s = stack.top(); stack.push( A ); stack.push(GOTO[s, A ]); } else if ( ACTION[s,token] == “ shift s (^) i ” ) then { stack.push(token); stack.push( s (^) i ); token ← scanner.next_token(); } else if ( ACTION[s,token] == “ accept ” & token == EOF ) then not_found = false;
else report a syntax error and recover; } report success;
The skeleton parser
CMSC430 Spring 2007 (^7)
$ s 0 SN s 1 baa EOF shift 3 $ s 0 SN s 1 baa s 3 EOF reduce 2 $ s 0 SN s 1 EOF accept
State EOF baa 0 — shift 2 1 accept shift 3 2 reduce 3 reduce 3 3 reduce 2 reduce 2
State SheepNoise 0 1 1 0 2 0 3 0
1 Goal →^ Sheep Noise 2 Sheep Noise →^ Sheep Noise baa 3 | baa
CMSC430 Spring 2007 (^8)
LR(0) Items ( no lookahead)
CMSC430 Spring 2007 (^9)
CMSC430 Spring 2007 (^10)
P1 E ::= T + E P2 | T P3 T ::= id
P0 S' ::= E P1 E ::= T + E P2 | T P3 T ::= id
S [ S' ::=• E ] goto(S1) [ E ::=• T + E ] goto(S2) [ E ::=• T ] goto(S2) [ T ::=• id ] goto(S3) S [ S' ::= E• ] Reduce (and accept) S [ E ::= T• + E ] goto(S4) [ E ::= T• ] Reduce S [ T ::= id• ] Reduce S [ E ::= T +• E ] goto(S5) [ E ::=• T + E ] goto(S2) [ E ::=• T ] goto(S2) [ T ::=• id] goto(S3) S [ E ::= T + E• ] Reduce
Oops - Shift-reduce Conflict. What do you do here?
CMSC430 Spring 2007 (^13)
P1 E ::= T + E P2 | T P3 T ::= id
P0 S' ::= E P1 E ::= T + E P2 | T P3 T ::= id
S [ S' ::=• E ] goto(S1) [ E ::=• T + E ] goto(S2) [ E ::=• T ] goto(S2) [ T ::=• id ] goto(S3) S [ S' ::= E• ] Reduce (and accept) S [ E ::= T• + E ] First(+)= +, goto(S4) [ E ::= T• ] Follow(E) =eof, Reduce S [ T ::= id• ] Reduce S [ E ::= T +• E ] goto(S5) [ E ::=• T + E ] goto(S2) [ E ::=• T ] goto(S2) [ T ::=• id] goto(S3) S [ E ::= T + E• ] Reduce
CMSC430 Spring 2007 (^14)
Note: if α =Xβ, FIRST(α) = FIRST(X)
ForSheepNoise:
F IRST(Goal) = { baa } F IRST(SN) = { baa } F IRST(baa) = { baa }
for each x ∈ T, F IRST (x) ← { x } for each A ∈ NT, F IRST (A) ← Ø while (F IRST sets are still changing) for each p ∈ P, of the form A → β , if β is ε then F IRST (A) ← F IRST (A) ∪ { ε } else if β is B 1 B 2 …B (^) k then begin F IRST (A) ← F IRST (A) ∪ ( F IRST (B 1 ) – { ε } ) for i ← 1 to k–1 by 1 while ε ∈ F IRST (B (^) i ) F IRST (A) ← F IRST (A) ∪ ( F IRST (B (^) i +1) – { ε } ) if i = k–1 and ε ∈ FIRST(B (^) k ) then F IRST (A) ← F IRST (A) ∪ { ε } end
CMSC430 Spring 2007 (^15)
information in the states of theLR parser.
α is a production fromG with a• at some position in therhs β is a lookahead string containingk symbols (terminals oreof)
[A ::= X• Y Z, a] [A ::= X• Y Z, b]
CMSC430 Spring 2007 (^16)
in [A ::= X• Y Z, a], a has no direct use in [A ::= X Y Z•, a], a is useful
CMSC430 Spring 2007 (^19)
[A ::= α • β, a], [A ::= α • β,b], and [A ::= α • β, c], [A ::= α • β, d]
CMSC430 Spring 2007 (^20)
ForSN, this takes the DFA to s 1
S 0
S 3
S 2
S 1
baa
baa
SN
Control DFA for SN
Reduce action
Reduce action
CMSC430 Spring 2007 (^21)
goto() is analogous tomove() in the subset construction closure() adds information to round out a state
Terminal or non-terminal
CMSC430 Spring 2007 (^22)
An LR(k) item is a pair [P, δ], where
The • in an item indicates the position of the top of the stack
[A→•βγ,a] means that the input seen so far is consistent with the use ofA →βγ immediately after the symbol on top of the stack
[A →β•γ,a] means that the input sees so far is consistent with the use ofA →βγ at this point in the parse,and that the parser has
[A →βγ•,a] means that the parser has seen βγ,and that a lookahead symbol of a is consistent with reducing toA.
The table construction algorithm uses items to represent
valid configurations of an LR(1) parser
CMSC430 Spring 2007 (^25)
Expr –Term
How did it choose to expandTerm rather than reduce toExpr?
CMSC430 Spring 2007 (^26)
Stack Input Handle Action $ id – num * id none shift $ id – num * id 9,1 red. 9 $Factor – num * id 7,1 red. 7 $Term – num * id 4,1 red. 4 $Expr – num * id none shift $Expr – num * id none shift $Expr – num * id 8,3 red. 8 $Expr –Factor * id 7,3 red. 7 $Expr –Term * id none shift $Expr –Term * id none shift $Expr –Term * id 9,5 red. 9 $Expr –Term *Factor 5,5 red. 5 $Expr –Term 3,3 red. 3 $Expr 1,1 red. 1 $Goal none accept
Remember this slide?
shift here
reduce here
CMSC430 Spring 2007 (^27)
Closure(s) adds all the items implied by items already ins
withB on thelhs, and eachx ∈ F IRST(δa)
Closure( s ) while ( s is still changing ) ∀ items [ A → β • B δ ,a] ∈ s ∀ productions B →τ ∈ P ∀ b ∈ F IRST ( δ a) // δ might be ε if [ B → • τ ,b] ∉ s then add [ B → • τ ,b] to s
Closure “fills out” a state
CMSC430 Spring 2007 (^28)
Initial step builds the item [Goal→•SheepNoise, EOF]
and takes itsclosure( )
Closure( [Goal→•SheepNoise, EOF])
So,S 0 is { [Goal→ • SheepNoise,EOF], [SheepNoise→ • SheepNoise baa,EOF] , [SheepNoise→• baa,EOF], [SheepNoise→ • SheepNoise baa,baa] , [SheepNoise→ • baa,baa] }
Item From [Goal→•SheepNoise, EOF] Original item [SheepNoise→•SheepNoise baa, EOF] 1, δa is EOF [SheepNoise→ • baa, EOF] 1,^ δa is^ EOF [SheepNoise→•SheepNoise baa,baa] 2, δa is baa EOF [SheepNoise→ • baa,baa] 2, δa is baa EOF
CMSC430 Spring 2007 (^31)
S 0 : { [Goal→ • SheepNoise, EOF], [SheepNoise→ • SheepNoise baa, EOF] , [SheepNoise→• baa, EOF], [SheepNoise→ • SheepNoise baa, baa] , [SheepNoise→ • baa, baa] }
S 1 =Goto(S 0 , SheepNoise) =
{ [Goal→ SheepNoise • , EOF], [SheepNoise→ SheepNoise • baa, EOF] , [SheepNoise→ SheepNoise • baa, baa] }
S 2 =Goto(S 0 , baa) = { [SheepNoise→ baa•, EOF], [SheepNoise→ baa•, baa] }
S 3 =Goto(S 1 , baa) = { [SheepNoise→ SheepNoise baa•, EOF] ,
[SheepNoise→ SheepNoise baa•, baa] }
CMSC430 Spring 2007 (^32)
Start froms 0 =closure( [S’→S,EOF])
∀ sj ∈ S and ∀ x ∈ ( T ∪ NT )
CMSC430 Spring 2007 (^33)
Goal → Expr Expr → Term – Expr Expr → Term Term → Factor * Term Term → Factor Factor → ident
Symbol FIRST Goal { ident } Expr { ident } Term { ident } Factor { ident }
CMSC430 Spring 2007 (^34)
s 0 ← closure( { [ Goal → • Expr , EOF ] } )
S ← {s 0 }
CMSC430 Spring 2007 (^37)
Example (Summary)
CMSC430 Spring 2007 (^38)
Example (Summary)
The Goto Relationship(from the construction)
State Expr Term Factor - * Ident 0 1 2 3 4 1 2 5 3 6 4 5 7 2 3 4 6 8 3 4 7 8
CMSC430 Spring 2007 (^39)
Closure( ) instantiates F IRST(X) directly for [A→β•Xδ,a ]
∀ set sx ∈ S ∀ item i ∈ sx if i is [A→β •aδ,b]and goto(sx ,a) = sk , a ∈ T then A CTION [x,a] ← “shift k” else if i is [S’→ S •, EOF] then A CTION [x ,a] ← “accept” else if i is [A→β •,a] then A CTION[x,a] ← “reduce A→β” ∀ n ∈ NT if goto(sx ,n) = sk then GOTO [x,n] ← k
x is the state number
CMSC430 Spring 2007 (^40)
Ident - * EOF Expr Term Factor