如何在 C++ 中为编译器进行组合?
How to make composition for compiler in C++?
我为 C++ 创建了编译器,我达到了代码生成级别(通过 MIPS),我想生成合成
YACC中的组成规则是:
variable:
object_access
|.....some rule not important here
;
object_access:
variable '.' name
|.....some rule not important here
;
当输入为:
class screw
{
int number;
screw();
}
class wheel
{
int type;
screw scw;// here composition
wheel();
}
class car
{
string model;
wheel whl;// here composition
}
void main()
{
car vec=new car();
cout<<vec.whl.scw.number;
}
当我访问 object_access 节点(生成代码)时,我可以得到(变量 && 名称)
我的问题是:
当我喜欢 (vec.whl.scw.number) 时,我不知道 (vec.whl.scw.number) 的长度,因为它递归有效。我需要长度因为(我们知道如果我加载(vec,whl,scw),我会得到地址但是如果我加载(数字)我会得到一个整数值。
所以如果我访问object_access节点递归我只需要通过vec的地址加载whl的地址并放入(例如$t1 ..),然后我加载地址通过 whl 的地址将 scw 放入 ($t1 ..例如),但是 (number is not object here I must not save it in ($t1) )..
我的问题:我如何知道 object_access 节点中的姓氏以停止加载地址?我需要 $t1 保存 object_access 节点中的最后一个地址,因为我将需要它进行另一个操作。
您可以使用您的符号 table 来了解您的(对象访问)中每个名称的类型...您需要将它们保存为地址的所有名称类型是(名称(那里的名称class)...I.e 不是 int 或 double...) 只有样本中的变量 (number) 有 (int or double or
除类型名称外的任何类型)
所以你可以知道你是否通过符号 table
的类型到达数字
我为 C++ 创建了编译器,我达到了代码生成级别(通过 MIPS),我想生成合成
YACC中的组成规则是:
variable:
object_access
|.....some rule not important here
;
object_access:
variable '.' name
|.....some rule not important here
;
当输入为:
class screw
{
int number;
screw();
}
class wheel
{
int type;
screw scw;// here composition
wheel();
}
class car
{
string model;
wheel whl;// here composition
}
void main()
{
car vec=new car();
cout<<vec.whl.scw.number;
}
当我访问 object_access 节点(生成代码)时,我可以得到(变量 && 名称)
我的问题是:
当我喜欢 (vec.whl.scw.number) 时,我不知道 (vec.whl.scw.number) 的长度,因为它递归有效。我需要长度因为(我们知道如果我加载(vec,whl,scw),我会得到地址但是如果我加载(数字)我会得到一个整数值。
所以如果我访问object_access节点递归我只需要通过vec的地址加载whl的地址并放入(例如$t1 ..),然后我加载地址通过 whl 的地址将 scw 放入 ($t1 ..例如),但是 (number is not object here I must not save it in ($t1) )..
我的问题:我如何知道 object_access 节点中的姓氏以停止加载地址?我需要 $t1 保存 object_access 节点中的最后一个地址,因为我将需要它进行另一个操作。
您可以使用您的符号 table 来了解您的(对象访问)中每个名称的类型...您需要将它们保存为地址的所有名称类型是(名称(那里的名称class)...I.e 不是 int 或 double...) 只有样本中的变量 (number) 有 (int or double or 除类型名称外的任何类型) 所以你可以知道你是否通过符号 table
的类型到达数字