Compiler Construction: By: Engr. Muhammad Adnan Malik Class of BS-CS, NCBA&E, MULTAN
Compiler Construction: By: Engr. Muhammad Adnan Malik Class of BS-CS, NCBA&E, MULTAN
Lecture#1
Course Outline/Intro,
Why study this course?,
Programming Languages
Compilers, Interpreters.
Course Outline
WILL BE MADE AVAILABLE ON GROUP
(HEC OUTLINE)
HEC-OUTLINE
Important
Maintain good attendance, late comers will face loss in sessional
marks.
• Every country or may be a city or an area have its own language which act
as a medium of interaction among them. For example Chinese, English,
Punjabi etc.
• So hence, when two or more persons who speak different languages, when
try to communicate with each other, needs a translation for understanding.
Translate below
• 你好,今天过得好吗?(Chinese)
PRGRAMMING
LANGUAGES
How does a computer translate the higher level language into its own machine
language, because in the end, it is the CPU that will performs all the
operation?
“compiler or interpreter”.
Compiler and interpreter
Compiler and interpreter
Compiler: A compiler is computer software that
transforms or translates computer code written in
one programming language (the source language) into
another computer language (the target language).
Compiler and interpreter
Interpreter: An interpreter is also a translator just like compiler, but it is
slightly different from compiler.
“Compiler reads the source or input program as a whole and then performs
the translation(execution is done by CPU) and check for errors where an
interpreter read the code line by line and then produces output and errors for
each”.
• Can you give some advantages and disadvantages for both compiler and
interpreter?
• When we need to write more complex program and need to uses it again and
again, we need compiler where interpreter are used for simple programs.
Important
• Does compiler only translate languages, or it can also do other
jobs? Any examples?
• This is the main goal of this course. You must be able to design
your own compiler.
Parts of Compiler
Parts of Compiler
• As we have a sound knowledge of compiler, let us briefly discuss
the parts of compiler and how they work together.
• Front End
• Back End
Parts of Compiler
errors
• IR-Intermediate Representation
Parts of Compiler
• It is also sometimes referred as “TWO PASS” compiler as it goes
through two stages.
errors
• IR-Intermediate Representation
Front End
Front End
Front End of compiler is further dividede into two
modules:
• Scanner
• Parser
Scanner
Scanner
Scanner: Its main task is to read the input characters and produce a
sequence of pairs.
Each pair has following two parts:
• Token type
• Word or part of speech
Scanner
• Example
x=x+y
becomes
<id,x>
<assign,=> <id,x>
<id,x>
<op,+> word
<id,y> token type
Parser
Parser
Parser: A parser recognizes context-free syntax and reports errors
and then builds builds IR for source program
Parser
It is a very important part of compiler. This what a Parser do:
these tokens.
Parser
• In other words, a scanner is unaware of language syntax, it will
just generate stream of tokens.
• It is the parser that will properly understand the grammar of
source language or code.
CFG
(Context Free Grammar)
CFG
• A CFG is the representation of source code or source language
generated by a parser.
expr op term
+ <id,y>
<id,x> <number,2>