Tải bản đầy đủ (.pdf) (3 trang)

Tutorial 4 solutions

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (65.79 KB, 3 trang )

Week 9 Solutions
Syntax-Directed Translation
1. Write a syntax-directed definition based on the following grammars to calculate
the value of a binary number.
a. B → B 0 | B 1 | 0 | 1
Production
B → B1 0
B → B1 1
B→ 0
B→ 1

Semantic rule
B.val = B1.val * 2
B.val = B1.val * 2 + 1
B.val = 0
B.val = 1

b. B → 0 B | 1 B | 0 | 1
Production
B → 0 B1
B → 1 B1
B→ 0
B→ 1

Semantic rule
B.val = B1.val; B.len = B1.len + 1
B.val = 2B1.len + B1.val ; B.len = B1.len + 1
B.val = 0; B.len = 1
B.val = 1; B.len = 1

2. Given the following grammar for an assignment expression, write a syntaxdirected definition to check if the LHS of an assignment is id. Otherwise, print out


“error”.
E→T=E|T
T→T+F|F
F → id | num | ( E )
Production
E → T = E1
E→T
T→ T+F
T→ F
F → id
F → num
F→(E)

Semantic rule
if (!T.isLeft) print(“error”)
E.isLeft = T.isLeft
T.isLeft = false
T.isLeft = F.isLeft
F.isLeft = true
F.isLeft = false
F.isLeft = false


3. Let a constant expression be an expression whose operands are constants or
constant expressions. Given the following grammar for an expression, construct a
translation scheme to print out “Divide by zero” if the denominator in a divide
expression is a constant-expression and its value is 0.
E → T E’
E’ → - T E’ | / T E’ | ∈
T → id | num | ( E )

E→T
{E’.iConstant = T.oConstant;
if (E’.iConstant) E’.ival = T.oval}

E’

E’ → - T

{E.oConstant = E’.oConstant;
E.oval = E’.oval}
{E1’.iConstant = T.oConstant && E’.iConstant;
if (E1’.iConstant) E1’.ival = E’.ival - T.oval}

E1’

E’ → / T

{E’.oConstant = E1’.oConstant;
if(E’.oConstant) E’.oval = E1’.oval}
{if (T.oConstant && T.oval == 0)
{print(“Divide by zero”); exit()}
E1’.iConstant = T.oConstant && E’.iConstant;
if (E1’.iConstant) E1’.ival = E’.ival / T.oval}

E1’

E’ → ∈

T → id
T → num


T→ (E)

{E’.oConstant = E1’.oConstant;
if(E’.oConstant) E’.oval = E1’.oval}
{E’.oConstant = E’.iConstant;
if (E’.oConstant) E’.oval = E’.ival}
{T.oConstant = false}
{T.oConstant = true;
T.oval = new Integer(num.Lexeme)}
{T.oConstant = E.oConstant;
if (E.oConstant) T.oval = E.oval}


4. Transform the following translation scheme such that the attributes of its
nonterminals can be evaluated by a top-down parser.
A → A1 B { A.a = f(A1.a, B.b) }
A → C { A.a = g(C.c) }
Read pages 304 of text book
Transform the underlying grammar:
A → C A’
A’ → B A1’
A’ → ∈
Transform the translation scheme:
A → C { A’.i = g(C.c) }
A’ {A.a = A’.o}
A’ → B {A1’.i = f(A1’.i,B.b)}
A’ {A’.o = A1’.o}
A’ → ∈ {A’.o = A’.i}
5. Transform the following translation scheme such that its semantic actions can be

evaluated by a bottom-up parser.
D → T { L.in = T.type } L
D → T [ ] { L.in = new ArrayType(T.type) } L
T → int { T.type = new IntType() }
T → float { T.type = new FloatType() }
L → { L1.in = L.in } L1 , id { addtype(id.entry,L.in) }
L → id { addtype(id.entry,L.in) }
Transform the second production:
D → T [ ] { M.i = T.type } M { L.in = M.o } L
M → ∈ {M.o = new ArrayType(M.i) }
Production
D→TL
D→T[]ML
M→∈
T → int
T → float
L → L , id
L → id

Semantic rule

val[ntop] = new ArrayType(val[top-2])
val[ntop] = new Integer()
val[ntop] = new FloatType()
addtype(val[top],val[top-3])
addtype(val[top],val[top-1])




Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×