6.3 节的练习¶
6.3.1¶
确定下列声明序列中各个标识符的类型和相对地址。
float x;
record {float x; float y;} p;
record {int tag; float x; float y;} q;
解答¶
SDT
S -> {top = new Evn(); offset = 0;}
D
D -> T id; {top.put(id.lexeme, T.type, offset);
offset += T.width}
D1
D -> ε
T -> int {T.type = interget; T.width = 4;}
T -> float {T.type = float; T.width = 8;}
T -> record '{'
{Evn.push(top), top = new Evn();
Stack.push(offset), offset = 0;}
D '}' {T.type = record(top); T.width = offset;
top = Evn.top(); offset = Stack.pop();}
标识符类型和相对地址
line id type offset Evn
1) x float 0 1
2) x float 0 2
2) y float 8 2
2) p record() 8 1
3) tag int 0 3
3) x float 4 3
3) y float 12 3
3) q record() 24 1
6.3.2 !¶
将图 6-18 对字段名的处理方法扩展到类和单继承的层次结构。
- 给出类 Evn 的一个实现。该实现支持符号表链,使得子类可以重定义一个字段名,也可以直接引用某个超类中的字段名。
- 给出一个翻译方案,该方案能够为类中的字段分配连续的数据区域,这些字段中包含继承而来的域。继承而来的字段必须保持在对超类进行存储分配时获得的相对地址。