Post on 30-May-2018
8/14/2019 Programming Language and Compiler Design Session
1/33
Programming Language andCompiler Design
L.Ramkumar
8/14/2019 Programming Language and Compiler Design Session
2/33
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
8/14/2019 Programming Language and Compiler Design Session
3/33
Levels
Gross distinction betweenprogramming language
Based on readability Based on independence
Based on purpose (specific general)
8/14/2019 Programming Language and Compiler Design Session
4/33
Levels
Machine levellanguage
Assembly levellanguage
High-level language (3GL) Sometimes 4GL - fourth generation
language
8/14/2019 Programming Language and Compiler Design Session
5/33
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 0s and 1s
8/14/2019 Programming Language and Compiler Design Session
6/33
Assembly Language
Assembly language is a variant ofmachine language in which namesand symbols take the place of theactual codes for machine operations,values, and storage locations, makingindividual instructions more readable.
8/14/2019 Programming Language and Compiler Design Session
7/33
Individual instructions in assemblylanguage are readable, but limitationsof the underlying machine can lead toconvoluted programs.
8/14/2019 Programming Language and Compiler Design Session
8/33
Basic Concepts of a RAM machine
Memory: addresses, contents
Program: instructions
Input/output: (files)
8/14/2019 Programming Language and Compiler Design Session
9/33
Program A random access machine
1. M[0] :=02. Read (M[1])3. If M[1] 0 then goto 54. Goto 75. M[3] := M[0] M[1]6. If M[3] 0 then goto 167. Writeln (M[1])8. Read (M[2])9. M[3] := M[2] M[1]10. If M[3] 0 then goto 1211. Goto 1412. M[3] := M[1] M[2]
13. If M[3] 0 then goto 814. M[1] := M[2] M[0]15. Goto 316. halt
Control
Input
0 1Memory
Output
8/14/2019 Programming Language and Compiler Design Session
10/33
High Level
Readable familiar notations
Machine independence
Availability of program libraries Consistency check (check data types
8/14/2019 Programming Language and Compiler Design Session
11/33
8/14/2019 Programming Language and Compiler Design Session
12/33
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 leadto devastating result and why it was notcaught
8/14/2019 Programming Language and Compiler Design Session
13/33
Bugs
Programming testing can be used toshow the presence of bugs, but nevertheir absence! - Dijkstra
Programming Languages can help
Readable and understandable
Organize such that parts can beunderstood.
8/14/2019 Programming Language and Compiler Design Session
14/33
Role of Programming Languages
Art (science) of programming isorganizing complexity
Must organize in such a way that our
limited powers are sufficient toguarantee that the computation willestablish the desired effect
(Dijkstra - structured programming,sometimes referred to as goto-lessprogramming)
8/14/2019 Programming Language and Compiler Design Session
15/33
Programming Paradigms
Imperative Programming
Functional Programming
Object Oriented Programming Logic Programming
8/14/2019 Programming Language and Compiler Design Session
16/33
8/14/2019 Programming Language and Compiler Design Session
17/33
Pascal was designed as a teachinglanguage.
C was created in 1972 by DennisRitche as an implementationlanguage associated with UNIXoperating System
In 1973 UNIX Operating System wasrewritten in C.
8/14/2019 Programming Language and Compiler Design Session
18/33
Functional Programming
The basic concepts of functional languagesoriginated with Lisp, a language designed in1958 for applications in artificial
intelligence. Lisp is designed primarily for symbolic data
processing.
Used for symbolic calculation in differential
and integral calculus, electrical circuittheory, mathematical logic, game playingand other field of artificial intelligence.
8/14/2019 Programming Language and Compiler Design Session
19/33
Object Oriented Programming
Object Oriented Programming owesmuch to Simulas origin in simulation.
The key concept from Simula is thatof a class of objects.
C++ and Smalltalk are popularlanguages for Object Oriented
Programming.
8/14/2019 Programming Language and Compiler Design Session
20/33
C++ was designed to bring thebenefits of objects to imperativeprogramming in C.
Smalltalk was designed as part of apersonal computing environment.
It is an interactive system with a
graphical user interface.
8/14/2019 Programming Language and Compiler Design Session
21/33
Logic Programming
Prolog was developed in 1972.
Prolog uses a specialized form of
logical reasoning to answer queries.
8/14/2019 Programming Language and Compiler Design Session
22/33
%% 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).
8/14/2019 Programming Language and Compiler Design Session
23/33
Choice of Language
The choice of programming languagedepends in part on the programmingto be done, and in part on externalfactors, such as availability, supportand training
8/14/2019 Programming Language and Compiler Design Session
24/33
Language Implementation
Compiler - source code it translatedinto 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 atime Source code is portable; executable on
any computer (that has an interpreterfor that language)
8/14/2019 Programming Language and Compiler Design Session
25/33
8/14/2019 Programming Language and Compiler Design Session
26/33
Compiled C
Source
code
in C
Pre-
procces
sor compiler
Linker
or
assembler
Machine
code (exe)
LoaderMachine
codes
.o
files
Pre
processed
code
Source code language is brought downto the machine language of that
specific computer
8/14/2019 Programming Language and Compiler Design Session
27/33
Interpreted Code
Each instruction is interpreted bymachine interpreter
Does not produce object code
Machine is brought up to a virtual machine that executes the
source code
8/14/2019 Programming Language and Compiler Design Session
28/33
Comparisons
Compilation more efficient
Interpreted more flexible
Mixed-mode good for distributedenvironments (e.g. Webclient/server)
8/14/2019 Programming Language and Compiler Design Session
29/33
Major Parts of Compilers
There are two major parts of a compiler: Analysisand 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, andCode Optimizer are the parts of this phase.
8/14/2019 Programming Language and Compiler Design Session
30/33
Other Applications In addition to the development of a compiler, the
techniques used in compiler design can be applicableto 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 inputequation. Most of the techniques used in compiler design can
be used in Natural Language Processing (NLP)systems.
8/14/2019 Programming Language and Compiler Design Session
31/33
Phases of A Compiler
Each phase transforms the source
program from one representationintoanother representation.
They communicate with errorhandlers.
They communicate with the symboltable.
Lexical
Analyzer
Semantic
Analyzer
Syntax
Analyzer
Intermediate
Code Generator
Code
Optimizer
Code
Generator
Target
ProgramSource
Program
8/14/2019 Programming Language and Compiler Design Session
32/33
Lex and Yacc
Lex and yacc are tools used togenerate lexical analyzers andparsers.
8/14/2019 Programming Language and Compiler Design Session
33/33