Header Ads Widget

Syntax Error

Syntax Error

During the syntax analysis phase, this type of error appears. Syntax error is found during the execution of the program.

Some syntax error can be:

  • Error in structure
  • Missing operators
  • Unbalanced parenthesis

When an invalid calculation enters into a calculator then a syntax error can also occurs. This can be caused by entering several decimal points in one number or by opening brackets without closing them.

For example 1: Using "=" when "==" is needed.

  1. 16   if (number=200)  
  2.     17         count << "number is equal to 20";  
  3.     18   else   
  4.     19      count << "number is not equal to 200"  

The following warning message will be displayed by many compilers:

Syntax Warning: assignment operator used in if expression line 16 of program firstprog.cpp

In this code, if expression used the equal sign which is actually an assignment operator not the relational operator which tests for equality.

Due to the assignment operator, number is set to 200 and the expression number=200 are always true because the expression's value is actually 200. For this example the correct code would be:

  1. 16   if (number==200)  

Example 2: Missing semicolon:

  1. int a = 5          // semicolon is missing  

Compiler message:

  1. ab.java:20';' expected  
  2. int a = 5  

Example 3: Errors in expressions:

  1. x = (3 + 5;  // missing closing parenthesis ')'  
  2. y = 3 + * 5;   // missing argument between '+' and '*'  

Post a Comment

0 Comments