Syntax vs. Semantic Error
Each programming language has its own set of grammatical rules, determining the syntax structures and terms that can appear in code statements. Dealing with errors is a major element in programming, so developers need to learn how to handle different types, including syntax and semantic errors. Syntax errors arise due to incorrect use of language structures, and semantic errors arise because of logical mistakes. These two types of errors typically become apparent at different phases in programming projects.
-
Programming Languages
-
When programmers learn to code in specific languages, they learn the vocabulary and grammar within those languages. When a programmer writes a line of code in which the language structures are incorrect, this is a syntax error. The following sample Java code demonstrates a valid line of code:
int myNum = 5;The following alteration illustrates a syntax error:
Int myNum = 5;Java is case sensitive, so the incorrect use of an upper case initial letter for the "int" variable declaration constitutes a syntax error. There are lots of possible syntax errors, many of which are simple typing mistakes. If a programmer uses an Integrated Development Environment, he will see syntax errors highlighted as he types code.
Logic
-
Each program involves logical steps of processing. Programmers dictate what will happen when a program executes using control structures such as loops and conditional statements. Semantic errors are errors in these logical structures. For example, the following JavaScript code demonstrates a valid loop:
var counter:
var myArray = new Array(3, 7, 2);
for(counter=0; counter<myArray.length; counter++) {
document.write(myArray[counter]);
}This code iterates through the array structure until it reaches the end. The loop stops when it reaches the final element because the conditional test checks that the counter variable is no larger than the array length minus one, which is the final index in the structure. The following altered version includes a semantic error:
var counter:
var myArray = new Array(3, 7, 2);
for(counter=0; counter<=myArray.length; counter++) {
document.write(myArray[counter]);
}This time the loop, with an extra equal sign in the second line, will iterate when the counter is equal to the array length, which is out -- with its bounds. This may cause the script to crash when it executes.
-
Execution
-
Depending on the language in question, some syntax errors prevent a program from executing at all. For example, when using the Eclipse IDE to create Java programs, developers cannot compile and run their applications unless they have no syntax errors in them. This creates a working model in which developers identify syntax errors at an earlier stage. However, this only applies to syntax errors, as semantic errors will not normally prevent a program from compiling and running.
Testing
-
Programmers typically do not become aware of semantic errors until the testing phase of a project. A program can contain semantic errors and still execute. Depending on the program and on the testing process, a semantic error may go undetected until extensive tests have been carried out. The processes of testing and debugging provide developers with the opportunity to identify and rectify syntax and semantic errors.
-
References
Resources
- Photo Credit Jupiterimages/Comstock/Getty Images