Programming Language and
Compiler Design
L.Ramkumar
What Is A Programming Language?
A tool for instructing machines
A means of communicating between
programmers
A vehicle for expressing high-level designs
A notation for algorithms
A way of expressing relationships between
concepts
A tool for experimentation
A means for controlling computerized
devices
Levels
Gross distinction between
programming language
Based on readability
Based on independence
Based on purpose (specific … general)
Levels
Machine level language
Assembly level language
High-level language (3GL)
Sometimes 4GL - fourth generation
language
Machine Level
00000010101111001010
00000010101111001000
00000011001110101000
Programs in machine language are
usually unintelligible at the lowest level,
the most detailed level, since they
consists only of 0’s and 1’s
Assembly Language
Assembly language 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.
Individual instructions in assembly
language are readable, but limitations
of the underlying machine can lead to
convoluted programs.
Basic Concepts of a RAM machine
Memory: addresses, contents
Program: instructions
Input/output: (files)
Program –
A random access machine
Input
1. M[0] :=0
2. Read (M[1])
3. If M[1] ≥ 0 then goto 5
4. Goto 7
5. M[3] := M[0] – M[1]
Control 6. If M[3] ≥ 0 then goto 16
7. Writeln (M[1])
8. Read (M[2])
9. M[3] := M[2] – M[1]
10. If M[3] ≥ 0 then goto 12
11. Goto 14
12. M[3] := M[1] – M[2]
13. If M[3] ≥ 0 then goto 8
Output
14. M[1] := M[2] – M[0]
15. Goto 3
16. halt
0 1
Memory
High Level
Readable familiar notations
Machine independence
Availability of program libraries
Consistency check (check data types
Example : Originally written in assembly
language, the unix operating system kernel
was rewritten in the programming language C
in 1973. Ritche [1978] recounts the resulting
benefits
New users and Programs
Readability
Portability
Problems of Scale
Changes are easy to make
Isolated program fragments can be
understood
BUT… one small bug can lead to disaster
Read the NOT story about Mariner rockets
(Refer http://en.wikipedia.org/wiki/Mariner_1)
Notice how the chairman does not
understand that a “small” problem can lead
to devastating result and why it was not
caught
Bugs
Programming testing can be used to
show the presence of bugs, but never
their absence! - Dijkstra
Programming Languages can help
Readable and understandable
Organize such that parts can be
understood.
Role of Programming Languages
Art (science) of programming is
organizing complexity
Must organize in such a way that our
limited powers are sufficient to
guarantee that the computation will
establish the desired effect
(Dijkstra - structured programming,
sometimes referred to as goto-less
programming)
Programming Paradigms
Imperative Programming
Functional Programming
Object Oriented Programming
Logic Programming
Imperative Programming
Imperative Programming are action
oriented; that is, a computation is
viewed as sequence of action.
Fortran, Pascal and C are general
purpose imperative language.
Algol 60’s dominated the
programming language scene in
1960’s
Pascal was designed as a teaching
language.
C was created in 1972 by Dennis
Ritche as an implementation
language associated with UNIX
operating System
In 1973 UNIX Operating System was
rewritten in C.
Functional Programming
The basic concepts of functional languages
originated with Lisp, a language designed in
1958 for applications in artificial
intelligence.
Lisp is designed primarily for symbolic data
processing.
Used for symbolic calculation in differential
and integral calculus, electrical circuit
theory, mathematical logic, game playing
and other field of artificial intelligence.
Object Oriented Programming
Object Oriented Programming owes
much to Simula’s origin in simulation.
The key concept from Simula is that
of a class of objects.
C++ and Smalltalk are popular
languages for Object Oriented
Programming.
C++ was designed to bring the
benefits of objects to imperative
programming in C.
Smalltalk was designed as part of a
personal computing environment.
It is an interactive system with a
graphical user interface.
Logic Programming
Prolog was developed in 1972.
Prolog uses a specialized form of
logical reasoning to answer queries.
%% Demo coming from http://clwww.essex.ac.uk/course/LG519/2-facts/index_18.html
%%
%% Please load this file into SWI-Prolog
%%
%% Sam's likes and dislikes in food
%%
%% Considering the following will give some practice
%% in thinking about backtracking.
%% ?- likes(sam,dahl).
%% ?- likes(sam,chop_suey).
%% ?- likes(sam,pizza).
%% ?- likes(sam,chips).
%% ?- likes(sam,curry).
likes(sam,Food) :-
indian(Food),
mild(Food).
likes(sam,Food) :-
chinese(Food).
likes(sam,Food) :-
italian(Food).
likes(sam,chips).
indian(curry).
indian(dahl).
indian(tandoori).
indian(kurma).
mild(dahl).
mild(tandoori).
mild(kurma).
chinese(chow_mein).
chinese(chop_suey).
chinese(sweet_and_sour).
italian(pizza).
italian(spaghetti).
Choice of Language
The choice of programming language
depends in part on the programming
to be done, and in part on external
factors, such as availability, support
and training
Language Implementation
Compiler - source code it translated
into machine code (all at once)
Source code is portable – compiled code
is NOT portable
Interpreter - machine is brought up
to the language (one statement at a
time
Source code is portable; executable on
any computer (that has an interpreter
for that language)
Mixed mode:
Compiled to an intermediate code (p-
code)
P-code is then interpreted
P-code is portable to any computer that has right interpreter
Compiled C
Source Pre- Pre Linker
.o
code procces processed compiler or
files
in C sor code assembler
Machine Machine
Loader codes
code (exe)
Source code language is brought down
to the machine language of that
specific computer
Interpreted Code
Each instruction is interpreted by
machine interpreter
Does not produce object code
Machine is brought up to a “virtual machine” that executes the
source code
Comparisons
Compilation more efficient
Interpreted more flexible
Mixed-mode good for distributed
environments (e.g. Web
client/server)
Major Parts of Compilers
There are two major parts of a compiler: Analysis
and Synthesis
In analysis phase, an intermediate representation is
created from the given source program.
Lexical Analyzer, Syntax Analyzer and Semantic
Analyzer are the parts of this phase.
In synthesis phase, the equivalent target program is
created from this intermediate representation.
Intermediate Code Generator, Code Generator, and
Code Optimizer are the parts of this phase.
Other Applications
In addition to the development of a compiler, the
techniques used in compiler design can be applicable
to many problems in computer science.
Techniques used in a lexical analyzer can be used in
text editors, information retrieval system, and
pattern recognition programs.
Techniques used in a parser can be used in a query
processing system such as SQL.
Many software having a complex front-end may need
techniques used in compiler design.
A symbolic equation solver which takes an equation as
input. That program should parse the given input
equation.
Most of the techniques used in compiler design can
be used in Natural Language Processing (NLP)
systems.
Phases of A Compiler
Source Lexical Syntax Semantic Intermediate Code Code Target
Program Analyzer Analyzer Analyzer Code Generator Optimizer Generator Program
Each phase transforms the source
program from one representationinto
another representation.
They communicate with error
handlers.
They communicate with the symbol
table.
Lex and Yacc
Lex and yacc are tools used to
generate lexical analyzers and
parsers.