Compiler Construction Lecture 1 - 2
Compiler Construction Lecture 1 - 2
______________________________________________________
GIMS- PMAS Arid Agriculture University, Gujrat Campus
1
Agenda
Course outline
Marks distribution
Sessional marks elaboration
Assignment/Project sending Submission
Procedure
Overview of the course
Why need to study this course?
2
Distribution
Sessional
Assignments = 3
Quizzes = 2 (unannounced + announced)
Project = 3
Exams
Mid term = 12
Final = 20
Practical = 20
3
Project
Implementation
Flex Script for analyzer module
Any language for other modules
4
Why Take this Course
Reason #1: understand compilers and
languages
understand the code structure
understand language semantics
understand relation between
source code and generated
machine code
become a better programmer
5
Why Take this Course
Practice
Apply theoretical notions to build a real compiler
6
Why Take this Course
7
Examples
Typical Compilers:
VC, VC++, GCC, JavaC
FORTRAN, Pascal, VB(?)
Translators
Word to PDF
PDF to Postscript
8
In This Course
9
Typical Compilation
Compiler
11
Source Code
12
Assembly Code
.globl _expr
_expr:
imull %eax,%edx
pushl %ebp
movl 8(%ebp),%eax
movl %esp,%ebp
incl %eax
subl $24,%esp
imull %eax,%edx
movl 8(%ebp),%eax
movl %edx,-4(%ebp)
movl %eax,%edx
movl -4(%ebp),%edx
leal 0(,%edx,4),%eax
movl %edx,%eax
movl %eax,%edx
jmp L2
imull 8(%ebp),%edx
.align 4
movl 8(%ebp),%eax
L2:
incl %eax
leave
ret
13
Assembly Code
14
How to Translate
Correctness:
the generated machine code must execute
precisely the same computation as the source
code
15
How to Translate
16
How to Translate
17
Before we start, Let’s Judge:
What is the output of the following snippets of
code?
1=
2=
18
What is a Compiler?
A compiler is a computer
program that translates a
program in a source language
into an equivalent program in a Source Target
target language. program compiler program
A source program/code is a
program/code written in the
source language, which is
usually a high-level language. Error
message
A target program/code is a
program/code written in the
target language, which often is
a machine language or an
intermediate code.
19
Process of Compiling Stream of characters
scanner
Stream of tokens
parser
Parse/syntax tree
Semantic analyzer
Annotated tree
Intermediate code generator
Intermediate code
Code optimization
Intermediate code
Code generator
Target code
Code optimization
Target code
20
Some Data Structures
Symbol table
Literal table
Parse tree
21
Symbol Table
22
Symbol Table (cont’d)
23
Literal table
24
Parse tree
25
Scanning
26
Parsing
27
Semantic analysis
29
Intermediate code generation
(cont’d)
31
Code generation
A code generator
takes either an intermediate code or a parse tree
produces a target program.
32
Error Handling
Linkers
Loaders
Interpreters
Assemblers
35