Unit 2 Cs 150
Unit 2 Cs 150
A programming language has a vocabulary and set of grammatical rules for instructing a
computer to perform specific tasks. The term programming language usually refers to high-level
languages, such as BASIC, C, C++, COBOL, FORTRAN, Ada, and Pascal. These are quite
advanced languages, at least above machine code, because they are able to use languages which
can readily be perceived by humans. Just as people speak many languages, you should
understand there are different kinds of programming languages.
1. A place for storing data. Arrays are advanced storing data facility. Also known as data
structures.
2. Rules for writing programs in that programming language
3. Control statements – which are building blocks for logic implementation.
4. Most programming languages of today support Object Oriented Programming (OOP). So,
constructs to implement like features Class declaration, objects, inheritance,
polymorphism and constructors are included.
5. Every language has operators. Operators are used execute mathematical operations.
6. All languages include facility to write programs, functions and procedures. Incidentally,
this is the place where you write your programs. Functions return values after execution,
whereas procedures simply execute programs.
7. All programs include facility to write libraries. Libraries are themselves programs, which
can be used in other programs.
8. All languages support exception handling. This feature is helpful to identify errors and
generate appropriate messages.
9. All languages include built in functionalities, provided as classes and functions. These
classes help to write better programs.
10. All languages include a compiler and memory handling features. These are implemented
in different ways by the person(s) who developed the language.
A machine language consists of the numeric codes for the operations that a particular computer
can execute directly. The codes are strings of 0s and 1s, or binary digits (“bits”), which are
frequently converted both from and to hexadecimal (base 16) for human viewing and
modification. Machine language instructions typically use some bits to represent operations, such
as addition, and some to represent operands, or perhaps the location of the next instruction.
Machine language is difficult to read and write, since it does not resemble conventional
mathematical notation or human language, and its codes vary from computer to computer.
Assembly language is one level above machine language. It uses short mnemonic codes for
instructions and allows the programmer to introduce names for blocks of memory that hold data.
One might thus write “add pay, total” instead of “0110101100101000” for an instruction that
adds two numbers. (Hemmendinger, 2015). Machine code programs are very efficient, but
obviously difficult to write.
Assembly languages use abbreviation or mnemonics such as ADD that are automatically
converted to the appropriate sequence of 1s and 0s
Assembly languages are much easier to use than machine language, but still more
difficult to use than higher level languages
These tend to be hardware dependent, but very efficient
This is a second level programming language. It is a variant of machine language in which
names and symbols take the place of the actual codes for machine operations , values and storage
locations , making individual instructions more readable
Query Languages
Query languages enable non-programmers to use certain easily understood
commands to search and generate reports from a database
Structured Query Language (SQL) is one of the most widely used query languages
Application Generators
An application generator (aka program coder) is a program that provides modules
of prewritten code.
Programmers can quickly create a program by referencing the appropriate modules
MS Access has a report generation application and a Report Wizard for quickly
creating reports
Because they are easier to use compared to 3rd generation languages, 4th generation languages are
called Very High Level Languages.
The following diagram can help you to have an overview of the programming languages
Interpreters
Interpreters, proceed through a program by translating and then executing single instructions or
small group of instructions. An Interpreter takes a program and its input at the same time. It
translates the program, implementing operations as it encounters them, and doing input/output as
necessary. One main advantage of an interpreter is that execution as well as syntax errors are
detected as each statement is encountered, thus debugging is easier in interpreted languages.
With an interpreter, the language comes as an environment, where you type in commands at a
prompt and the environment executes them for you. For more complicated programs, you can
type the commands into a file and get the interpreter to load the file and execute the commands
in it. If anything goes wrong, many interpreters will drop you into a debugger to help you track
down the problem.
The advantage of this is that you can see the results of your commands immediately, and
mistakes can be corrected readily. The biggest disadvantage comes when you want to share your
programs with someone. They must have the same interpreter, or you must have some way of
giving it to them, and they need to understand how to use it. Also users may not appreciate being
thrown into a debugger if they press the wrong key! From a performance point of view,
interpreters can use up a lot of memory, and generally do not generate code as efficiently as
compilers.
It can however be said that, interpreted languages are the best way to start if you have not done
any programming before. This kind of environment is typically found with languages like Lisp,
Smalltalk, Perl and Basic.
Compilers
Compilers translate the entire program into machine language before executing any of the
instructions. Compilers translate source code into machine oriented target code called object
code. After source code is compiled into object code , no further references is made to the source
language.
First of all, you write your code in a file (or files) using an editor. You then run the compiler and
see if it accepts your program. If it did not compile, grit your teeth and go back to the editor; if it
did compile and gave you a program, you can run it either at a shell command prompt or in a
debugger to see if it works properly.
An IR is any data structure that can represent the program without loss of information so that its
execution can be conducted accurately. It serves as the common interface among the compiler
components. Since its use is internal to a compiler, each compiler is free to define the form and
details of its IR, and its specification needs to be known only to the compiler writers. Its
existence can be transient during the compilation process, or it can be output and handled as text
or binary files.
Using an IR enables a compiler to support multiple front ends that translate from different
programming languages and multiple back ends to generate code for different processor targets
(figure below). The execution platform can also be interpretive in the sense that its execution is
conducted by a software program or virtual machine. In such cases, the medium of execution can
be at a level higher than assembly code, while being lower or at the same level as the IR.
Key
A compiler is a program that changes source code to object code
An interpreter translates source code one line at a time and executes the instruction
Executable File
It is a file in a format that the computer can directly execute. Unlike source file, executable files
cannot be read by humans. To transform a source file into an executable file you need to pass it
through a compiler or assembler Example of Compiled Languages are C, C++. The translation of
a source program into object code is said to occur at translation time. Once translation is
complete the object code is run at a later time called run time. The object code created by the
compiler occupies more space than machine code .
Figure 2.5: Linking Libraries into an executable file
A bytecode program may be executed by parsing and directly executing the instructions, one at a
time. This kind of bytecode interpreter is very portable. Some systems, called dynamic
translators, or "just-in-time" (JIT) compilers, translate bytecode into machine language as
necessary at runtime: this makes the virtual machine hardware-specific, but doesn't lose the
portability of the bytecode itself. For example, Java and Smalltalk code is typically stored in
bytecoded format, which is typically then JIT compiled to translate the bytecode to machine code
before execution. This introduces a delay before a program is run, when bytecode is compiled to
native machine code, but improves execution speed considerably compared to direct
interpretation of the source code, normally by several magnitudes
Because of its performance advantage, today many language implementations execute a program
in two phases, first compiling the source code into bytecode, and then passing the bytecode to the
virtual machine. There are bytecode based virtual machines of this sort
for Java, Python, PHP, Tcl, and Forth (however, Forth is not ordinarily compiled via bytecodes
in this way, and its virtual machine is more generic instead). The implementation
of Perl and Ruby 1.8 instead work by walking an abstract syntax tree representation derived from
the source code.
A JVM language is any language with functionality that can be expressed in terms of a valid
class file which can be hosted by the Java Virtual Machine. A class file contains Java Virtual
Machine instructions (or bytecode) and a symbol table, as well as other support information.
EXERCISE