Download presentation
Presentation is loading. Please wait.
Published byEmerald Goodwin Modified over 9 years ago
1
Introduction to compilers Dr. Raed Al-qadi Lecture 11- Syntax-Directed Translation Computer Engineering Department Dr. Raed Al-Qadi Orwa Hamad
2
Introduction to compilers Dr. Raed Al-qadi Lecture 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: ProductionTranslation 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 SVal 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 ProductionTranslation B 0 B.dec=0 1 B.dec=1 B0 B1 B B 1 B 0 1 1 1*2 1*2*2+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 ProductionTranslation 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 compilers James Larus Lecture 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 InputParse StackSemantic StackProduction 101 B B 1#2 x 101 1#2X 01 #2X (#2 ) 01 X1 X B`X 01 B`X1 B` 0#3 01 0#3X1 1 #3X1 (#3 ) 1 X2 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`();
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.