加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 10:08:55 所属栏目:百科 来源:网络整理
导读:我在某处阅读了有关编译器和解释器的以下文档: – 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 sep
我在某处阅读了有关编译器和解释器的以下文档: –

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之后缺少分号,您将收到错误,但是由于编译器无法找到变量j,因此您也会得到scanf行的错误,即使scanf调用是正确的.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读