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

Tutorial 4

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 (41.31 KB, 1 trang )

COMPILER CONSTRUCTION
Week 9 Tutorial Questions

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
b. B → 0 B | 1 B | 0 | 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 )
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 )
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) }
5. Transform the following translation scheme such that its semantic actions can be
performed 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) }
Hint: Read pages 311-315 of text book.



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

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