0% found this document useful (0 votes)
7 views53 pages

Compiler Construction Lec 1a

This document outlines the responsibilities and policies for a Compiler Construction course taught by Alisha Farman, including attendance, assignment submission, and plagiarism rules. It covers the basics of compilers and interpreters, their phases, types, and the importance of error detection and optimization. Additionally, it details the phases of compilation, including lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation.

Uploaded by

usamajaved425
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views53 pages

Compiler Construction Lec 1a

This document outlines the responsibilities and policies for a Compiler Construction course taught by Alisha Farman, including attendance, assignment submission, and plagiarism rules. It covers the basics of compilers and interpreters, their phases, types, and the importance of error detection and optimization. Additionally, it details the phases of compilation, including lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation.

Uploaded by

usamajaved425
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Compiler Construction

Lecture 1a
Instructor: Alisha Farman ([email protected])
Your Responsibility
• Attend classes regularly
• Ask question if you have any queries regarding
course material or anything.
• Submit assignment in time.
• Don’t miss quizzes, assignments and examinations-
Can get good result.
• No Plagiarism is allowed in any sort of a writer
material – Write in your own words.
Course Policy
Assignments:
• Assignments are due at the beginning of class.
• Late assignment will not be accepted.
• All works have to be done independently except in case of group
assignments.
• Students handing in similar assignments will receive a grade of 0
(Zero).

Attendance:
• Students are expected to attend all classes.
BOOKS:
1. Programming Compilers - Principles, Techniques, and Tools Alfred V. Aho, Ravi
Sethi and Jeffrey D. Ullman
2. Compiler Construction: Principles and Practice Kenneth C. Louden
MARKS DISTRIBUTION
4 Assignments 10 marks
4 Quizzes 10 marks
Project + Presentation + viva 15 marks
Midterm Exam 25 marks
Final Exam 40 marks
Compiler
A compiler is a program that translates code written in a high-level
programming language into machine code, bytecode, or another
intermediate code that a computer's processor can execute. The
process involves several stages, each designed to perform specific
tasks in the translation process.

● Compilers are used for


languages like C, C++, and
Java.

https://www.youtube.com/watch
?v=UllD_AY_U-8
All the software running on the computers was written
in some programming language.

Types of Languages:
High level languages
Low level languages
A program must be translated into a form in which it can be
executed by a computer.

The software systems that do this translation are called


COMPILERS
Interpreter
An interpreter directly executes the instructions written in a programming
language without converting them into machine code. It reads the program line by
line, converts each line into an intermediate form, and executes it immediately.

An interpreter translates code written in a high-level programming language into


machine code line-by-line as the code runs. Examples of interpreted languages
include Python, Ruby, and JavaScript.
Modern Compilers
Modern compilers are more sophisticated and provide additional features such
as:

● Error Detection and Reporting: Modern compilers offer detailed error


messages to help developers identify and fix issues in their code.
● Optimization: They perform various optimization techniques to improve
the performance of the generated code.
● Portability: They often generate intermediate code (e.g., bytecode) which
can be executed on multiple platforms with the help of a virtual machine.
Languages Preprocessors
Simply stated, a compiler is a program that can read a program in one language -
the source language - and translate it into an equivalent program in another
language - the target language;

