If you like my posts please leave a comment.

Monday, July 23, 2012

Role of Compiler and A Quick Guide to Turboc C++ IDE

Turbo C++ 3.0 IDE
 

This is the most commonly used Turbo C++ IDE (Integrated Development Environment); runs on DOS platform. Let's have a quick look - How to use this IDE?
To create a new file: File -> new.
To open an existing source file: File -> open OR F3-> select your file -> Open
To save your source file: File -> save / SaveAs OR F2-> give an name to your source file with .CPP extension -> OK. **Note your primary file name should not be longer than 8 characters
To rename or change the location of save source file: File -> SaveAs->write new file location (if required) and new file name -> OK Example - "D:\SAMPLE.CPP"
To print the program source code: File -> Print
Undo the last operation: Edit -> Undo OR Alt + Bksp
Redo the last operation: Edit -> Redo OR  Shift + Alt + Bksp
Copy: Edit -> Copy OR Ctrl + Ins
Cut: Edit -> Cut OR Shift + Del
Paste: Edit -> Paste OR Shift + Ins
To find a specific word in your source (program): Search -> Find
To search for a word and to replace with some other: Search -> Replace
To Search again with previously mentioned word: Search -> Search again OR Ctrl + L
Go to specific line number: Search -> Go to line number
To run your program: Run -> Run OR Ctrl + F9
Menu options helpful for tracing the flow of program control:
Trace into: Run -> Trace into OR F7 
Trace over: Run -> Trace over OR F8
To compile your program: Compile -> Compile OR Alt + F9


Errors:

There are several types of errors that you may face while writing a program. Some errors may be very common like typing mistakes. Such errors can be discovers by compiling the program. Also it may be the case that you have typed the program correctly but even after this you are not getting your expected output. So, some errors may appear at compile time while some may appear at run time. 

Role of Compiler:
 
A part of the compiler's job is to analyze the program code to check its correctness.
A compiler reports an error by flashing an error message, with corresponding line numbers and the brief description of the error. Remember, existence of a single error in the program or correction of the same may lead to generation of more errors. We need to go on correcting the source code by following the rules of the particular programming language (in our case it is C++) unless it is completely error free.
For example:
main(); //On compiling the program we will receive error message for this line. On correcting this and after compiling again
{
int a, b,c: //we will get error for this line as - Declaration syntax error
cin>>a>>b;
a * b = c; // and for this as Lvalue required.
return 0;
}
 Compiler translates the corrected program’s source code (program text) into object or assembly instruction text; understood by the computer. This process is known as code generation.  After code generation execution of the program takes place.
Compiler can report only Syntax errors, semantics errors and type errors. Where as Run-time errors becomes apparent at run-time and logical errors can be detected by tracing the program line by line and verifying with the related output(s).  
So, on the basis of above discussion now we can categorize the common forms of program errors as: Compile – time Errors, Run – time Errors and Logical Errors:

Compile – time Errors:

The errors that can be traced during compilation of the source code by the compiler is called Compile – time Errors. Several compile – time errors are discussed below.

1. Syntax Errors:

Syntax errors occur when rules of a programming language is not followed while writing a program means when grammatical rule of a particular programming language is violated.
Example:
main();
{
int a, b:
cin>>a>>b;
cout<<a + b;
return 0;
}
Compiling this program you will get a syntax error as the statement main() should not be terminated by (;). Again you will receive a syntax error for the line int a,b: as statement is terminated with colon (:). It should be terminated by a semicolon (;)

2. Semantics Errors:

If you write a statement which is not meaningful or ambiguous, semantics error occurs. Where semantics means to the set of rules which gives the meaning to a statement. 
Example:
A * B = C;
The above example is semantically wrong as multiplication between A and B should be done first and answer should be assigned to C. So the statement should be C = A * B;

3. Type Errors:

If a function is given with a data with wrong type then type error is signaled by the compiler. For instance, if an integer value was expected but given with string data type, is a type error.

Run – time Errors:

The errors that occur during the execution of the program are called Run-time errors. It can be caused because of some illegal operation taking place, or unavailability of memory, or unavailability of some external file that your program needs to open, etc.

Logical Errors:

A program having logical error(s) produces wrong result. In this case while compiling compiler never produces any error message. For instance, in the following example suppose we want to print the name 5 times and if we write:
int ctr = 1;
while (int i > = 5)
{
cout<<”NAME”<<endl;
}

Here the loop will not execute as the given condition is not true. So, the correct version of the code should:
int ctr = 1;
while (int i < = 5)
{
cout<<”NAME”<<endl;
}