1 / 10

Section 5.5

Section 5.5. Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs, CT 06269. aggelos@cse.uconn.edu http://www.cse.uconn.edu/~akiayias. Top-Down Translation. F  ( E ) | id.

uta
Download Presentation

Section 5.5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Section 5.5 Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs, CT 06269 aggelos@cse.uconn.edu http://www.cse.uconn.edu/~akiayias

  2. Top-Down Translation F  ( E ) | id E  TE’ E’  + TE’ |  T  FT’ T’  * FT’ |  • Implementing L-attributed Definitions during “predictive parsing.” • Recall predictive parsing (chapter 4): • Grammar has no left-recursion and it is left-factored. • For each non-terminal construct a procedure thatimplements all of its productions. • Example: recall the grammar

  3. Fragment of a Predictive Parser main() { E(); } E’() { if lookahead = ‘+’ then { match(‘+’); T(); E’(); } } F() { if lookahead = ‘(’ then { match(‘(‘); E(); match(‘)’); } else ... } TE() { T(); E’(); } T() { F(); T’(); } E’() { if lookahead = ‘*’ then { match(‘*’); F(); T’(); } }

  4. Eliminating Left-Recursion From Translation Schemes TRANSLATION SCHEME E E1+ T {E.val = E1.val + T.val} E E1- T {E.val = E1.val - T.val} E T {E.val = T.val} T (E) {T.val = E.val} T  num {T.val = text.val } Grammar with left recursion removed: E T R {E.val = ?} R + T R {?} R - T R {?} R   {?} T (E) {T.val = E.val} T  num {T.val = text.val }

  5. Eliminating Left-Recursion From Translation Schemes, II TRANSLATION SCHEME E E1+ T {E.val = E1.val + T.val} E E1- T {E.val = E1.val - T.val} E T {E.val = T.val} T (E) {T.val = E.val} T  num {T.val = text.val } Grammar with left recursion removed: E T {R.i = T.val} R {E.val = R.s } R + T {R1.i = R.i + T.val} R1 {R.s = R1.s} R - T {R1.i = R.i - T.val} R1 {R.s = R1.s} R   {R.s = R.i} T (E) {T.val = E.val} T  num {T.val = text.val } Employ for R an inherited and a synthesized attribute i, s. Example: 9-5+2

  6. Eliminating Left-Recursion From Translation Schemes, III In GENERAL: TRANSLATION SCHEME with synthesized attributes A A1 Y {A.a = g(A1.a , Y.y) } A X {A.a = f(X.x) } Translation scheme with left recursion removed: A X {R.i = f(X.x)} R {A.a = R.s) } R Y {R1.i = g(R.i, Y.y)} R1 {A.a = R.s) } R  {R.s = R.i} Example: XYY

  7. Eliminating Left-Recursion from Translation Schemes, IV E E1+ T {E.nptr = mknode(“+”, E1.nptr , T.nptr )} E E1- T {E.nptr = mknode(“-”, E1.nptr , T.nptr )} E T {E.nptr = T.nptr } T (E) {T.nptr = E.nptr} T id {T.nptr = mkleaf(id, id.entry)} T num {T.nptr = mkleaf(num, num.entry)} Translation Scheme with left recursion removed: E T { ??? } R { ??? } R + T { ??? } R1 { ??? } R - T { ??? } R1 { ??? } R   {R.s = R.i} T (E) {T.nptr = E.nptr} T id {T.nptr = mkleaf(id, id.entry)} T num {T.nptr = mkleaf(num, num.entry)}

  8. Eliminating Left-Recursion from Translation Schemes, V E E1+ T {E.nptr = mknode(“+”, E1.nptr , T.nptr )} E E1- T {E.nptr = mknode(“-”, E1.nptr , T.nptr )} E T {E.nptr = T.nptr } T (E) {T.nptr = E.nptr} T id {T.nptr = mkleaf(id, id.entry)} T num {T.nptr = mkleaf(num, num.entry)} Translation Scheme with left recursion removed: E T { R.i = T.nptr } R { E.nptr =R.s} R + T { R1.i = mknode(“+”, R.i , T.nptr )} R1 { R.s = R1.s } R - T { R1.i = mknode(“-”, R.i , T.nptr )} R1 { R.s = R1.s } R   {R.s = R.i} T (E) {T.nptr = E.nptr} T id {T.nptr = mkleaf(id, id.entry)} T num {T.nptr = mkleaf(num, num.entry)}

  9. Translation over a Predictive Parser (Simplified) Translation Scheme E T { R.i = T.nptr } R { E.nptr =R.s} R addop T { R1.i = mknode(addop,lex , R.i , T.nptr )} R1 { R.s = R1.s } R   {R.s = R.i} T (E) {T.nptr = E.nptr} T id {T.nptr = mkleaf(id, id.entry)} T num {T.nptr = mkleaf(num, num.entry)} main() { E(); } Plain Predictive Parser: T() {if lookahead = ‘(’ then { match(‘(’); E(); match(‘)’); } else if lookahead = ‘id’ then {match(‘id’);} else if lookahead = ‘num’ then {match(‘num’);} else error} R() {if lookahead=‘addop’ then { match(‘addop’); T(); R(); } else { } } E() { T(); R(); }

  10. Translation over a Predictive Parser, II node T() {if lookahead = ‘(’ then { match(‘(’); nptr=E(); match(‘)’); } else if lookahead = ‘id’ then { match(‘id’); label = lexval; nptr=mkleaf(label);} else if lookahead = ‘num’ then { match(‘num’); label = lexval; nptr=mkleaf(label);} else error; return nptr} node main() { return E(); } node TD_E() { nptr = T(); return R(nptr); } node R(i:node) {if lookahead=‘addop’ then { match(‘addop’); addoplex=lexval; nptr = T(); i = mknode(addoplex,i,nptr); s = R(i); } else { s=i } return s }

More Related