01 Introduction To Compiler Construction
01 Introduction To Compiler Construction
Faryal Shamsi
Lecturer Computer Science
• Experience
• 16 years, Alhamdulillah!
• Joined Sukkur IBA University 4 years ago
• Previously –
• Lecturer at Sistech Sukkur - February 2008 to January 2020
• Visiting Faculty Member Sukkur IBA University in STHP 2018
• Visiting Faculty Member DIHE in 2017
About Us
• Today is our day!
● Let’s share –
○ Our experience(s) in previous courses?
○ Any other Suggestions/Regrets?
○ Which is your favorite programing language? Why?
○ Have you developed some exciting projects?
○ What you already know about Compiler Construction?
○ What you expect to learn in this course?
For Online 24/7 Support
• Course Dashboard: https://elearning.iba-suk.edu.pk/course/view.php?id=360
• Self Enrollments: GROUP NAME ENROLLEMENT KEY
Section B CCFall2024-B
Section C CCFall2024-C
Section D CCFall2024-D
Section E CCFall2024-E
Section F CCFall2024-F
• Email: [email protected]
• Some online lectures created during COVID lockdown are still available on my
YouTube Channel: https://www.youtube.com/channel/UCQ1D9g88cCWJwEj8VGT6KoA
My Availability & Support
• 1 ¼ Hours Class Meeting:
• Twice a week (as per Time Table)
• One-to-One Counseling:
• COSULATION HOURS:
• Wednesdays: 9:00am to 1:00pm
• Consultation Hours fluctuate with Time Table
• Text Book
• Reference Book:
GA2 Problem Analysis: An ability to identify, formulate, research literature, and analyze
complex computing problems reaching substantiated conclusions using first principles of mathematics,
natural sciences and computing sciences.
Project Presentation/
7 (4 + 3) Points Submission Punctuality, Class Participation
Demonstration
Faryal Shamsi
Lecturer Computer Science
• Compiler
• Interpreter
• Assembler
Compiler vs Interpreter
Compiler Interpreter
Compiler vs Interpreter
Compiler Interpreter
• Single-pass compilers
• 2-pass compilers
• Multi-pass compilers
The Cross Compilers
• Compiled Simulation
• Instead of writing a simulator that interprets the design, it is faster to compile the
design to produce machine code that simulates that particular design natively.
• Hardware Synthesis:
• Hardware designs are typically described at the register transfer level (RTL) ,
where variables represent registers and expressions represent combinational logic.
• Hardware-synthesis tools translate RTL descriptions automatically into gates , which
are then mapped to transistors and eventually to a physical layout .
Programming Language Basics
Programming Language Basics
• Environment and states (Figure 1.8)
• Suppose also that parameters are passed by value, but that array
names are really references to the location where the array is stored,
as in C or similar languages.
• Now, x and y have become aliases of each other. The important point is
that if within q there is an assignment x [10] = 2, then the value of
y[10] also becomes 2.
Phases of Compilation
Lecture 1
Faryal Shamsi
Lecturer Computer Science
• Analysis / Front-end
• Synthesis / Back-end
• The analysis part also collects information about the source program
and stores it in a data structure called a symbol table which is passed
along with the intermediate representation to the synthesis part .
Backend – Synthesis
• The synthesis part constructs the desired target program from the
intermediate representation and the information in the symbol table.
• The symbol table, which stores information about the entire source
program, is used by all phases of the compiler.
Mid-end or Optimization
Symbol Table
• Modern compilers contain two (large)
parts, each of which is often subdivided.
• Position is a lexeme that would be mapped into a token <id , 1> where id is an abstract
symbol standing for identifier and 1 points to the symbol table entry for position.
• The assignment symbol = is a lexeme that is mapped into the token <=>. It does not
need attribute value to store name and type.
• Blanks separating the lexemes would be discarded by the lexical analyzer.
Lexical Analysis
• The symbol-table entry for an identifier holds information about the
identifier, such as its name and type.
1. Lexical Analysis/ Scanning
1. Lexical Analysis/ Scanning
• Reads the stream of characters making up
the source program and groups the characters
into meaningful sequences called lexemes.
• For each lexeme, the lexical analyzer produces as output a token of
the form
<token-name , attribute-value>
• token-name is an abstract symbol that is used during syntax
analysis,
• attribute-value points to an entry in the symbol table for this token.
2. Syntax Analysis or Parsing
• The syntax Analyzer (parser) uses the first components of the
tokens produced by the lexical analyzer to create a tree-like
intermediate representation(IR) that depicts the grammatical
structure of the token stream.
E E E E
+ *
E E E E id
id * +
id id id id
3. Semantic Analyzer
• An important part of semantic analysis is type checking, where the
compiler checks that each operator has matching operands.
• The compiler needs semantic information, e.g., the types (integer,
real, pointer to array of integers, etc.) of the objects involved.
• The language specification may permit some type conversions
called coercions.
• Suppose that position, initial, and rate have been declared to be
floating-point numbers, and that the lexeme 60 by itself forms an
integer. Here integer may be converted into floating-point.
3. Semantic Analysis
4. Intermediate Code Generation
• After syntax and semantic analysis of the source program, many
compilers generate an explicit low-level or machine-like
intermediate representation,
Faryal Shamsi
Lecturer Computer Science
• Single-pass compilers
• 2-pass compilers
• Multi-pass compilers
Single Pass Compiler
• If we combine or group all the phases of compiler design in
a single module known as single pass compiler.
Single Pass Compiler
• A one pass/single pass compiler is that type of compiler that passes
through the part of each compilation unit exactly once.
• Single pass compiler is faster and smaller than the multi pass compiler.
• As a disadvantage of single pass compiler is that it is less efficient in
comparison with multi-pass compiler.
• Single pass compiler almost never done, early Pascal compiler did this as
an introduction.
• We can not optimize very well due to the context of expressions are limited, as
we can’t backup and process, it again so grammar should be limited or
simplified.
2-pass or Multi-pass Compiler