66fa371e68426CCWeek 01lecture01
66fa371e68426CCWeek 01lecture01
(CSC-320)
Lecture # 01
Course Instructor: M. Ramzan Shahid Khan
2
How are Languages Implemented?
• Two Major Strategies:
1. Interpreters
• LISP, bash, java, …
• Interpreters run programs “as is” (Little pre-processing)
• Carry-out the meaning of a program.
2. Compilers
• gcc, javac, …
• Compilers do extensive pre-processing
• Transform a program in a (higher-level) language into an efficient program in a
(lower-level) language, preserving the meaning.
3
Key Difference Between Compiler and
Interpreter
• Compiled code runs faster, while interpreted code runs slower.
• Compiler displays all errors after compilation, on the other hand, the
Interpreter displays errors of each line one by one.
5
What is Compiler?
• A Compiler is a computer program that transforms code written in a
high-level programming language into the machine code.
6
What is Compiler?
• A Compiler should comply with the syntax rule of that programming
language in which it is written.
• However, the compiler is only a program and can not fix errors found
in that program
• So, if you make a mistake, you need to make changes in the syntax of
your program,
• This includes
• Source code,
• Pre-compiled code, and
• Scripts
8
Compiler Vs Interpreter
• Both Compiler and Interpreters do the same job which is converting
higher level programming language to machine code.
• However, a compiler will convert the code into the machine code
(create an exe) before program run.
9
Compiler vs. Interpreter
Compiler Interpreter
•
• Take single instruction as input
Takes Entire program as input
• It is Faster • It is Slower
• intermediate object code is generated. • No intermediate code is generated
• Required more memory Due to • Required less memory As no intermediate
intermediate object code code is generated
• Program not need compile every time • Every time higher level program is converted
• Errors are displayed after entire program into lower level program.
is checked. • Errors are displayed for every instruction
• Debugging is comparatively hard. interpreted.
• Ex: C, C++. • Debugging is easy.
• Ex: python, Ruby, basic.
10
Difference between Compiler and Interpreter
Basis of difference Compiler Interpreter
Programming Steps • Create the program. • Create the Program
• Compile will parse or analyses • No linking of files or machine
all of the language statements code generation
for its correctness. If incorrect, • Source statements executed line
throws an error by line DURING Execution
• If no error, the compiler will
convert source code to machine
code.
• It links different code files into a
runnable program(know as exe)
• Run the Program
Advantage The program code is already Interpreters are easier to use,
translated into machine code. especially for beginners.
Thus, code execution time is less.
11
Difference between Compiler and Interpreter
Basis of difference Compiler Interpreter
Disadvantage You can’t change the program Interpreted programs can run on
without going back to the source computers that have the
code. corresponding interpreter.
Machine Code Store machine language as Not saving machine code at all
machine code on the disk
Running Time Compiled code run faster Interpreted code run slower
Model It is based on language translation It is based on Interpretation
lining-loading model Method
Program Generation Generates output program (in the Do not generate output program.
form of exe) which can be run So they evaluate the source
independently from the original program at every time during
program execution
12
Difference between Compiler and Interpreter
Basis of difference Compiler Interpreter
Execution Program execution is separate from Program Execution is a part of
the compilation. It performed only Interpretation Process, so it is
after the entire output program is performed line by line
compiled
Memory Requirement Target program execute The interpreter exists in the
independently and do not required memory during interpretation
the compiler in the memory
Best Suited For Bounded to the specific target For web environments, where load
machine and cannot be ported. times are important. Due to all the
C and C++ are a most popular exhaustive analysis is done,
programming language which uses compiler take relatively larger time
compilation model to compile even small code that
may not be run multiple times. In
such cases, interpreters are better.
13
Difference between Compiler and Interpreter
Basis of difference Compiler Interpreter
Code Optimization The compiler sees the entire code Interpreter see code line by line,
upfront. Hence, they perform lots and thus optimizations are not as
of optimizations that make code robust as compilers
run faster
Dynamic Typing Difficult to implement as compilers Interpreted languages support
cannot predict what happens at Dynamic Typing
run time
Usage It is best suited for the Production It is best suited for the program
Environment and development environment
Error Execution Compiler displays all errors and The interpreter reads a single
warning at the compilation time. statement and shows the error if
Therefore, you can’t run the any. You must correct the error to
program without fixing errors interpret next line
14
Difference between Compiler and Interpreter
Basis of difference Compiler Interpreter
Input It takes an entire program It takes a single line of code
Output Compiler generates intermediate Interpreter never generate any
machine code intermediate machine code
Errors Display all errors after, compilation, Display all errors of each line one
all at the same time by one
Pertaining Programming Languages C, C++, C#, Scala, Java all use PHP, Perl, Ruby uses an interpreter
Compiler
15
Role of Compiler
• Compiler reads the source code, outputs executable code.
• It converts the text that a programmer writes into a format the CPU
can understand.
16
Role of Compiler
• The process of compilation is relatively complicated.
17
Role of Interpreter
• The interpreter converts the source code line-by-line during Run time.
• Relatively less time spend for analysing and processing the program
18
High-level Languages
• High-level languages, like C, C++, JAVA etc., are very near to English.
20
Source Code
• Source code refers to high-level code or assembly code that is generated by
humans/programmers.
21
Object Code
• On Compilation of Source Code, the machine code generated for
different processors like
• Intel,
• AMD, and
• ARM
• is different.
22
Object Code
• Object code refers to low-level code that is understandable by machine.
• Object code is generated from source code after going through a compiler
or other translator.
• It contains the instructions for the machine to perform. These instructions
are in the form of binary digits.
• It is an intermediary code (similar to machine code) that processor will
understand.
• At run time, the object code is converted to the machine code of the
underlying platform.
23
Hybrid Compiler
• Compilation and interpretation may be combined to implement a
programming language:
• Ex: java
24
Hybrid Compiler
Source Program
Translator
(Compiler)
Intermediate Program
Virtual machine Output
(Interpreter)
Input
25
Java is both Compiled and Interpreted
• To exploit relative advantages of compilers and interpreters, some
programming language like JAVA are Both Compiled and Interpreted.
• At run time, the JVM (Java Virtual Machine) interprets the Object
Code into Machine Code of the target computer.
26
Programmer’s View of a Computer System
A Concept by Tanenbaum
Virtual Machine Concept
Operating System
Level 3
Instruction Set
Architecture Level 2
Microarchitecture Level 1
Each level
Digital Logic hides the
Level 0 details of the
level below it 27