Difference Between a Syntax Error and a Semantic Error

Difference Between a Syntax Error and a Semantic Error thumbnail
Programming errors may be frustrating but are sometimes unavoidable.

Programming can be a complex task. No programmer writes perfect code the first time or every time. For this reason, learning to find and fix the mistakes in your code is a major element in any programmer's job. Programming errors are also varied so the techniques used to locate and address them need to be tailored to suit. Syntax errors occur when code fails to correctly observe the grammatical rules of a programming language. Semantic errors occur when code includes logical mistakes.

  1. Syntax

    • Programming languages, like natural languages, each have their own set of grammatical rules. These rules specify the text characters and structures that programmers can use to deliver their applications. In most cases a section of programming code will include alphabetical and numeric characters as well as punctuation symbols. These symbols often have special meanings within a language. The following sample PHP code demonstrates a correct statement:
      $some_name = "Martha";

      The following amended version contains two syntax errors:
      some_name = "Martha;

      The variable name is not correctly indicated and the closing set of quotes for the variable value is missing.

    Logic

    • When developers create programming code to implement a particular area of application functionality, they need to outline tasks for the computer to carry out, using statements. These statements often appear within complex control structures, including loops and conditionals. When using control structures certain semantic or logical errors are common. The following sample JavaScript code demonstrates creating an array, then accessing an element:
      var myPals=["bob", "joe", "sam"];
      document.write(myPals[0]);

      This code writes the first array element into the Web page. The following amended version contains a semantic error:
      var myPals=["bob", "joe", "sam"];
      document.write(myPals[3]);

      This code attempts to read from an element position that is outside the array's range. The final element in the array is at position two so this code may cause the program to crash.

    Developing

    • Depending on the development practices a programmer uses some errors may be easier than others to identify during the coding process. For example when using an Integrated Development Environment or a code editor that highlights syntax some errors will be immediately visible. When writing Java applications in an IDE such as Eclipse, programmers are unable to compile and run code that contains syntax errors which are also visibly highlighted within the interface. However, semantic errors often go unnoticed until the code executes when they can cause programs to crash or function incorrectly.

    Testing

    • Syntax errors are detected during the development process in many cases but this does depend on the platform in use. For example when developing a website using PHP scripts developers are able to upload code with syntax errors onto websites. When these scripts are fetched within the Web browser they may display error messages. The testing process for any application should help to identify semantic errors which may only become visible when developers check the behavior of an application while it runs.

Related Searches:

References

Resources

  • Photo Credit Zedcor Wholly Owned/PhotoObjects.net/Getty Images

Comments

Related Ads

Featured