Role of Compiler.

A part of the compiler's job is to analyze the program code for "correctness". If the meaning of a program is correct, then a compiler can not detect errors (e.g. if different statements are used etc.), but a compiler can certainly detect errors in the form of a program.

Some common form of program errors are below:

1. Syntax Errors

Syntax errors occur when rules of a programming language are misused i.e., when a grammatical rule of C++ is violated.
For instance, in the following program segment, 
  1. main()
  2. {
  3. int a,b:
  4. cin>>a>>b;
  5. cout<<a+b,
  6. return 0
  7. }

the first statement of main() produces a syntax error as the statement is terminated by : rather than ;. The third statement also has the similar error, it is terminated by, rather than ; and the last statement also statement also has missing semicolon (;), again an error.

2. Semantics Error

Semantic errors occur when statements are not meaningful. Semantics refers to the set of rules which give the meaning of a statement. For instance, the statement 'Sita plays Guitar' is syntactically and semantically correct as it has some meaning but the statement 'Guitar plays Sita' is syntactically correct (as the syntax is correct) nut semantically incorrect. Similarly, their are semantics rules of a programming language, violation of which results in semantical errors. For instance, the statement
X*Y=Z
will result in a semantical error as an expression can not come on the left side of an assignment statement.

3. Type Errors

Data in C++ has an associated data type. The value 7, for instance, is an integer. 'a' is a character constant whereas "hi" is a string. If a function  is given wrong type of data, type error is signalled by the compiler. For instance, if the  integer argument was expected but actually string was given in place of it, it is a type error.

4. Run-time errors (Execution Errors)

A run-time error is that occurs during the execution of a program. It is caused of some illegal operation taking place or in-availability of desired or requited conditions for the execution of the program. For instance, if a program is trying to open a file which does not exist or it could not be opened, it results int an execution error. Similarly, if enough memory is not available or an expressio is trying to divide a number by zero are run-time errors.

5. Logical Errors

A logical error is that error which caused a program to produce incorrect or undesired output. For instance , if we are trying to print the table of a number 3 and if we say

ctr=1
while (ctr>10)
{ cout<<n*ctr;
ctr=ctr+1;
}

Here the loop would not be executed even once as the condition (ct>10)is not fulfilled at all. Therefore, no output will be produced. such an error is logical error.
Consider other example of logical error. If the following code has been written with an intention to compute average:
a=7;b=3;
c=5;
avg=a*b*c/3;

You yourself can determine that above code is syntactically correct but will not produce correct output as it has a logical error in it.

A compiler reports an error by flashing a n error message. an error message
contains a line number and a brief description of the error. Often a single error can cause a compiler to report more errors than actually are present. After correction the program, the program should be recompiled and retested.

The second thing, a compiler does is , it translates the corrected program text into object or assembly instruction text understood by the computer. This process of translation is call code generation. After code generation the actual program execution takes place.

A compiler can report only the syntax errors, semantics errors, and type errors. Run-time errors become know at program line by line and checking its output.
Types of  errors.

A part from the things mentioned in the chapter, C++ offers lot many new things, features and facilities that increase the usefulness of C++. We will learn about other features of C++ in the coming articles. Our very next variable etc. that form the basic part of a program.



Post a Comment

0 Comments