fbpx
维基百科

名字解析 (程序设计)

计算机程序设计语言中,名字解析是指把程序表达式中的标记token)对应解析到程序成分(program components)。

概述 编辑

不同语言的名字解析算法的复杂度不同。例如,汇编语言的名字解析只需要简单地查关联表英语Associative array。而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构造函数中,类的数据成员xy被局部变量屏蔽了。这可通过α更名改善:

 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; }  } 


参见 编辑

参考文献 编辑

  1. ^ [Python-Ideas] str.format utility function. 9 May 2009 [2011-01-23]. (原始内容于2018-07-14). 
  2. ^ 8.6. Dictionary-based string formatting. diveintopython.org. Mark Pilgrim. [2011-01-23]. (原始内容于2019-12-23). 
  3. ^ 9. Classes - Python v2.7.1 documentation. [2011-01-23]. (原始内容于2012-10-23). search for names is done dynamically, at run time — however, the language definition is evolving towards static name resolution 

名字解析, 程序设计, 计算机程序设计语言中, 名字解析是指把程序表达式中的标记, token, 对应解析到程序成分, program, components, 目录, 概述, 静态解析与动态解析, 名字屏蔽, α更名简化了名字解析, 参见, 参考文献概述, 编辑不同语言的名字解析算法的复杂度不同, 例如, 汇编语言的名字解析只需要简单地查关联表, 英语, associative, array, 而c, 的名字解析就非常复杂, 受命名空间, 作用域, 可见性规则, visibility, rules, 函数重载, . 计算机程序设计语言中 名字解析是指把程序表达式中的标记 token 对应解析到程序成分 program components 目录 1 概述 2 静态解析与动态解析 3 名字屏蔽 4 a更名简化了名字解析 5 参见 6 参考文献概述 编辑不同语言的名字解析算法的复杂度不同 例如 汇编语言的名字解析只需要简单地查关联表 英语 Associative array 而C 的名字解析就非常复杂 受命名空间 作用域 可见性规则 visibility rules 函数重载 可访问性 accessibility 影响 静态解析与动态解析 编辑编译时完成的称静态名字解析 运行时完成的称动态名字解析 动态类型并不意味着动态名字解析 例如 Erlang是动态类型但静态名字解析 下述Python程序 gt gt gt locals num 999 equivalent to num 999 gt gt gt noun troubles gt gt gt noun2 hound gt gt gt which variables to use are decided at runtime gt gt gt 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 a更名简化了名字解析 编辑程序设计语言使用a 变换使得没有变量名屏蔽了其它同名的实体 可用于静态代码分析 使得理解源代码更为容易 例如 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被局部变量屏蔽了 这可通过a更名改善 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 参见 编辑命名空间 作用域 命名冲突 英语 Naming collision 参考文献 编辑 Python Ideas str format utility function 9 May 2009 2011 01 23 原始内容存档于2018 07 14 8 6 Dictionary based string formatting diveintopython org Mark Pilgrim 2011 01 23 原始内容存档于2019 12 23 9 Classes Python v2 7 1 documentation 2011 01 23 原始内容存档于2012 10 23 search for names is done dynamically at run time however the language definition is evolving towards static name resolution 取自 https zh wikipedia org w index php title 名字解析 程序设计 amp oldid 63728577, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。