Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: compiler
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.
Posted 14-Nov-11 4:17am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
instead of
Go kitchen and make six cup coffee (milk two sugar)
  Permalink  
Comments
Kumar Bhandari at 14-Nov-11 11:25am
   
What I wanted to ask was, suppose there is a syntax error then will that code be still compiled or not ? What I've learned till now is compiler creates a separate executable file and then runs it. So, if there is/are any such syntax error then will that code be compiled or not ? What would be the result in case of an interpreter ?
OriginalGriff at 14-Nov-11 11:57am
   
No.
It's like trying to provide a translation of Korean text into French, if you only know English. You cannot even start to produce any output. The compiler has the same problem.
SAKryukov at 14-Nov-11 12:18pm
   
Not as simple. It's a bit more complex matter. With interpretative languages one can even continue after a syntax error.
Please see my answer.
--SA
johannesnestler at 14-Nov-11 11:40am
   
The definition of "syntax error" in the context of a compiler is: CAN'T RUN IT. So: The compiler does not compile anything because he doesn't understand what you typed. Same goes for an interpreter - can't run. I think OriginalGriffs explanation is quite good...
SAKryukov at 14-Nov-11 12:18pm
   
Not as simple. It's a bit more complex matter. With interpretative languages one can even continue after a syntax error.
Please see my answer.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 540
1 OriginalGriff 300
2 CPallini 290
3 ChauhanAjay 124
4 George Jonsson 120
0 OriginalGriff 6,474
1 Sergey Alexandrovich Kryukov 6,190
2 CPallini 5,180
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 14 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100