Compiler and interpreter

 Compiler and Interpreter

A program written in a high level language. A high level language

is only understood by humans. It generally contains phrases From english language, but computer cannot understand high-level language.

Computer can only understand program written in 0‟s and 1‟s in

binary, called the machine code.

A program written in high-level language is called a source code.

Source code must be translated into machine language before it can be executed. 

So we need to call the source code into machine code and this is accomplished by compiler and interpreters.

Compiler or an interpreter is a program that converts programs written in high-level language into machine code understood bythe computer.

Compiler or interpreter is a computer program that accepts high-level program (e.g. C Program) as input data and generation program as output.

 The original high-level program is called the source program and resulting machine-language program is called the object program.

The difference between an interpreter and Compiler interpreter Compiler translate program one statement at a time.

Scan the entire program translate the whole program into machine code.

It takes less amount of time to analyze the source code but the overall execution time is slower.

It takes large amount of time to analyze the source code but the overall execution time is comparatively faster.

No intermediate code is generated, hence are memory efficient.

Generates intermediate object code which further required linking, hence requires more memory.

Continuous translating the program until the first error is It generates the error message only after scanning the whole met, in which case it stops.

Hence debugging is easy. program, Hence debugging is

comparatively hard.

Programming language like python, Ruby use interpreter.

Programming language like C, C++ use compiler.

Editor

An editor refers to any program capable of editing files. 

The term editor is commonly used to refer to a text editor, which is a software program that allows users to create or manipulate plain text computer files.

Error, Debugging and Testing

Errors:

There are 3 types of program errors:

 Syntax errors

 Run-time errors

 Logical error

Syntax Error:

A syntax error is a violation of the rules of the

programming language.

Example:

Printf(“ hi” )

Syntax errors are relatively easy to find and fix.

Run-time Error:

A run-time error is an error that results from using

invalid operand values for an operation.

The following statement generates a run-time error, if

count has the value zero. Since division by zero is not

mathematically defined.

Example:

int total=100;

int count=0;

Avg = total/count;

Logical Error:

Logical error in program is any code that causes

incorrect output/results even though the program runs to

completion.

A program with logical error may give the correct

answer sometimes and the wrong answer sometimes.

Logical error typically are the most difficult type of the

error to find and correct.

Example:

The following code contains a logic error

because the > and >=.

if( age > 18)

{

printf(“you are eligible for voting”);

}

Program testing:

 Testing is the process of finding errors in a program.

 Testing can find the presence of errors.

 Testing small program is much easier than testing large programs.

 You should test your program with expected and unexpected input.

o You should know how your program will perform with good

idea as well as bad data.

o Unexpected values may be negative, extremely large, or

extremely small values.

 You should test your program with boundary condition values, i.e

values at and around a value for which the behavior of a program should change.

o For example, good boundary condition test values for age in the code above would be 17, 18, 19.

 When testing loops, provide input that cause the loop to repeat zero, one, and many times.

Program debugging:

Debugging, in computer programming and engineering, is a multi step 

process that involves identifying a problem, isolating the source of the

problem, and then either correcting the problem or determining a way to work around it.

o Debugging is the process of locating and correcting errors in a program.

o Debugging id problem-solving and often can be very challenging.

o You can help minimize your time spent debugging by:

o Starting with a good program design

o Coding carefully

o Testing your program as you write.

Documentation section:

The documentation section consists of a set of comment lines giving the name of the program, the author and other details, which the programmer would like to use later. This information gives the program an identity and basic authority.

Comment can be

 Single line comment

The single line comment can be written by // (double slash)

 Multi line comment

The multiline comment can be written by

/*………………..

………………….*/

Link section:

The link section provides instruction to the compiler to link or include

the required in-built functions from the system library such as using

the #include directive. This is important, because if we need to use any in-built

system function we must first include the library in which the function has

been defined.

#include<file_name>

The source code of the file “file_name” is included in main() function C program.

The link section is also called include section, this section is prefixed by

# sign,

#include<stdio.h>

#include<conio.h>

#include<math.h>

Definition section:

The definition section defines all symbolic constants using the #define

directive. Having the constants being defined here, we can use them elsewhere

in code.

#define pi 3.14

# is preprocessor which gives direction to the compiler that define

pi as a constant value for a program.

Global declaration section:

There are some variables that are used in more than one function; these

variables are called global variables and are declared in this global declaration

section which is outside the definition of any function. This section also

declares all the user-defined functions. As this global scope, these functions

and variables can be used from definition of other functions.

main () function section:

A C program must have one main function section in its structure. This

section contains two parts; declaration part and executable part. However, the

content of these two parts can be mixed.

(a) Declaration part:

The declaration part declares all the variables used in the

executable part.

(b) Executable part:

There is at least one statement in the executable part.

These two parts must appear between the opening and closing braces of the main function.

 The program execution begins at the opening brace and ends at the closing brace. 

The closing brace of the main function is the logical end of the program. 

All statements in the declaration and executable part end with a semicolon.

Subprogram section:

If the program is a multi-function program then the subprogram section contains definition of all the user-defined functions which were declared earlier in the Definition Section. 

User-defined functions are generally placed immediately after the main () function, although they may appear in any order.

All section, except the main () function section may be absent when they are not required.

Comments

Popular posts from this blog

Computer programming

Functional units of computer