SlideShare a Scribd company logo
1 of 49
CHAPTER 2:
PRINCIPLES OF COMPILER
DESIGN
PROGRAM LANGUAGE
TRANSLATION
Why Study Compilers?
Reason #1: understand compilers and languages.
 Understand the code structure and language semantics.
 Understand relation between source code and generated machine code.
 Allow to become a better programmer and increase programmer
productivity and portability
Reason #2: nice balance of theory and practice.
 Theory:
 Mathematical models: regular expressions, automata, grammars, graphs.
 Algorithms that use these models.
 Practice:
 Apply theoretical notions to build a real compiler.
Reason #3: programming experience.
 Creating a compiler entails writing a large computer program which
manipulates complex data structures and implement sophisticated algorithm
 Increasing programming capability
Language Processors:
 Computer programs are generally written in high-level
languages (like C++, Python, and Java).
 A language processor, or language translator, is a computer
program that convert source code from one programming
language to another language or human readable language.
 They also find errors during translation.
Language Processors:
 Language Processors includes
– Compilers and translators,
– Interpreters,
– Pre-processors,
– Assemblers,
– Linkers,
– Loaders
Compilers
 Compiler is a program which translates source program written
in one language to an equivalent program in other language (the
target language).
 Usually the source language is a high level language like Java,
C, C++ etc. whereas the target language is machine code or
"code" that a computer's processor understands.
 An important role of the compiler is to report any errors in the
source program that it detects during the translation process.
Compilers
Figure 2.1: A compiler
Compilers
 If the target program is an executable machine-language program, it
can then be called by the user to process inputs and produce outputs;
 Interpreters - translate programs written in high-level languages
into machine code that a computer understands.
 .
Example 2.1:
 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.
Example 2.1:
 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.
Introduction to Compilers….
 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.
Introduction to Compilers ….
 The source language is optimized for humans.
– It is more user-friendly, to some extent platform-
independent.
– They are easier to read, write, and maintain, and hence it is
easy to avoid errors.
 A program written in any language must be translated into a
form that is understood by the computer.
– This form is typically known as Machine Language (ML)
or Machine Code, or Object Code.
 Consists of streams of 0’s and 1’s
Introduction to Compilers ….
 Some examples of compilers are:
– A Java compiler for the Apple Macintosh
– A COBOL compiler for the SUN
– A C++ compiler for the Apple Macintosh
 If a portion of the input to a Java compiler looked like this:
– a = b + c ∗ d;
– the output corresponding to this input might look something like this:
Sample Problem 2.1.1
 Show the output of a Java native code
compiler, in any typical assembly language, for
the following Java input string: while (x<a+b) x
= 2*x;
Introduction to Compilers ….
Figure 2.4: A Compiler and Interpreter produce very different output for the
same input.
Introduction to Compilers ….
 The input to an interpreter is a program written in a high-level
language, but rather than generating a machine language
program, the interpreter actually carries out the computations
specified in the source program.
 In other words, the output of a compiler is a program, whereas
the output of an interpreter is the source program’s output.
Sample Problem 2.1.2
 Show the compiler output and the interpreter output for the
following Java source code:
for (i=1; i<=4; i++) System.out.println (i*3);
Compiler vs. Interpreter
Compiler Interpreter
A compiler is a program that converts the
entire source code of a programming
language into executable machine code.
An interpreter takes a source program
and runs it line by line, translating
each line as it comes to it.
The compiler takes a large amount of time
to analyze the entire source code but the
overall execution time of the program is
comparatively faster.
An interpreter takes less amount of
time to analyze the source code but
the overall execution time of the
program is slower.
The compiler generates the error message
only after scanning the whole program, so
debugging is comparatively hard as the
error can be present anywhere in the
program.
Its Debugging is easier as it continues
translating the program until the error
is met.
The compiler requires a lot of memory for
generating object codes.
It requires less memory than a
compiler because no object code is
generated.
Generates intermediate object code. No intermediate object code is
Big C notation for compilers
 It is important to remember that a compiler is a program, and it
