为什么编译器更喜欢词法作用域?

发布于 2024-07-24 01:45:19 字数 34 浏览 7 评论 0原文

词法作用域如何帮助编译器? 它对编译或优化有帮助吗?

How does lexical scope help the compilers? Does it help in compilation or optimization?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

诗酒趁年少 2024-07-31 01:45:19

我确实认为词法作用域有助于编译器和优化。 但这取决于你所说的帮助是什么意思。

词法或静态作用域允许编译器在本地引用时(这意味着在其词法上下文中)证明变量的可用性。 它必须位于引用变量的方法的范围内。

要在动态作用域环境中执行此操作,必须考虑所有调用上下文,因为函数也知道其调用上下文也知道的所有变量。 为了确保变量可供引用,在编译时需要对所有调用上下文进行递归回溯。

由于这非常复杂,因此在编译时会被省略,在运行时抛出异常。

请参阅此处:相比之下,在动态作用域中,您首先在本地函数中搜索,然后再搜索在调用本地函数的函数中搜索,然后在调用该函数的函数中搜索,依此类推,在调用堆栈中向上搜索。 “动态”指的是变化,因为每次调用给定函数时,调用堆栈都可能不同,因此该函数可能会根据调用位置而命中不同的变量。

I do think that lexical scope helps the compiler and optimization. It depends what you mean by help though.

Lexical or static scope allow the compiler to proof availability of a variable when referenced locally, that means within its lexical context. It has to be in the scope of the method referencing the variable.

To do such in dynamic scope environments, all calling contexts have to be taken into consideration, as a function knows all variables its calling context knows as well. To make sure a variable is available for reference, a recursive backtrack of all calling contexts would be necessary at compile time.

As this is very complicated, it would be omitted at compile time, throwing exceptions at runtime.

See here: In dynamic scoping, by contrast, you search in the local function first, then you search in the function that called the local function, then you search in the function that called that function, and so on, up the call stack. "Dynamic" refers to change, in that the call stack can be different every time a given function is called, and so the function might hit different variables depending on where it is called from.

烟酒忠诚 2024-07-31 01:45:19

词法(或静态)作用域减少了编译器将文本正确转换为代码所需的信息量。 它可以帮助编译,因为编译器不需要添加必须在运行时访问的附加信息(就像动态作用域的情况一样)。 对于优化,编译器不需要考虑可能存在于其他作用域中的变量,因为您可以访问局部变量或全局变量,仅此而已。

Lexical (or static) scoping reduces the amount of information a compiler needs to correctly transform text into code. It can help compilation because the compiler does not need to add additional information that must be accessed at runtime (as is the case with dynamic scoping). For optimizations, the compiler does not need to consider variables that could exist in other scopes, as you can either access local variables or global variables and nothing else.

何以心动 2024-07-31 01:45:19

词法作用域不会帮助编译器或优化代码。 这是语言设计的决定。 请参阅此问题了解更多说明。

Lexical scope does not help the compiler or optimise the code. It is a language design decision. See this question for more explanation.

很快妥协 2024-07-31 01:45:19

对于动态范围,许多优化都是不可能的,因为您无法保证使优化成为可能的约束。

这对于不保证存储大小或表示的动态语言尤其重要。

例如,动态类型语言的编译器可能能够获取装箱对象的链接列表,并将其替换为无符号 8 位字节数组,如果它可以证明列表的元素始终是 0 到 255 之间的整数当你正确使用静态作用域时,这种事情很容易证明,并且可以导致空间和计算效率的巨大增加。

调试词法范围的代码通常也更容易,因为在调试中跟踪动态变量要困难得多。 定义事物的地方有一种意大利面条式代码,类似于 goto 和过度使用全局变量引起的问题。

With dynamic scope, many optimizations are impossible, because you cannot guarantee the constraints that make the optimization possible.

This is especially important in dynamic languages that do not guarantee storage sizes or representations.

For example a compiler for a dynamically typed language may be able to take a linked list of boxed objects and replace it with an array of unsigned 8 bit bytes, if it can prove that the elements of the list will always be integers between 0 and 255. This sort of thing is easy to prove with static scope when you use it properly, and can cause huge increase with both space and computational efficiency.

It is also often easier to debug lexically scoped code, because dynamic variables are much harder to track down in debugging. There is kind of a spaghetti-code of where things may have been defined, similar with the problems caused by goto and overuse of global variables.

醉生梦死 2024-07-31 01:45:19

很简单地说,词法(或静态)作用域在语言是静态类型时有帮助,动态作用域在语言是动态类型时有帮助。

在动态作用域中,变量的作用域在运行时确定。 如果声明为 int 的变量在另一个词法环境中使用,其中同名变量的类型为 float。 唯一的是将它们视为两个不同的变量,这意味着变量必须携带类型信息,在大多数静态类型系统中,类型信息根本不进入目标代码。 一个程序只有在能够证明不会发生类型错误时才会简单地编译,然后当然就不再需要类型信息了。

由于这个原因,具有词法作用域的动态语言通常需要使用堆而不是堆栈来分配运行时信息。

Very simply said, lexical (or static) scope helps when a language is statically typed, dynamic scope helps when a language is dynamically typed.

In dynamic scope, the scope of a variable is resolved at runtime. If a variable declared int is used in another lexical environment where a variable with the same name has type float. The only was it to consider them two different variables, which means that variables must carry type information, in most static type systems, type information does not enter the object code at all. A program will simple only compile once it can prove that no type errors will occur, and then no type information is needed any more of course.

Dynamic languages which have lexical scope often need to use a heap rather than a stack to allocate runtime information to for this reason.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文