You are on page 1of 22

Compiler Construction

Instructor: Aunsia Khan


aunsia.khan@numl.edu.pk

Lecture# 01: Introduction

Week 1

1
Learning Outcomes
 Understand the basic techniques used in
compiler construction such as
 Lexical analysis,
 Top-down parsing,
 Bottom-up parsing,
 Context-sensitive analysis,
 Intermediate code generation.

2
Learning Outcomes
 Understand the basic data structures used in
compiler construction such as
 Abstract Syntax Trees,
 Symbol Tables,
 Three-address code,
 Stack machines.

3
Marks Distribution

Marks Distribution

Total Total Marks


Marks Head Marks /Frequency
Frequency /Head

Quiz & Assignment


3 vary 20

Mid Term Paper 1 30 30

Final Paper & Viva 1 50 50

  Total Marks 100


Books
 Textbook:
 Compilers: Principles, Techniques, and Tools, , A. V. Aho, R.
Sethi and J. D. Ullman, Addison-Wesley.
 Reference Books:
 Modern Compiler Design, , D. Grune, H. E. Bal, C. J. H.
Jacobs, K. G. Langendoen, Jo, 1st 
 Writing Compilers and Interpreters: A Software Engineering
Approach, R. Mak.
 Lex & Yacc, J. R. Levine, T. Mason, D. Brown, O’Reilly.
 A Compact Guide to Lex & Yacc, T. Niemann.

5
Material & Submissions
 Lecture Notes: LMS
 Helping Videos: LMS

 Google Classroom:
Class code for 5-B: 42djau3

6
Week 1
 Introduction to programming process
and the tools used for this task like
Interpreters, Compilers, Assemblers,
 Comparison of interpreters and
compilers

7
Compilers and Interpreters
 Compilation
 Translation of a program written in a source
language into a semantically equivalent program
written in a target language.
Source Target
Compiler Program
Program

Error messages

 As an important part of this translation process, the


compiler reports to its user the presence of errors in
the source program. 8
Compilers and Interpreters
 There are thousands of source languages,
ranging from traditional programming
languages such as FORTRAN and Pascal to
specialized languages.
 Target languages are equally as varied;
 A target language may be another programming
language, or the machine language of any
computer.

9
Compilers and Interpreters
 Running the Target Program
 If the target program is an executable machine-
language program, it can then be called by the
user to process inputs and produce outputs.

Input Target Program Output

10
Compilers and Interpreters
 An interpreter is another common kind of language
processor. 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,
Source
Program Interpreter Output
Input
Error messages
 Interpretation
 Performing the operations implied by the source program.

11
Compilers and Interpreters
 The machine-language target program
produced by a compiler is usually much faster
than an interpreter at mapping inputs to
outputs .
 An interpreter, however, can usually give
better error diagnostics than a compiler,
because it executes the source program
statement by statement.

12
Example
 Java language processors combine compilation and
interpretation. A Java source program may first be
compiled into an intermediate form called bytecodes.
The bytecodes are then interpreted by a virtual
machine.
 A benefit of this arrangement is that bytecodes
compiled on one machine can be interpreted on
another machine, perhaps across a network.
 In order to achieve faster processing of inputs to outputs,
some Java compilers, called just-in-time compilers, translate
the bytecodes into machine language immediately before
they run the intermediate program to process the input.
13
Compilers and Interpreters
 Hybrid Compiler
 Performing the operations implied by the source
program.

Source
Translator
Program

Intermediate Program Virtual


Input Machine Output

14
Language Processing System
Source Program

Preprocessor
Modified Source Program
Compiler Libraries
Target Assembly Program and
Assembler Relocatable Object Files
Relocatable Object Code
Linker/Loader

Machine Code 15
Language Processing System
 Preprocessor can
 Delete comments
 Include other files, e.g.
#include <stdio.h>
 Perform macro substitutions, e.g.
#define SUM(x, y) (x + y)
 Assembler
 Translator for Assembly Language

16
Language Processing System
 Usually, a program called a loader performs the two
functions of loading and link-editing.
 Linker
 Collects code separately compiled or assembled in different
object files.
 Connects object program(s) to the code for standard library
functions.
 The process of loading consists of taking relocatable
machine code, altering the relocatable addresses, and
placing the altered instructions and data in memory at the
proper location.

17
Language Processing System
mouse.c keyboard.c

mouse.c editor.c keyboard.c


Compile
mouse.obj editor.obj keyboard.obj

Linker Libraries

Executable Files
18
Relocation
The modified source program is then fed to a compiler. The compiler
may produce an assembly-language program as its output, because
assembly language is easier to produce as output and is easier to
debug.
Sample Program After Compilation
   
a db 3 0000 a db 3
b db 5 0001 b db 5
c db 0 0002 c db 0
mov AX, a 0003 mov AX, [0000]
mov BX, b 0005 mov BX, [0001]
add AX, BX 0007 add AX, BX
mov c, AX 0009 mov [0002], AX
 
- Assuming each instruction is of 2B
- Addresses are relocatable

19
Relocation
The assembly language is then processed by a program
called an assembler that produces relocatable machine code
as its output.
After Compilation Loaded at 5000
   
0000 a db 3   
0001 b db 5 5000 a db 3
0002 c db 0 5001 b db 5
0003 mov AX, [0000] 5002 c db 0
0005 mov BX, [0001] 5003 mov AX, [5000]
0007 add AX, BX 5005 mov BX, [5001]
0009 mov [0002], AX 5007 add AX, BX
  5009 mov [5002], AX
- Assuming each instruction is of 2B  
- Addresses are relocatable - Addresses are physical

20
Use of Compilation Techniques

21
 Thank you

22

You might also like