must be written in some language (machine, assembly, high-
level).
 In describing this program, we are dealing with three
languages:
1) The source language, i.e. the input to the compiler,
2) The object language, i.e. the output of the compiler,
3) The language in which the compiler is written
Sample Problem 2.1.3
 Using the big C notation, show each of the following
compilers:
1) An Ada compiler which runs on the PC and compiles to the
PC machine language.
2) An Ada compiler which compiles to the PC machine
language, but which is written in Ada.
3) An Ada compiler which compiles to the PC machine
language, but which runs on a Sun.
Error handling in Compilers
 Other kinds of errors not
generally detected by the
compiler are called run-time
errors.
Compile time errors
 Syntax errors are reported
by the compiler at compile
time.
Assemblers-
 Assemblers- translate programs written in low-level or
assembly language into machine code.
 Assembly language is called low-level language.
 Because there is one to one correspondence between the assembly
language statements and machine language statements.
 Symbolic form of the machine language, makes it easy to translate
 Compiler generates assembly language as its target language and
assembler translate it into object code.
Assemblers-
 Assembler is basically the 1st interface that is able to
communicate humans with the machine.
Linker
 Linker - is responsible for taking object code
generated by the compiler and linking it with
libraries and other object code to create a final
executable program.
 It resolves references to external symbols and
creates the complete executable.
 Common examples include the GNU linker (ld)
and the Microsoft Visual C++ linker.
Loader
 Loader - is responsible for loading an executable program
into memory for execution.
 It prepares the memory space, reads the binary code, and
resolves any dynamic linking or loading of shared libraries.
 This is often done by the operating system's loader
component.
A language-processing system
 In addition to compiler, several other
programs may be required to create
an executable target program.
 A source program may be divided
into modules stored in separate files.
 The task of collecting the source
program is the responsibility of
another program called preprocessor.
 The target program created by the
compiler may require further
processing before it can be run.
The Phases of a Compiler
 The structure of a compiler includes;
 Lexical analysis,
 Syntax analysis,
 Semantic Analysis,
 Intermediate code generation,
 Code Optimization,
 Code generation,
 Bookkeeping and
 Error handling
The Phases of a Compiler
Phase of Compilers
1. Lexical Analysis
 The first phase of a compiler is lexical analysis, also known as
scanning.
 The lexical phase reads the characters in the source program
and groups them into a stream of tokens in which each token
represents a logically cohesive sequence of characters, such
as, an identifier, keyword, punctuation character.
 The character sequence forming a token is called the lexeme
for the token.
Lexical Analysis
 A token is basically the arrangement of characters that defines
a unit of information in the source code.
 NOTE: In computer science, a program that executes the
process of lexical analysis is called a scanner, tokenizer, or
lexer.
Lexical Analysis
 Roles and Responsibilities of Lexical Analyzer
 It is accountable for terminating the comments
and white spaces from the source program.
 It helps in identifying the tokens.
 Categorization of lexical units.
Lexical Analysis
 A token, also known as a lexeme, a lexical item, or a lexical token, is a string
of input characters which is taken as a unit and passed on to the next phase of
compilation.
 Examples
 key words - while, void, if, for, ...
 identifiers - declared by the programmer
 operators - +, -, *, /, =, ==, ...
 numeric constants - numbers such as 124, 12.35, 0.09E-23, etc.
Lexical Analysis
 Examples
 character constants - single characters or strings of characters enclosed in
quotes
 special characters - characters used as delimiters such as . ( ) , ; :
 comments - ignored by subsequent phases. These must be identified by the
scanner, but are not included in the output.
Sample Problem 2.2.4
2. Syntax Analysis
 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).
Syntax Analysis
 It accepts tokens as input and provides a
parse tree as output. It is also known as
parsing in a compiler.
 The parser will check for proper syntax, issue appropriate error messages,
