Upload
emerald-goodwin
View
217
Download
1
Embed Size (px)
Citation preview
Introduction to compilersDr. Raed Al-qadi
Lecture 11- Syntax-Directed Translation
• Computer Engineering Department
• Dr. Raed Al-Qadi
• Orwa Hamad
Introduction to compilersDr. 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
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)
valSvalSvalS .10*.. 321
Subscript identifiers the instance of S Val is the attribute of non-terminals used to discribe the translation
SSS
letterS
revSrevSrevS
valueletterrevS
...
..
231
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
2*.. 21 decBdecB 12*.. 21 decBdecB
B 1
B0
1
1
1*2
1*2*2+1
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 directlyABCS ).,.,.(. valCvalBvalAfvalS
Parse Stack Semantic Stack
C
B
A.....
valC.
valB.valA.
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
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
2*.. 21 decBdecB 12*.. 21 decBdecB
1#0B2#13#0B4#1B
View as dew type of non-terminal.When production #1 is applied, codeIs invoked
Introduction to compilersJames 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
• Input Parse Stack Semantic Stack Production 101 B B1#2 x 101 1#2X 01 #2X (#2 ) 01 X 1 XB`X 01 B`X 1 B`0#3 01 0#3X 1 1 #3X 1 (#3 ) 1 X 2
B`0#3 1#4
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()) */
valTvalEvalE ... 21 valTvalE ..
valFvalTvalT .*.. 21 valFvalT .. valuevalF .int. valTvalF ..
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)
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`();