1 / 11

Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation

Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation. Computer Engineering Department Dr. Raed Al-Qadi Orwa Hamad. Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation. Syntax-Directed Translation (SDT):

Télécharger la présentation

Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation

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. Introduction to compilersDr. Raed Al-qadiLecture 11- Syntax-Directed Translation • Computer Engineering Department • Dr. Raed Al-Qadi • Orwa Hamad

  2. Introduction to compilersDr. Raed Al-qadiLecture 11- Syntax-Directed Translation Syntax-Directed Translation (SDT): Associate actions with grammar productions * when production is applied, action is invoked. * Natural way to build parse tree or generate code. * Order in which action applied depends on grammar and sentence being parsed (i.e. Syntax-directed). Start with translation rules * Expression result as a function of * Constants * RHS’s terminal’s value * RHS’s non-terminal’s translations results Hand-Convert to actions

  3. Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Example: String to integers: Simple translation since values from RHS used in order: Production Translation Rules S  digit S.val= ord (digit.value)-ord(“0”) S  SS Reverse sequence of letters * Non-simple translation (values used in reverse order) Subscript identifiers the instance of S Val is the attribute of non-terminals used to discribe the translation

  4. Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Example: Binary to Decimal Production Translation B  0 B.dec=0  1 B.dec=1  B0  B1 B 1*2*2+1 1*2 B 1 1 B 0 1

  5. Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Implementing SDT: Have a semantic stack (SS) that parallels to the parse stack * Semantic stack holds results from RHS translations * When translation rule applied, it can find the results of previously applied translations on semantic stack. * Its result is pushed on stack. Action manipulate semantic stack directly Parse Stack Semantic Stack

  6. Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Writing a SDT: Write translation rules (one per production) * Define the name and meaning of symbol’s attributes. * LHS’s translation can use any (or none) result from the RHS’s previously applied translations. Convert rules to actions * Actions are mixed in with RHS of productions. * Actions manipulate semantic stack directly * Pop all of RHS’s results * Compute LHS’s function *Push result onto semantic stack Make grammar LL(1), carrying along actions

  7. Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Example: Binary to Decimal Production Translation B  0 B.dec=0  1 B.dec=1  B0  B1 Actions #1 PushSS(0); #2 PushSS(1); #3 PushSS(2*popSS()); #4 PushSS(2*popSS()+1); New Grammar View as dew type of non-terminal. When production #1 is applied, code Is invoked

  8. Introduction to compilersJames LarusLecture 11- Syntax-Directed Translation Example, cont’d Make grammar LL(1) by eliminating left-factoring and left-recursion removal: B #1 B  0#1 X #2  1#2 X BB` X B’X B’  0#3  1#4 Input Parse Stack Semantic Stack Production 101 B B1#2 x 101 1#2X 01 #2X (#2 ) 01 X 1 XB`X 01 B`X 1 B`0#3 01 0#3X 1 1 #3X 1 (#3 ) 1 X 2 B`0#3 1#4

  9. Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Example: Arithmetic Expressions E  E+T I T T T * F I F F  int I (E) Convert rules to actions: #1 (Ttmp =popSS(); /* RHS Pushed left to right */ Etmp=popSS();/* S0 pop, right-to-left */ PushSS (Etmp+Ttmp);) #2 nop /* PushSS (popSS()) */

  10. Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Example,Cont’d: Make grammar LL(1) E  TE` E` +T#1E`I T  FT` T`*F#2T`I F int#3 I(E)

  11. Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Recursive-Descent Parsing: Can use in recursive-descent parse by writing a procedure for each non-terminal * procedure E() T(); E`(); procedure E`() int Ttmp, Etmp; If next_token()=PLUS b then match(plus); T(); Ttmp=popSS(); Etmp=popSS(); PushSS(Etmp+Ttmp); E`();

More Related