最近在作编译器时遇到静态做用域和动态做用域的抉择问题。
以前没有仔细探究,作的时候才发现问题。javascript
静态做用域,在编译阶段就作完全部name check的工做,像C语言:java
{ // Push scope 1 table int a = 10; // Insert (a, 10) to scope 1 { // Push scope 2 table int a = 15; // Insert (a, 15) to scope 2 int b = 20; // Insert (b, 20) to scope 2 a = 100; // Use a variable // Pop scope 2 table } // Pop scope 2 table }
Scope 1 | |
a | 10 |
Scope 2 | |
a | 15 |
b | 20 |
在语义分析阶段的时候进行top-down parse,每进入一个block就push一张符号表,离开block时候就pop一张符号表,
使用一个变量时就依次从最后一个符号表向前找,若是找不到就该报undefined或者undeclared错误了。因此C语言的函数不能在定义前调用。由于在解析main的函数体时hello还没插入到符号表里。ide
int main(){ hello(); // Use of undeclared identifier "hello" return 0; } void hello(){};
动态做用域,像javascript这样,就能够在函数定义以前调用函数
var a = function(){ b(); // Run-time name check } var b = function(){} a();
a()->global
由于这时候再也不是编译时的name check而是运行时的name check,做用域是函数做用域,在运行a函数而且执行到b()的时候,会在当前函数做用域找b的值,若是找不到就往上一个函数做用域寻找,直到global做用域。也就说,只要保证在函数运行到这个位置的时候,在做用域链里存在这个变量,而与定义顺序无关。spa