and determine the underlying structure of the source program.
 The output of this phase may be a stream of atoms or a collection of syntax
trees.
 An atom is an atomic operation, or one that is generally available with one
machine language instruction(s) on most target machines.
 For example, MULT, ADD, and MOVE could represent atomic operations
for multiplication, addition, and moving data in memory.
 Each atom consists of three or four parts:
 an operation,
 one or two operands, and
 a result.
 Example –
 (ADD, A, B, C)
Sample Problem 2.2.5
 Show the atoms corresponding to the following
Java statement:
a = b + c * d ;
 Solution:
 (MULT, c, d, temp1)
 (ADD, b, temp1, temp2)
 (MOVE, temp2, a)
Syntax Analysis
 Roles and Responsibilities of Syntax Analyzer
 Note syntax errors.
 Helps in building a parse tree.
 Acquire tokens from the lexical analyzer.
 Scan the syntax errors, if any.
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.
 Roles and Responsibilities of Semantic
Analyzer:
 Saving collected data to symbol tables or syntax
trees.
 It notifies semantic errors.
 Scanning for semantic errors.
Intermediate Code Generation
 This phase generates an intermediate representation of the source code that
can be easily translated into machine code.
 A middle-level language code generated by a compiler at the time of the
translation of a source program into the object code is known as intermediate
code or text.
Roles and Responsibilities:
Helps in maintaining the priority ordering of the source
language.
Translate the intermediate code into the machine code.
Having operands of instructions.
Optimization
 This phase applies various optimization
techniques to the intermediate code to improve
the performance of the generated machine
code.
 Roles and Responsibilities:
 Remove the unused variables and unreachable code.
 Enhance runtime and execution of the program.
 Produce streamlined code from the intermediate expression.
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.
 Roles and Responsibilities:
 Translate the intermediate code to target machine
code.
 Select and allocate memory spots and registers.
Phases in Compiler
 Generally, phases are divided into two parts:
 Front End phases: These phases are source
language-dependent and target machine,
independents. These generally consist
of lexical analysis, semantic
analysis, syntactic analysis, symbol table
creation, and intermediate code generation.
 The front-end part also includes the error
handling that goes along with each of the
phases.
 Back End phases: The portions of
compilers that depend on the target machine
and do not depend on the source language.
 In the back end, code generation and code
optimization phases, along with error
handling and symbol table operations.
Symbol Table
 The symbol table is mainly known as the data structure of the compiler.
 It helps in storing the identifiers with their name and types.
 It stores:
 Information about the identifier such as,
– its type, (by semantic and intermediate code)
– its scope, (by semantic and intermediate code)
– storage allocation, (by code generation)
– number of arguments and its type for procedure, the type returned.
 It stores the literal constants and strings.
 It helps in storing the function names.
 It also prefers to store variable names and constants.
 It stores labels in source languages.
Error Detecting and
Reporting
 Each phase encounters errors.
 Lexical phase determine the input that do not
form token.
 Syntax phase determine the token that violates
the syntax rule.
 Semantic phase detects the constructs that have
no meaning to operand.
 Example These phases are illustrated by considering the
following statement: position := initial + rate *60
Compiler writing tools
 The compiler writer can use some specialized
tools that help in implementing various phases of
a compiler.
 These tools assist in the creation of an entire
compiler or its parts.
 Some commonly used compiler construction tools
include:
 Parser generators.
 YACC,
 Scanner generators.
 LEX
 Syntax-directed translation engines.

More Related Content

Similar to Chapter 2 Program language translation.pptx

La 5 Pl Translator
La 5   Pl TranslatorLa 5   Pl Translator
La 5 Pl TranslatorCma Mohd
 
Introduction to compiler development
Introduction to compiler developmentIntroduction to compiler development
Introduction to compiler developmentDeepOad
 
1.Overview of Programming.pptx
1.Overview of Programming.pptx1.Overview of Programming.pptx
1.Overview of Programming.pptxVishwas459764
 