Important Role
Report errors, if any, found in source program.
Languages Preprocessors
If the target program is an executable machine-language
program, it can then be called by the user to process inputs and
produce outputs;
Parts of
Compilation
Phases of Compiler
ANALYSIS PHASE
Known as the front-end of the compiler, the
analysis phase of the compiler reads the
source program, divides it into core parts and
then checks for lexical, grammar and syntax
errors. The analysis phase generates an
intermediate representation of the source
program and symbol table, which should be
fed to the Synthesis phase as input.
SYNTHESIS PHASE
Known as the back-end of the compiler, the
synthesis phase generates the target
program with the help of intermediate
source code representation and symbol
table.
CLASSIFICATION OF COMPILER
• Single pass compilers
• Two pass compilers
• Multipass compilers
Single-Pass Compiler
● Single Analysis: A single-pass compiler reads the source code one time and
performs all necessary analysis during that single pass. This means that it
processes each line of code sequentially.
● No Backtracking: Once a line or section of code has been analyzed, the
compiler does not go back to re-evaluate or re-analyze that code. If you
write new lines of code that depend on or reference earlier parts of the
code, the compiler cannot re-evaluate those earlier lines. It will only
compile based on the information it has gathered during the initial pass.
● Forward References: If your code includes forward references (e.g., calling
a function before itʼs defined), the single-pass compiler may struggle to
compile it correctly since it won't have seen the function definition yet.
This limitation can lead to errors if the code relies on such references.
Example of single pass compiler
Line 1: The compiler reads and processes this
int main() {
line.
printf("Hello, World!"); // Line 1 Line 2: The compiler encounters a call to
greet(); // Line 2 greet(). However, it hasn't seen the definition
} of greet() yet (which is below this line), so it
can't resolve what greet() is at this point.
void greet() { // Line 3 Line 3: The definition of greet() is processed,
but by this time, the compiler has already made
printf("Hi!"); // Line 4
decisions based on the previous lines.
} Because it can't go back, a single-pass compiler
may generate an error for greet() if it relies on
its definition that comes later in the code.
Advantages:
● Faster compilation times due to a single traversal of the source
code.
● Lower memory usage since it doesnʼt need to store intermediate
representations of multiple passes.
Disadvantages:
● Limited in its ability to perform complex optimizations or checks
that require more than one look at the code.
Two Pass or Multipass Compiler
• A multi-pass compiler is a type of compiler that
processes the source code of a program several times.
• Each pass takes the result of the previous pass as the
input, and creates an intermediate output.
• In this way, the (intermediate) code is improved pass
by pass, until the final pass emits the final code.
• Multi-pass compilers are sometimes called wide
compilers, referring to the greater scope of the
passes: they can "see" the entire program being
compiled, instead of just a small portion of it.
Example of Multi pass compiler
int main() {
printf("Hello, World!"); // Line 1 Second Pass:
greet(); // Line 2 ● The compiler performs semantic analysis, checking the
} types of variables, the validity of function calls, and ensuring
that all declarations are properly resolved.
void greet() { // Line 3 ● At this point, when the compiler reaches Line 2 (the call to
printf("Hi!"); // Line 4
greet()), it knows that greet() is defined later in the code (on
}
Line 3). Since the first pass created a representation of the
entire code, it can successfully link the function call to its
First Pass: definition.

● The compiler reads the entire code and Subsequent Passes:


creates a parse tree or abstract syntax tree
(AST). During this pass, it gathers ● The compiler may perform several optimization passes to
information about the structure of the code improve performance (e.g., inlining functions, removing
but does not generate any output yet. redundant code).
● Finally, the compiler generates the final machine code or
intermediate representation based on the optimized
structure of the code
CROSS COMPILER
A compiler that runs on platform (A) and is capable
of generating executable code for platform (B) is
called a cross-compiler.
A cross compiler is a compiler capable of creating
executable code for a platform other than the one
on which the compiler is running. For example, a
compiler that runs on a PC but generates code that
runs on Android devices is a cross compiler.
Source to Source Compiler
A compiler that takes the source code of one programming
language and translates it into the source code of another
programming language is called a source-to-source compiler.

A source-to-source translator, source-to-source compiler (S2S


compiler), transcompiler, or transpiler is a type of translator
that takes the source code of a program written in a
programming language as its input and produces an
equivalent source code in the same or a different
programming language.
Examples of Source-to-Source Compilers
1⃣ C++ to C Transpiler

● Early C++ compilers (like Cfront) translated C++ code into C before
compiling it.
Example:

Babel
● What it Does: Converts modern JavaScript (ES6+) into older JavaScript (ES5).
● Why: So that the code can run in older web browsers that donʼt support newer features.

TypeScript Compiler (tsc)


● What it Does: Converts TypeScript code (which has additional features like types) into
standard JavaScript.
● Why: To allow developers to use features like static types while still creating code that
can run everywhere JavaScript can run.

