编译和解释语言中的语法错误! [英] Syntax error in Compiled and Interpreted languages !

查看:134
本文介绍了编译和解释语言中的语法错误!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能解释一下在编译和解释语言中出现语法错误时会发生什么吗?假设已编译和解释的语言的第7行出现语法错误.如果我尝试编译/解释源代码,结果将是什么?

PS:这个问题对于一些经验丰富的人来说可能听起来很愚蠢,但是我对编程世界是一个全新的想法.

Can anyone explain me what happens when there is a syntax error in compiled and interpreted language ? Suppose there is a syntax error in line number 7 of a compiled and interpreted language. What would be the result if I try to compile/interpret the source code ?

PS: This question might sound silly to some of the experienced ones but I''m totally new to the World of programming.

推荐答案

基本上,如果有编译的话错误,则无法运行.
语法错误表示编译器/解释器在说我不知道您的意思",因此,在您修复它之前,它无法生成任何代码或执行任何命令.

有点像我告诉你
Basically, if there is a compilation error, then you can''t run it.
A syntax error means the compiler / interpreter is saying "I do not know what you mean by this", so it cannot generate any code or execute any commands until you have fixed it.

It''s a bit like me telling you to
Go coffee kitchen (cup make milk) sugar two and six

,而不是

Go kitchen and make six cup coffee (milk two sugar)


编译语言和解释语言甚至编译之间都有本质的区别和解释使用相同的语言.

如果有编译错误,编译将不会产生任何结果.但是,某些编译会尝试继续编译,以立即显示更多错误(例如,C ++编译器通常会这样做).但是,将永远不会创建编译,目标或可执行文件的结果.使用目标文件,情况会更加复杂:可能会创建一些目标文件,但不会创建整个目标文件.

通过解释,情况就不同了.在解释和执行的周期中,常规异常和语法错误之间没有明显区别.尤其是考虑到解释程序可以动态修改其源代码时,尤其如此.系统可以解释并执行部分命令流,然后在错误解释无法执行的块处停止并显示错误消息.原则上,甚至可以将其作为异常处理并在错误发生后继续执行.此类功能的典型示例是JavaScript.

—SA
There is an essential difference between compiled and interpreted languages, or even between compilation and interpretation use of the same language.

The compilation will not produce anything if there is a compilation error. However, some compile would try to continue compilation just to show some more errors at once (C++ compilers usually do that, for example). Nevertheless, the result of compilation, object or executable file will never be created. With object files, situation is more complex: some of the object files may be created, but the whole set will not.

With interpretation, the situation is different. In the cycle of interpretation and execution, there is not distinct difference between regular exception and syntax error. Especially if you take into account that an interpretative program can modify its source code on the fly. The system may interpret and execute part of the command flow, and then stop with error message at the chunk where interpretation is impossible. In principle, it''s even possible to handle it as exception and continue execution after the error. A typical example of such functionality is JavaScript.

—SA


这确实取决于语言.从理论上讲,编译器或解释器可以忽略语法错误,而对其余有效代码进行编译或解释.但是,实际上,解释型语言有时会忽略语法错误并跳过无效代码.在像JavaScript这样的具有分号和花括号(或到结束行的其他字符)的语言中,从错误中恢复是一件相当简单的事情……解释器只需找到这些字符之一,然后再开始解释.

JavaScript是一种通常可以解释的语言(尽管没有什么可以阻止它被编译).在典型的JavaScript实现(Internet Explorer,Chrome,Firefox)中,语法错误将被忽略或向用户报告,并且JavaScript将继续被解释.

C#是一种通常被编译的语言(尽管没有什么可以阻止它被解释).在典型的C#实现中(Microsoft,Mono),语法错误将导致编译器报告错误,并且(EXE,DLL等的)构建将停止.编译器可能会报告多个编译错误(表明编译器能够忽略语法错误以查找更多语法错误),但是如果代码包含语法错误,典型的编译器将不会输出可以运行的文件. br/>
请记住,有数百种(也许数千种)编程语言和相应的编译器/解释器.某些语言,例如QuickBasic 4.5,甚至具有IDE,它们使程序员可以选择是否要在解释器中运行程序或希望对其进行编译.每种编程语言都有一点不同,有些可能与编译/解释的语言通常不符合.
It really depends on the language. In theory, a compiler or interpreter could ignore a syntax error and compile or intepret the rest of the valid code. In practice, however, interpreted languages will sometimes ignore syntax errors and skip over the invalid code. In languages like JavaScript that have semicolons and curly braces (or some other character to end lines), recovering from the error is a fairly simple matter... the interpreter just has to find one of those characters and start interpreting again at that point.

JavaScript is one language that is typically interpreted (though there''s nothing stopping it from being compiled). In typical JavaScript implementations (Internet Explorer, Chrome, Firefox), syntax errors will either be ignored or reported to the user and the JavaScript will continue to be interpreted.

C# is one language that is typically compiled (though there''s nothing stopping it from being interpreted). In typical C# implementations (Microsoft, Mono), syntax errors will cause the compiler to report an error and the build (of EXE''s, DLL''s, and so on) will halt. Multiple compile errors may be reported by the compiler (indicating that the compiler is capable of ignoring syntax errors in order to find more syntax errors), but the typical compilers will not output a file that can be run if the code contains syntax errors.

Keep in mind that there are hundreds (perhaps thousands) of programming languages and corresponding compilers/interpreters. Some languages, such as QuickBasic 4.5, even had IDE''s that allowed the programmer to choose if they wanted to run the program in an interpreter or if they wanted it compiled. Each programming language out there is a little different and some may not conform to what is typically done with compiled/interpreted langauges.


这篇关于编译和解释语言中的语法错误!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