COMPILER DESIGN OPTIONS
COMPILER DESIGN OPTIONSCOMPILER DESIGN OPTIONS
COMPILER DESIGN OPTIONSsonalikharade3
 
Compiler_Lecture1.pdf
Compiler_Lecture1.pdfCompiler_Lecture1.pdf
Compiler_Lecture1.pdfAkarTaher
 
Ch 1.pptx
Ch 1.pptxCh 1.pptx
Ch 1.pptxwoldu2
 
design intoduction of_COMPILER_DESIGN.pdf
design intoduction of_COMPILER_DESIGN.pdfdesign intoduction of_COMPILER_DESIGN.pdf
design intoduction of_COMPILER_DESIGN.pdfadvRajatSharma
 
Introduction to Programming Concepts By Aamir Saleem Ansari
Introduction to Programming Concepts By Aamir Saleem AnsariIntroduction to Programming Concepts By Aamir Saleem Ansari
Introduction to Programming Concepts By Aamir Saleem AnsariTech
 
SSD Mod 2 -18CS61_Notes.pdf
SSD Mod 2 -18CS61_Notes.pdfSSD Mod 2 -18CS61_Notes.pdf
SSD Mod 2 -18CS61_Notes.pdfJacobDragonette
 
Language translators
Language translatorsLanguage translators
Language translatorsAditya Sharat
 
Compiler Construction Lecture One .pptx
Compiler Construction Lecture One  .pptxCompiler Construction Lecture One  .pptx
Compiler Construction Lecture One .pptxانشال عارف
 
Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Tirumala Rao
 
2 Programming Language.pdf
2 Programming Language.pdf2 Programming Language.pdf
2 Programming Language.pdfKINGZzofYouTube
 
Compiler design slide share
Compiler design slide shareCompiler design slide share
Compiler design slide shareSudhaa Ravi
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compilerA. S. M. Shafi
 
Software programming and development
Software programming and developmentSoftware programming and development
Software programming and developmentAli Raza
 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptxssuser3b4934
 

Similar to Chapter 2 Program language translation.pptx (20)

La 5 Pl Translator
La 5   Pl TranslatorLa 5   Pl Translator
La 5 Pl Translator
 
Introduction to compiler development
Introduction to compiler developmentIntroduction to compiler development
Introduction to compiler development
 
1.Overview of Programming.pptx
1.Overview of Programming.pptx1.Overview of Programming.pptx
1.Overview of Programming.pptx
 
COMPILER DESIGN OPTIONS
COMPILER DESIGN OPTIONSCOMPILER DESIGN OPTIONS
COMPILER DESIGN OPTIONS
 
Compiler_Lecture1.pdf
Compiler_Lecture1.pdfCompiler_Lecture1.pdf
Compiler_Lecture1.pdf
 
Ch 1.pptx
Ch 1.pptxCh 1.pptx
Ch 1.pptx
 
design intoduction of_COMPILER_DESIGN.pdf
design intoduction of_COMPILER_DESIGN.pdfdesign intoduction of_COMPILER_DESIGN.pdf
design intoduction of_COMPILER_DESIGN.pdf
 
Introduction to Programming Concepts By Aamir Saleem Ansari
Introduction to Programming Concepts By Aamir Saleem AnsariIntroduction to Programming Concepts By Aamir Saleem Ansari
Introduction to Programming Concepts By Aamir Saleem Ansari
 
SSD Mod 2 -18CS61_Notes.pdf
SSD Mod 2 -18CS61_Notes.pdfSSD Mod 2 -18CS61_Notes.pdf
SSD Mod 2 -18CS61_Notes.pdf
 
Language translators
Language translatorsLanguage translators
Language translators
 
Compiler Construction Lecture One .pptx
Compiler Construction Lecture One  .pptxCompiler Construction Lecture One  .pptx
Compiler Construction Lecture One .pptx
 
Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02Dineshmaterial1 091225091539-phpapp02
Dineshmaterial1 091225091539-phpapp02
 
