290 likes | 453 Views
Lesson 9. CDT301 – Compiler Theory , Spring 2011 Teacher : Linus Källberg. Outline. Creating SLR parsing tables LR(0) items Canonical LR(0) collections LR(0) automata. Creating SLR parsing tables. Repetition: Shift-reduce parsing. Stack Input Action $ id * id $ sh. id
E N D
Lesson 9 CDT301 – CompilerTheory, Spring 2011 Teacher: Linus Källberg
Outline • Creating SLR parsing tables • LR(0) items • Canonical LR(0) collections • LR(0) automata
Repetition: Shift-reduce parsing StackInputAction $ id * id $ sh. id $ id * id $ red. by F → id $ F * id $ red. by T → F $ T * id $ sh. * $ T * id $ sh. id $ T * id $ red. by F → id $ T * F $ red. by T → T * F $ T $ red. by E → T $ E $ accept id * id ⇐ F * id ⇐ T * id ⇐ T * F ⇐ T ⇐ E
(1) E → E + T (2) E → T (3) T → T * F (4) T → F (5) F → ( E ) (6) F → id
Repetition: LR parsing StackInputAction $ 0 id * id $ sh. 5 $ 0 5id * id $ red. by (6) F → id $ 0 3F * id $ red. by (4) T → F $ 0 2T * id $ sh. 7 $ 0 2T 7*id $ sh. 5 $ 0 2T 7* 5id $ red. by (6) F → id $ 0 2T 7* 10F $ red. by (3) T → T * F $ 0 2T $ red. by (2) E → T $ 0 1E $ accept
LR(0) items • One staterepresents: symbol + context • The set of handles the parser might be in the process of reading in that state • The progress so far in reading those handles • LR(0) item = handle with a “dot” • Example: E → E • + T
LR(0) items • Grammar: decl → typeid ; | typeid [ num ] ; type → int | float | class class → id
LR(0) items • Set of items I1: decl → • typeid ; | • typeid [ num ] ; type → • int | • float | • class class → • id
Computing CLOSURE CLOSURE(I) J = I repeat foreach item in J if it has the form A → α•Bβ add all B → •γ to J until no more items were added in an iteration return J
The GOTO function • GOTO(I1, int) = I2: type → int • • GOTO(I1, float) = I3: type → float• • GOTO(I1, id) = I4: class→ id •
The GOTO function • GOTO(I1, type) = I5: decl → type • id ; | type • id [ num ] ; • GOTO(I1, class) = I6 type → class •
ComputingGOTO GOTO(I, X) J = Ø foreachitem A → α•Xβ in I add A → αX•β to J returnCLOSURE(J)
Reductions • State: type → int • • Pop one state • I1 appears on the stack • Pushtype and go to GOTO(I1, type)
LR(0) automata • Based on the canonical LR(0) collection • States represent sets of items • Transitionsdefined by the GOTOfunction
LR(0) automata • Grammar: S → A B S → A c A → A a A → a B → b B→ ε • Augmentedgrammar: S' → S S → A B S → A c A → A a A → a B → b B→ ε
LR(0) automata I0: S‘ → • S S → • A B S → • A c A → • A a A → • a I7: S‘ → S • I3: S → A c • S c I4: A → A a • a A I2: S → A • B S → A • c A → A • a B → • b B → • a I5: B → b • b I1: A → a • B I6: S → A B •
Computing the canonicalcollection of sets of items items(G') C = CLOSURE({ S' → • S }) repeat foreach item I in C foreach grammar symbol X ifGOTO(I, X) is non-empty, add it to C until no more sets were added in an iteration return C
Exercise (1) Compute • CLOSURE({ F → • id }) • CLOSURE({ S' → • E }) • CLOSURE({ E → T • E’ }) S' → E E → T E' E' → + T E’ | ε T → F T' T' → * F T’ | ε F → ( E ) | id
Exercise (2) Let I be the following set of items: E → T • E' E' → • + T E' E' → • Compute GOTO(I, X) for all relevant X. S' → E E → T E' E' → + T E’ | ε T → F T' T' → * F T’ | ε F → ( E ) | id
Constructing the parsing table • Enumerate the productions: (0) S' → S (1) S → A B (2) S → A c (3) A → A a (4) A → a (5) B → b (6) B→ ε • Compute FOLLOW: FOLLOW(S') = { $ } FOLLOW(S) = { $ } FOLLOW(A) = { a, b, c, $ } FOLLOW(B) = { $ }
Constructing the parsing table table(C) foreach set of item Ii in C if Ii has an item [A → α•aβ], where a is a terminal set ACTION[i, a] to a “shift j”, where j is the state representing Ij = GOTO(Ii, a) if Ii has an item [A → α•Bβ] set GOTO[i, B] to j, where j is the state representing Ij = GOTO(Ii, B) if Ii has an item [A → α•] set ACTION[i, a] to “red. by. A → α” for all a in FOLLOW(A) if Ii has the item [S' → S•] set ACTION[i, $] to “accept”
Constructing the parsing table I0: S‘ → • S S → • A B S → • A c A → • A a A → • a I7: S‘ → S • I3: S → A c • S c I4: A → A a • a A I2: S → A • B S → A • c A → A • a B → • b B → • a I5: B → b • b I1: A → a • B I6: S → A B •
Exercise (3) Construct a parsing table for the grammar here to the right. Recall: you need to • compute the canonical collection of sets of items. • compute FOLLOW for all nonterminals in the grammar. • insert actions into the parsing table. (0) S' → S (1) S → h B e (2) B → B A (3) B → ε (4) A → x (5) A → t
Shift/reduceconflicts • Grammar: stmt→ if ( expr ) stmt | if ( expr ) stmtelsestmt | other • Problematicconfiguration: StackInput $ … if ( expr ) stmtelse … $
Reduce/reduceconflicts • Grammar: stmt→ id ( param_list ) stmt→ expr := expr param_list → param_list , param param_list → param param → id expr → id ( expr_list ) expr → id expr_list → expr_list , expr expr_list → expr • Problematicconfiguration: StackInput $ … id ( id , id ) …
Conclusion • LR(0) items • Canonical LR(0) collections • LR(0) automata
Next time • Parser generator tools • Syntax-directed definitions/translation • Abstract syntax trees