Plan fr day Ambiguus Grammars Disambiguating ambiguus grammars Predictive parsing IR and OLLOW sets Predictive Parsing table C453 Lecture p-dwn Predictive Parsers 1 Ambiguus Grammars Ambiguus grammar: >1 parse tree fr 1 sentence xpressin grammar parse tree 1 à à à - à ( ) à ID à NUM tring parse tree 2 42 7 6 what abut 42-7-6? C453 Lecture p-dwn Predictive Parsers 2 (42) (42) Gal: disambiguate the grammar Unambiguus grammar fr simple expressins Cause: the grammar did nt specify the precedence nr the assciativity f the peratrs,-, w ptins: keep the ambiguus grammar, but add extra directives t the parser, s that nly ne tree is frmed (ee PA0.cup fr imple xpressin Language) Rewrite the grammar, making the precedence and assciativity explicit in the grammar. Grammar à - à à ( ) ID NUM tring 4276 parse tree Hw is the precedence encded? (42) Hw is the assciativity encded? C453 Lecture p-dwn Predictive Parsers 3 C453 Lecture p-dwn Predictive Parsers 4 C453 Intr and PA1 1
Augmenting the grammar with nd f ile Grammar defines the syntactically valid strings Parser recgnizes them (same as reg.exp. and scanner) deal with end-f-file we augment the grammar with an end-f-file symbl ($), and create a new start symbl: à $ C453 Lecture p-dwn Predictive Parsers 5 Predictive Parsing Predictive parsing, such as recursive descent parsing, creates the parse tree OP DOWN, starting at the start symbl. r each nn-terminal N there is a methd recgnizing the strings that can be prduced by N, with ne (case) clause fr each prductin. his wrked great fr a slightly changed versin f ur example frm last lecture: start -> stmts O stmts -> ε stmt stmts stmt -> iftmt whiletmt ID = NUM iftmt -> I id { stmts } whiletmt -> WHIL id { stmts } because each clause culd be uniquely identified by lking ahead ne tken. Let s predictively build the parse tree fr if t { while b { x = 6 }} $ C453 Lecture p-dwn Predictive Parsers 6 When Predictive Parsing wrks, when it des nt What abut ur expressin grammar: à - à à ( ) ID NUM he methd cannt decide lking ne tken ahead whether t predict, -, r. ame prblem fr. irst Given a phrase γ f terminals and nn-terminals (a rhs f a prductin), IR(γ) is the set f all terminals that can begin a string derived frm γ. IR() =? IR()=? IR(XYZ) = IR(X)? Predictive parsing wrks fr grammars where the first terminal symbl f each sub expressin prvides enugh infrmatin t decide which prductin t use. NO! X culd prduce ε and then IR(Y) cmes int play we must keep track f which nn terminals are NULLABL C453 Lecture p-dwn Predictive Parsers 7 C453 Lecture p-dwn Predictive Parsers 8 C453 Intr and PA1 2
llw It als turns ut t be useful t determine which terminals can directly fllw a nn terminal X (t decide parsing X is finished). terminal t is in OLLOW(X) if there is any derivatin cntaining Xt. his can ccur if the derivatin cntains XYZt and Y and Z are nullable IR and OLLOW sets NULLABL X is a nnterminal nullable(x) is true if X can derive the empty string IR IR(z) = {z}, where z is a terminal IR(X) = unin f all IR( rhs i ), where X is a nnterminal and X -> rhs i IR(rhs i ) = unin all f IR(sym) n rhs up t and including first nnnullable OLLOW(Y), nly relevant when Y is a nnterminal lk fr Y in rhs f rules (lhs -> rhs) and unin all IR sets fr symbls after Y up t and including first nnnullable if all symbls after Y are nullable then als unin in OLLOW(lhs) C453 Lecture p-dwn Predictive Parsers 9 C453 Lecture p-dwn Predictive Parsers 10 Cnstructive Definitin f nullable, first and fllw fr each terminal t IR(t)={t} Anther ransitive Clsure algrithm: keep ding P until nthing changes P: fr each prductin X à Y 1 Y 2 Y k if Y 1 t Y k nullable (r k = 0) nullable(x) = true fr each i frm 1 t k, each j frm i1 t k 1: if Y 1 Y i-1 nullable (r i=1) IR(X) = IR(Y i ) //: unin 2: if Y i1 Y k nullable (r i=k) OLLOW(Y i ) = OLLOW(X) 3: if Y i1 Y j-1 nullable (r i1=j) OLLOW(Y i ) = IR(Y j ) Class xercise Cmpute nullable, IR and OLLOW fr Z à d X Y Z X à a Y Y à c ε We can cmpute nullable, then IR, and then OLLOW C453 Lecture p-dwn Predictive Parsers 11 C453 Lecture p-dwn Predictive Parsers 12 C453 Intr and PA1 3
Cnstructing the Predictive Parser able A predictive parse table has a rw fr each nn-terminal X, and a clumn fr each input tken t. ntries table[x,t] cntain prductins: fr each X -> gamma fr each t in IR(gamma) table[x,t] = X->gamma if gamma is nullable fr each t in OLLOW(X) Cmpute the predictive parse table fr Z à d X Y Z X à a Y Y à c ε table[x,t] = X->gamma a c d X Xà a Xà Y Xà Y Xà Y Y Yà ε Yà ε Yà ε Yà c Z Zà XYZ Zà XYZ Zà XYZ Zà d Multiple entries in the Predictive parse table: Ambiguity An ambiguus grammar will lead t multiple entries in the parse table. Our grammar I ambiguus, e.g. but als Z à d Zà XYZà YZà d r grammars with n multiple entries in the table, we can use the table t prduce ne parse tree fr each valid sentence. We call these grammars LL(1): Left t right parse, Left-mst derivatin, 1 symbl lkahead. A recursive descent parser examines input left t right. he rder it expands nn-terminals is leftmst first, and it lks ahead 1 tken. C453 Lecture p-dwn Predictive Parsers 13 C453 Lecture p-dwn Predictive Parsers 14 Left recursin and Predictive parsing What happens t the recursive descent parser if we have a left recursive prductin rule, e.g. à calls calls frever eliminate left recursin we rewrite the grammar: frm: t: à - à à à - ε à ( ) ID NUM à à ε à ( ) ID NUM replacing left recursin Xà Xγ α (where α des nt start with X) by right recursin, as X prduces α γ that can be prduced right recursively. Nw we can augment the grammar (à $), cmpute nullable, IR and OLLOW, and prduce an LL(1) predictive parse table, see iger ectin 3.2. C453 Lecture p-dwn Predictive Parsers 15 Left actring Left recursin des nt wrk fr predictive parsing. Neither des a grammar that has a nn-terminal with tw prductins that start with a cmmn phrase, s we left factr the grammar: αβ 1 αβ 2 Left refactr.g.: if statement: à I t HN L I t HN becmes à I t HN X Xà L ε α' ' β 1 β 2 When building the predictive parse table, there will be a multiple entries. WHY? C453 Lecture p-dwn Predictive Parsers 16 C453 Intr and PA1 4
Dangling else prblem: ambiguity Dangling else disambiguatin Given à I t HN X Xà L ε cnstruct tw parse trees fr I t HN I t HN L he crrect parse tree is: I t HN X I t HN X ε L I t HN X I t HN X L ε Which is the crrect parse tree? (C, Java rules) I t HN I t HN We can get this parse tree by remving the Xà ε rule in the multiple entry slt in the parse tree. ee written hmewrk 2. X L X ε C453 Lecture p-dwn Predictive Parsers 17 C453 Lecture p-dwn Predictive Parsers 18 One mre time Balanced parentheses grammar 1: à ( ) ε 1. Augment the grammar 2. Cnstruct Nullable, irst and llw 3. Build the predictive parse table, what happens? One mre time, but this time with feeling Balanced parentheses grammar 1: à ( ) ε 1. Augment the grammar 2. Cnstruct Nullable, irst and llw 3. Build the predictive parse table 4. Using the predictive parse table, cnstruct the parse tree fr ( ) ( ( ) ) $ and ( ) ( ) ( ) $ C453 Lecture p-dwn Predictive Parsers 19 C453 Lecture p-dwn Predictive Parsers 20 C453 Intr and PA1 5