CoffeeScript
● What it Does: Converts CoffeeScript (a language that compiles into JavaScript) into
standard JavaScript.
● Why: To allow developers to write cleaner, more readable code that is still ultimately run
as JavaScript.
PHASES OF COMPILER
A compiler is a software program that converts the high-level
source code written in a programming language into low-level
machine code that can be executed by the computer hardware.
The process of converting the source code into machine code
involves several phases or stages, which are collectively known as
the phases of a compiler.
Phases of Compiler
1ST PHASE OF COMPILER: LEXICAL ANALYSIS
The first phase of a compiler is lexical analysis, also
known as scanning. This phase reads the source code and
breaks it into a stream of tokens, which are the basic
units of the programming language. The tokens are then
passed on to the next phase for further processing.
Lexical analyzer reads the stream of characters making
up the source program and groups the characters into
meaningful sequences called lexeme.
A token describes a pattern of characters having same
meaning in the source program. (such as identifiers,
operators, keywords, numbers, delimiters and so on)
Ex: newval := oldval + 12 => tokens: newval identifier
:= assignment operator
oldval identifier
+ add operator
12 a number
• Puts information about identifiers into the symbol table.
• Regular expressions are used to describe tokens (lexical
constructs).
• A (Deterministic) Finite State Automaton can be used in the
implementation of a lexical analyzer.
2ND PHASE OF COMPILER: SYNTAX ANALYZER
The second phase of a compiler is syntax analysis, also known as
parsing. This phase takes the stream of tokens generated by the
lexical analysis phase and checks whether they conform to the
grammar of the programming language. The output of this phase is
usually an Abstract Syntax Tree (AST)
• A syntax analyzer creates a general parse tree for a given
program.
• A syntax analyzer also called a parser.
• A syntax of a language is specified by a C.F.G.
• The rate in C.F.G is mostly recursive
EXAMPLE: newval := oldval + 12
3RD PHASE OF COMPILER: SEMANTIC ANALYSIS
The compiler checks the tokens and syntax tree for semantic correctness. This
involves type checking, scope resolution, and ensuring that variables and
functions are used correctly.

The third phase of a compiler is semantic analysis. This phase checks whether
the code is semantically correct, i.e., whether it conforms to the languageʼs
type system and other semantic rules. In this stage, the compiler checks the
meaning of the source code to ensure that it makes sense. The compiler
performs type checking, which ensures that variables are used correctly and
that operations are performed on compatible data types. The compiler also
checks for other semantic errors, such as undeclared variables and incorrect
function calls.
• A semantic analyzer checks the source program for
semantic errors and collect the type information for
the code generation.
• Normally semantic information cannot be
represented by a context-free language used in
syntax analyzer.
• So,
Context free grammars used in the syntax analysis are
integrated with attributes(semantic rules)
-the result is a syntax-directed translation.
-Attribute Grammars.
4TH PHASE OF COMPILER: INTERMEDIATE CODE
GENERATOR
The fourth phase of a compiler is intermediate code generation. This phase generates an
intermediate representation of the source code that can be easily translated into machine
code.
After syntax and semantic analysis of the source program, many compilers generate an
explicit low-level or machine-like intermediate representation (a program for an abstract
machine). This intermediate representation should have two important properties:
– it should be easy to produce and
– it should be easy to translate into the target machine.
The considered intermediate form called three-address code, which consists of a sequence of
assembly-like instructions with three operands per instruction. Each operand can act like a
register.
5TH PHASE OF COMPILER: CODE OPTIMIZATION
The fifth phase of a compiler is optimization. This phase
applies various optimization techniques to the intermediate
code to improve the performance of the generated machine
code.
Code Optimization: to generate better target code
The machine-independent code-optimization phase attempts
to improve the intermediate code so that better target code
will result.
Usually better means: – faster, shorter code, or target code
that consumes less power.
EXAMPLE:
newval :=oldval*fact+1
Id1 :=id2 *id3+1
MUL id2,id3,temp1
ADD temp1,#1,temp2
MOV temp2,id1
6TH PHASE OF COMPILER: CODE GENERATION
The final phase of a compiler is code generation. This phase
takes the optimized intermediate code and generates the
actual machine code that can be executed by the target
hardware
In this phase we will be achieve our target code.
-The final phase of the compiler is the generation of target
code consisting normally relocatable code or assembly code.
-since code generator generate the assembly code in term in
term of registers from the input it got from above steps.
Example:
For above example (suppose id1,id2,id3 are saved to
registers AX,BX,CX respectively), then assembly code
will be)
id+id*id
MUL BX,CX
ADD AX, CX
Home work:
Cross Compiler

You might also like