名字解析 (程序设计)计算机程序设计语言中,名字解析是指把程序表达式中的标记(token)对应解析到程序成分(program components)。 概述不同语言的名字解析算法的复杂度不同。例如,汇编语言的名字解析只需要简单地查关联表。而C++的名字解析就非常复杂,受命名空间、作用域、可见性规则(visibility rules)、函数重载、可访问性(accessibility)影响。 静态解析与动态解析编译时完成的称静态名字解析;运行时完成的称动态名字解析。 动态类型并不意味着动态名字解析。例如,Erlang是动态类型但静态名字解析。 下述Python程序: >>> locals()['num'] = 999 # equivalent to: num = 999
>>> noun = "troubles"
>>> noun2 = "hound"
>>> # which variables to use are decided at runtime
>>> print("{num} {noun} and a {noun2} ain't one".format(**locals()))
999 troubles and a hound ain't one
但现在的Python编程风格指引不建议使用动态名字解析。[1][2][3] 静态名字解析的语言有:C语言, C++, E语言, Erlang, Haskell, Java, Pascal语言, Scheme语言, Smalltalk。动态名字解析的语言有:Lisp, Perl, PHP, Python, REBOL, Tcl. 名字屏蔽名字屏蔽(name Masking)发生在同一个名字用于不同的实体,出现在重叠的作用域内。 例如,在下述Java程序中: private int foo; // A declaration with name "foo" in an outer scope
public void setFoo(int foo) { // A declaration with the same name in the inner scope
// "foo" is resolved by looking in the innermost scope first,
// so the author uses a different syntax, this.foo, to refer to the name "foo"
// in the outer scope.
this.foo = foo;
}
// "foo" here means the same as this.foo below,
// since setFoo's parameter is no longer in scope.
public int getFoo() { return foo; }
α更名简化了名字解析程序设计语言使用α-变换使得没有变量名屏蔽了其它同名的实体。可用于静态代码分析,使得理解源代码更为容易。 例如: class Point {
private:
double x, y;
public:
Point(double x, double y) { // x and y declared here mask the privates
setX(x);
setY(y);
}
void setX(double newx) { x = newx; }
void setY(double newy) { y = newy; }
}
Point构造函数中,类的数据成员x与y被局部变量屏蔽了。这可通过α更名改善: class Point {
private:
double x, y;
public:
Point(double a, double b) {
setX(a);
setY(b);
}
void setX(double newx) { x = newx; }
void setY(double newy) { y = newy; }
}
参见参考文献
|
Index:
pl ar de en es fr it arz nl ja pt ceb sv uk vi war zh ru af ast az bg zh-min-nan bn be ca cs cy da et el eo eu fa gl ko hi hr id he ka la lv lt hu mk ms min no nn ce uz kk ro simple sk sl sr sh fi ta tt th tg azb tr ur zh-yue hy my ace als am an hyw ban bjn map-bms ba be-tarask bcl bpy bar bs br cv nv eml hif fo fy ga gd gu hak ha hsb io ig ilo ia ie os is jv kn ht ku ckb ky mrj lb lij li lmo mai mg ml zh-classical mr xmf mzn cdo mn nap new ne frr oc mhr or as pa pnb ps pms nds crh qu sa sah sco sq scn si sd szl su sw tl shn te bug vec vo wa wuu yi yo diq bat-smg zu lad kbd ang smn ab roa-rup frp arc gn av ay bh bi bo bxr cbk-zam co za dag ary se pdc dv dsb myv ext fur gv gag inh ki glk gan guw xal haw rw kbp pam csb kw km kv koi kg gom ks gcr lo lbe ltg lez nia ln jbo lg mt mi tw mwl mdf mnw nqo fj nah na nds-nl nrm nov om pi pag pap pfl pcd krc kaa ksh rm rue sm sat sc trv stq nso sn cu so srn kab roa-tara tet tpi to chr tum tk tyv udm ug vep fiu-vro vls wo xh zea ty ak bm ch ny ee ff got iu ik kl mad cr pih ami pwn pnt dz rmy rn sg st tn ss ti din chy ts kcg ve
Portal di Ensiklopedia Dunia