SyntaxDirected translation Exercises Exercise 5 1 1 For

  • Slides: 26
Download presentation
Syntax-Directed translation Exercises

Syntax-Directed translation Exercises

Exercise 5. 1. 1 • For the SDD of the following Figure, give annotated

Exercise 5. 1. 1 • For the SDD of the following Figure, give annotated parse trees for the following expressions: • a) 3 * 5 + 4 n • b) (3+4) * (5+6)n • c) 1*2*3*(4+5)n

Exercise 5. 1. 1. a (Answer)

Exercise 5. 1. 1. a (Answer)

Exercise 5. 1. 1. b (Answer)

Exercise 5. 1. 1. b (Answer)

Exercise 5. 1. 1. c (Answer)

Exercise 5. 1. 1. c (Answer)

Example • For the SDD of in Exercise 5. 1. 1, give annotated parse

Example • For the SDD of in Exercise 5. 1. 1, give annotated parse trees and dependency graph for the following expression: • 5+3*4 n

Annotated Parse Tree (Answer) L Input: 5+3*4 E. val=17 E. val=5 + return T.

Annotated Parse Tree (Answer) L Input: 5+3*4 E. val=17 E. val=5 + return T. val=12 T. val=5 T. val=3 * F. val=4 F. val=5 F. val=3 digit. lexval=4 digit. lexval=5 digit. lexval=3 7

Dependency Graph (Answer) L Input: 5+3*4 E. val=17 E. val=5 T. val=12 T. val=5

Dependency Graph (Answer) L Input: 5+3*4 E. val=17 E. val=5 T. val=12 T. val=5 T. val=3 F. val=4 F. val=5 F. val=3 digit. lexval=4 digit. lexval=5 digit. lexval=3 8

Exercise 5. 1. 3. a • For the SDD of the following Figure, give

Exercise 5. 1. 3. a • For the SDD of the following Figure, give annotated parse tree and dependency graph for the following expressions: • a) 1 * 2

Exercise 5. 1. 3. a (Answer)

Exercise 5. 1. 3. a (Answer)

Exercise 5. 1. 3. b • For the SDD of the following Figure, give

Exercise 5. 1. 3. b • For the SDD of the following Figure, give annotated parse trees for the following expressions: • b) 3 + 4 n

Exercise 5. 1. 3. b (Answer)

Exercise 5. 1. 3. b (Answer)

Exercise 5. 2. 2 • For the SDD of the following Figure, give annotated

Exercise 5. 2. 2 • For the SDD of the following Figure, give annotated parse trees and dependency graph for the following expressions: • a) real id 1, id 2, id 3 • b) int a, b, c.

Exercise 5. 2. 2. b(answer)

Exercise 5. 2. 2. b(answer)

Exercise 5. 2. 2. c(answer)

Exercise 5. 2. 2. c(answer)

Exercise 2. 3. 2 • Construct a syntax-directed translation scheme that translates arithmetic expressions

Exercise 2. 3. 2 • Construct a syntax-directed translation scheme that translates arithmetic expressions from infix notation into postfix notation. Give annotated parse trees for the input 1+2/3. • expr -> expr + term • expr -> term • term -> term / factor • term -> factor • factor -> digit • factor -> (expr)

Exercise 2. 3. 2(Answer) • • • expr -> expr + term {print("+")} expr

Exercise 2. 3. 2(Answer) • • • expr -> expr + term {print("+")} expr -> term / factor {print("/")} term -> factor -> digit {print(digit)} factor -> (expr)

Exercise 2. 3. 2(Answer)

Exercise 2. 3. 2(Answer)

Exercise 2. 3. 1 • Construct a syntax-directed translation scheme that translates arithmetic expressions

Exercise 2. 3. 1 • Construct a syntax-directed translation scheme that translates arithmetic expressions from infix notation into postfix notation. Give annotated parse trees for the input a+b+c. E→TR R → + T R 1 R→ T → id

Exercise 2. 3. 1(Answer) E→TR R → + T { print(“+”) } R 1

Exercise 2. 3. 1(Answer) E→TR R → + T { print(“+”) } R 1 R→ T → id { print(id. name) }

Example 1 Consider the following Syntax-Directed Definition, what is the generated intermediate code of

Example 1 Consider the following Syntax-Directed Definition, what is the generated intermediate code of the following expressions, Give the annotated parse trees : a)a+b*c b) x- (y + z) Production E → E 1 + T E → E 1 - T E→T T → T 1 * F T→F F→(E) F → id Semantic Rules E. loc=newtemp(), E. code = E 1. code || T. code || add E 1. loc, T. loc, E. loc=newtemp(), E. code = E 1. code || T. code || sub E 1. loc, T. loc, E. loc = T. loc, E. code=T. code T. loc=newtemp(), T. code = T 1. code || F. code || mult T 1. loc, F. loc, T. loc = F. loc, T. code=F. code F. loc = E. loc, F. code=E. code F. loc = id. name, F. code=“”

Answer 1 (a)

Answer 1 (a)

Answer 1 (b)

Answer 1 (b)

Example 2 Consider the following Syntax-Directed Definition, what is the generated intermediate code of

Example 2 Consider the following Syntax-Directed Definition, what is the generated intermediate code of the following expressions, Give the annotated parse trees : a ) If y>x then z+w b) while y>x do z+w Production Semantic Rules S while E do S 1 S. begin = newlabel(); S. after = newlabel(); S. code = gen(S. begin “: ”) || E. code || gen(‘jmpf’ E. place ‘, , ’ S. after) || S 1. code ||gen(‘jmp’ ‘, , ’ S. begin) || gen(S. after ‘: ”) S if E then S 1 S. after = newlabel(); S. code = E. code || gen(‘jmpf’ E. place ‘, , ’ S. after) || S 1. code || gen(S. after ‘: ”) S E S. code = E. code E → E 1 + E 2 E. place=newtemp(), E. code = E 1. code || E 2. code || gen(‘add’ E 1. place ‘, ’ E 2. place ‘, ’ E. place) E → E 1 relop E 2 E. place=newtemp(), E. code = E 1. code || E 2. code || gen(‘relop’ E 1. place ‘, ’ E 2. place ‘, ’ E. place) E → id E. place = id. name, E. code=‘ ‘

Answer 2 (a)

Answer 2 (a)

Answer 2 (b)

Answer 2 (b)