2 Programming Language.pdf
2 Programming Language.pdf2 Programming Language.pdf
2 Programming Language.pdf
 
Compiler design slide share
Compiler design slide shareCompiler design slide share
Compiler design slide share
 
Introduction to compiler
Introduction to compilerIntroduction to compiler
Introduction to compiler
 
Software programming and development
Software programming and developmentSoftware programming and development
Software programming and development
 
3.2
3.23.2
3.2
 
Phases of Compiler.pptx
Phases of Compiler.pptxPhases of Compiler.pptx
Phases of Compiler.pptx
 
df
dfdf
df
 
Ic lecture8
Ic lecture8 Ic lecture8
Ic lecture8
 

Recently uploaded

Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 

Recently uploaded (20)

Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 

Chapter 2 Program language translation.pptx

  • 1. CHAPTER 2: PRINCIPLES OF COMPILER DESIGN PROGRAM LANGUAGE TRANSLATION
  • 2. Why Study Compilers? Reason #1: understand compilers and languages.  Understand the code structure and language semantics.  Understand relation between source code and generated machine code.  Allow to become a better programmer and increase programmer productivity and portability Reason #2: nice balance of theory and practice.  Theory:  Mathematical models: regular expressions, automata, grammars, graphs.  Algorithms that use these models.  Practice:  Apply theoretical notions to build a real compiler. Reason #3: programming experience.  Creating a compiler entails writing a large computer program which manipulates complex data structures and implement sophisticated algorithm  Increasing programming capability
  • 3. Language Processors:  Computer programs are generally written in high-level languages (like C++, Python, and Java).  A language processor, or language translator, is a computer program that convert source code from one programming language to another language or human readable language.  They also find errors during translation.
  • 4. Language Processors:  Language Processors includes – Compilers and translators, – Interpreters, – Pre-processors, – Assemblers, – Linkers, – Loaders
  • 5. Compilers  Compiler is a program which translates source program written in one language to an equivalent program in other language (the target language).  Usually the source language is a high level language like Java, C, C++ etc. whereas the target language is machine code or "code" that a computer's processor understands.  An important role of the compiler is to report any errors in the source program that it detects during the translation process.
  • 7. Compilers  If the target program is an executable machine-language program, it can then be called by the user to process inputs and produce outputs;  Interpreters - translate programs written in high-level languages into machine code that a computer understands.  .
  • 8. Example 2.1:  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.
  • 9. Example 2.1:  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.
  • 10. Introduction to Compilers….  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.
  • 11. Introduction to Compilers ….  The source language is optimized for humans. – It is more user-friendly, to some extent platform- independent. – They are easier to read, write, and maintain, and hence it is easy to avoid errors.  A program written in any language must be translated into a form that is understood by the computer. – This form is typically known as Machine Language (ML) or Machine Code, or Object Code.  Consists of streams of 0’s and 1’s
  • 12. Introduction to Compilers ….  Some examples of compilers are: – A Java compiler for the Apple Macintosh – A COBOL compiler for the SUN – A C++ compiler for the Apple Macintosh  If a portion of the input to a Java compiler looked like this: – a = b + c ∗ d; – the output corresponding to this input might look something like this:
  • 13. Sample Problem 2.1.1  Show the output of a Java native code compiler, in any typical assembly language, for the following Java input string: while (x<a+b) x = 2*x;
  • 14. Introduction to Compilers …. Figure 2.4: A Compiler and Interpreter produce very different output for the same input.
  • 15. Introduction to Compilers ….  The input to an interpreter is a program written in a high-level language, but rather than generating a machine language program, the interpreter actually carries out the computations specified in the source program.  In other words, the output of a compiler is a program, whereas the output of an interpreter is the source program’s output.
  • 16. Sample Problem 2.1.2  Show the compiler output and the interpreter output for the following Java source code: for (i=1; i<=4; i++) System.out.println (i*3);
  • 17. Compiler vs. Interpreter Compiler Interpreter A compiler is a program that converts the entire source code of a programming language into executable machine code. An interpreter takes a source program and runs it line by line, translating each line as it comes to it. The compiler takes a large amount of time to analyze the entire source code but the overall execution time of the program is comparatively faster. An interpreter takes less amount of time to analyze the source code but the overall execution time of the program is slower. The compiler generates the error message only after scanning the whole program, so debugging is comparatively hard as the error can be present anywhere in the program. Its Debugging is easier as it continues translating the program until the error is met. The compiler requires a lot of memory for generating object codes. It requires less memory than a compiler because no object code is generated. Generates intermediate object code. No intermediate object code is
  • 18. Big C notation for compilers  It is important to remember that a compiler is a program, and it must be written in some language (machine, assembly, high- level).  In describing this program, we are dealing with three languages: 1) The source language, i.e. the input to the compiler, 2) The object language, i.e. the output of the compiler, 3) The language in which the compiler is written
  • 19. Sample Problem 2.1.3  Using the big C notation, show each of the following compilers: 1) An Ada compiler which runs on the PC and compiles to the PC machine language. 2) An Ada compiler which compiles to the PC machine language, but which is written in Ada. 3) An Ada compiler which compiles to the PC machine language, but which runs on a Sun.
  • 20. Error handling in Compilers  Other kinds of errors not generally detected by the compiler are called run-time errors. Compile time errors  Syntax errors are reported by the compiler at compile time.
  • 21. Assemblers-  Assemblers- translate programs written in low-level or assembly language into machine code.  Assembly language is called low-level language.  Because there is one to one correspondence between the assembly language statements and machine language statements.  Symbolic form of the machine language, makes it easy to translate  Compiler generates assembly language as its target language and assembler translate it into object code.
  • 22. Assemblers-  Assembler is basically the 1st interface that is able to communicate humans with the machine.
  • 23. Linker  Linker - is responsible for taking object code generated by the compiler and linking it with libraries and other object code to create a final executable program.  It resolves references to external symbols and creates the complete executable.  Common examples include the GNU linker (ld) and the Microsoft Visual C++ linker.
  • 24. Loader  Loader - is responsible for loading an executable program into memory for execution.  It prepares the memory space, reads the binary code, and resolves any dynamic linking or loading of shared libraries.  This is often done by the operating system's loader component.
  • 25. A language-processing system  In addition to compiler, several other programs may be required to create an executable target program.  A source program may be divided into modules stored in separate files.  The task of collecting the source program is the responsibility of another program called preprocessor.  The target program created by the compiler may require further processing before it can be run.
  • 26. The Phases of a Compiler  The structure of a compiler includes;  Lexical analysis,  Syntax analysis,  Semantic Analysis,  Intermediate code generation,  Code Optimization,  Code generation,  Bookkeeping and  Error handling
  • 27. The Phases of a Compiler
  • 29. 1. Lexical Analysis  The first phase of a compiler is lexical analysis, also known as scanning.  The lexical phase reads the characters in the source program and groups them into a stream of tokens in which each token represents a logically cohesive sequence of characters, such as, an identifier, keyword, punctuation character.  The character sequence forming a token is called the lexeme for the token.
  • 30. Lexical Analysis  A token is basically the arrangement of characters that defines a unit of information in the source code.  NOTE: In computer science, a program that executes the process of lexical analysis is called a scanner, tokenizer, or lexer.
  • 31. Lexical Analysis  Roles and Responsibilities of Lexical Analyzer  It is accountable for terminating the comments and white spaces from the source program.  It helps in identifying the tokens.  Categorization of lexical units.
  • 32. Lexical Analysis  A token, also known as a lexeme, a lexical item, or a lexical token, is a string of input characters which is taken as a unit and passed on to the next phase of compilation.  Examples  key words - while, void, if, for, ...  identifiers - declared by the programmer  operators - +, -, *, /, =, ==, ...  numeric constants - numbers such as 124, 12.35, 0.09E-23, etc.
  • 33. Lexical Analysis  Examples  character constants - single characters or strings of characters enclosed in quotes  special characters - characters used as delimiters such as . ( ) , ; :  comments - ignored by subsequent phases. These must be identified by the scanner, but are not included in the output.
  • 35. 2. Syntax Analysis  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).
  • 36. Syntax Analysis  It accepts tokens as input and provides a parse tree as output. It is also known as parsing in a compiler.
  • 37.  The parser will check for proper syntax, issue appropriate error messages, and determine the underlying structure of the source program.  The output of this phase may be a stream of atoms or a collection of syntax trees.  An atom is an atomic operation, or one that is generally available with one machine language instruction(s) on most target machines.  For example, MULT, ADD, and MOVE could represent atomic operations for multiplication, addition, and moving data in memory.
  • 38.  Each atom consists of three or four parts:  an operation,  one or two operands, and  a result.  Example –  (ADD, A, B, C)
  • 39. Sample Problem 2.2.5  Show the atoms corresponding to the following Java statement: a = b + c * d ;  Solution:  (MULT, c, d, temp1)  (ADD, b, temp1, temp2)  (MOVE, temp2, a)
  • 40. Syntax Analysis  Roles and Responsibilities of Syntax Analyzer  Note syntax errors.  Helps in building a parse tree.  Acquire tokens from the lexical analyzer.  Scan the syntax errors, if any.
  • 41. 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.  Roles and Responsibilities of Semantic Analyzer:  Saving collected data to symbol tables or syntax trees.  It notifies semantic errors.  Scanning for semantic errors.
  • 42. Intermediate Code Generation  This phase generates an intermediate representation of the source code that can be easily translated into machine code.  A middle-level language code generated by a compiler at the time of the translation of a source program into the object code is known as intermediate code or text. Roles and Responsibilities: Helps in maintaining the priority ordering of the source language. Translate the intermediate code into the machine code. Having operands of instructions.
  • 43. Optimization  This phase applies various optimization techniques to the intermediate code to improve the performance of the generated machine code.  Roles and Responsibilities:  Remove the unused variables and unreachable code.  Enhance runtime and execution of the program.  Produce streamlined code from the intermediate expression.
  • 44. 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.  Roles and Responsibilities:  Translate the intermediate code to target machine code.  Select and allocate memory spots and registers.
  • 45. Phases in Compiler  Generally, phases are divided into two parts:  Front End phases: These phases are source language-dependent and target machine, independents. These generally consist of lexical analysis, semantic analysis, syntactic analysis, symbol table creation, and intermediate code generation.  The front-end part also includes the error handling that goes along with each of the phases.  Back End phases: The portions of compilers that depend on the target machine and do not depend on the source language.  In the back end, code generation and code optimization phases, along with error handling and symbol table operations.
  • 46. Symbol Table  The symbol table is mainly known as the data structure of the compiler.  It helps in storing the identifiers with their name and types.  It stores:  Information about the identifier such as, – its type, (by semantic and intermediate code) – its scope, (by semantic and intermediate code) – storage allocation, (by code generation) – number of arguments and its type for procedure, the type returned.  It stores the literal constants and strings.  It helps in storing the function names.  It also prefers to store variable names and constants.  It stores labels in source languages.
  • 47. Error Detecting and Reporting  Each phase encounters errors.  Lexical phase determine the input that do not form token.  Syntax phase determine the token that violates the syntax rule.  Semantic phase detects the constructs that have no meaning to operand.
  • 48.  Example These phases are illustrated by considering the following statement: position := initial + rate *60
  • 49. Compiler writing tools  The compiler writer can use some specialized tools that help in implementing various phases of a compiler.  These tools assist in the creation of an entire compiler or its parts.  Some commonly used compiler construction tools include:  Parser generators.  YACC,  Scanner generators.  LEX  Syntax-directed translation engines.