gpt4 book ai didi

编译器和解释器之间的混淆?

转载 作者:太空狗 更新时间:2023-10-29 15:50:37 24 4
gpt4 key购买 nike

我在某处阅读了以下关于编译器和解释器的文档:-

A compiler searches all the errors of a program and lists them. If the program is error free then it converts the code of program into machine code and then the program can be executed by separate commands.

An interpreter checks the errors of a program statement by statement. After checking one statement, it converts that statement into machine code and then executes that statement. The process continues until the last statement of program occurs.

我的疑惑来自于下面的代码:

int main()
{
printf("hello")
scanf("%d",&j);
return 0;
}

我正在使用 MINGW GCC 编译器。当我编译上面的代码时,发生了以下事情:

首先我得到错误

error: expected ';' before 'scanf()'

在我更正上面的错误之后,我得到了第二个错误

error: 'j' undeclared (first use in this function)

所以我想知道为什么这两个错误没有同时列出?

最佳答案

编译器和解释器在技术上是两个不同的东西,尽管有时界限可能非常不稳定。

编译器基本上就是一个语言翻译器。它以源语言作为输入并生成目标语言作为输出。

解释器采用一种语言(无论是高级语言还是低级语言)并执行该语言描述的代码。

造成混淆的主要原因是大多数现代脚本语言都包含编译器和解释器,其中编译器采用脚本语言并创建一个较低级别的等效项(类似于二进制机器语言),然后由解释器读取和执行。


至于编译器错误的问题,很可能是因为编译器由于第一个错误而无法继续解析 scanf 调用,而只是跳过它(包括未声明的变量)。

你还应该知道,在 C 中,一些错误实际上会导致代码中出现更多错误,例如,在其他方面是正确的

int j
printf("Enter something: ");
scanf("%d", &j);

由于在声明变量 j 后缺少分号,您会收到错误,但您也会收到 scanf 行的错误,因为编译器可以找不到变量 j,即使 scanf 调用在其他方面是正确的。

在不相关的代码中会产生后续错误的另一个典型错误示例是忘记头文件中结构的终止分号。如果它是最后一个结构,您甚至可能不会在头文件中收到任何错误,而只会在包含头文件的源文件中收到不相关的错误。

关于编译器和解释器之间的混淆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21475744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com