Chapter 1
Chapter 1
Chapter 1
(CS 1703)
CHAPTER 1
COMPILER FUNDAMENTALS , COMPILER STRUCTURE
Objectives:
2
Why Should We Study Compiler Design?
• Correctness
• Speed (runtime and compile time)
Degrees of optimization
Multiple passes
4
Learning outcomes
5
Learning outcomes
8
1. Introduction
• In order to reduce the complexity of designing and building computers, nearly all of
these are made to execute relatively simple commands (but do so very quickly).
• Programming languages are notations for describing computations to people and to
machines.
• All the software running on all the computers was written in some programming
9
Introduction contd..
• Since this is a tedious and error prone process most programming is, instead, done
using a high-level programming language.
• This language can be very different from the machine language that the computer
can execute, so some means of bridging the gap is required. This is where the
compiler comes in.
11
SOURCE PROGRAM COMPILER TARGET PROGRAM
INPUT
OUTPUT
12
Fig1: COMPILER
Example: Compiler compiling a statement of a program
MOV id3, R2
MUL #10.0, R2
X= a+b*10 COMPILER
MOV id2, R1
13
Why Use a high-level programming
language?
• Compared to machine language, the notation used by programming languages is
closer to the way humans think about problems.
• The compiler can spot some obvious programming mistakes.
• Programs written in a high-level language tend to be shorter than equivalent
14
Notes:
• Programs that are written in a high-level language and automatically translated to
machine language may run somewhat slower than programs that are hand-coded in
machine language. Hence, some time-critical programs are still written partly in
machine language.
15
2. INTERPRETER
• An interpreter, like a compiler, translates high-level language into low-level machine
language.
• Instead of producing a target program as a translation, an interpreter appears to
directly execute the operations specified in the source program on inputs supplied
by the user.
16
SOURCE PROGRAM
INTERPRETER Output
Fig 2: INTERPRETER
17
3. COMPILER Vs INTERPRETER
COMPILER INTERPRETER
The machine-language target program An interpreter, give better error
produced by a compiler is usually diagnostics than a compiler, because it
much faster than an interpreter at executes the source program statement
mapping inputs to outputs by statement
20
SOURCE INTERPRETER
COMPILER OUTPUT
PROGRAM (Virtual Memory)
Bytecode Machine
Translate
21
Language-Processing System
• We have learnt that any computer system is made of hardware and software.
• The hardware understands a language, which humans cannot understand.
• So we write programs in high-level language, which is easier for us to understand and
remember.
• These programs are then fed into a series of tools and OS components to get the
22
Source program Pre-processor
Compiler
Relocatable object
Linker/Loader
/Library files
24
CONTD…
• Preprocessor
A preprocessor, generally considered as a part of compiler, is a tool that produces input for
compilers. It deals with macro-processing, augmentation, file inclusion, language
extension, etc.
• Assembler
25
• Linker
Linker is a computer program that links and merges various object files together in order to
make an executable file.
All these files might have been compiled by separate assemblers.
The major task of a linker is to search and locate referenced module/routines in a program
and to determine the memory location where these codes will be loaded, making the
program instruction to have absolute references.
26
QUESTIONS
1. What is the difference between a compiler and an interpreter?
2. What are the advantages of :
(a) a compiler over an interpreter
(b) an interpreter over a compiler?
27
How to convert a low level language into high
level language?
• A program that translates from a low level language to a higher level one is
a decompiler.
1. Analysis
2. Synthesis Intermediate Code
Compiler
29
• The analysis phase of the compiler reads the source program, divides it into core
parts and then checks for lexical, grammar and syntax errors.
• It generates an intermediate representation of the source program and symbol
table, which should be fed to the Synthesis phase as input.
• It generates the target program with the help of intermediate source code
representation and symbol table.
• A compiler can have many phases and passes.
• Pass : A pass refers to the traversal of a compiler through the
32
Lexical
Analysis/Scanner
• The first phase of scanner works as a text scanner.
• This phase scans the source code as a stream of characters
and converts it into meaningful lexemes .
<token_name, attribute_value>
33
Lexical Analysis/Scanner
• Lexical analysis is the first phase of a compiler. It takes the modified source
code from language preprocessors that are written in the form of sentences.
The lexical analyzer breaks these syntaxes into a series of tokens, by
removing any whitespace or comments in the source code.
• If the lexical analyzer finds a token invalid, it generates an error. The lexical
34
Toke
n
• Lexemes are said to be a sequence of characters
(alphanumeric) in a token.
• There are some predefined rules for every lexeme to be
identified as a valid token.
36
Syntax Analysis/ Parsing
37
prepared by Chitrapriya, CSE Dept
38
Semantic Analysis/Type Checking
machine code.
Code Optimization
41
prepared by Chitrapriya, CSE Dept
Error handler
44
Symbol Table
45
6. Compiler-Construction Tools
• The compiler writer, like any programmer, can profitably use
tools such as
• Debuggers, language editor
• Version managers,
46
Contd…
• These tools use specialized languages for specifying and implementing the
component, and many use algorithms that are quite sophisticated.
• The most successful tools are those that hide the details of the generation
algorithm and produce components that can be easily integrated into the
47
• The following is a list of some useful compiler-construction tools:
• Parser generators
• Scanner generators
• Syntax directed translation engines
• Automatic code generators
48
Compiler-Construction Tools:
• Parser generators
automatically produce syntax analyzers from a grammatical description
of a programming language.(CFG)
• Scanner generators
41
Compiler-Construction Tools: Contd…
• Automatic code generators:
produce a code generator from a collection of rules for translating each
operation of the intermediate language into the machine language for a
target machine
42
THANK YOU