SlideShare una empresa de Scribd logo
1 de 12
Descargar para leer sin conexión
Compiler Design
For
Computer Science
&
Information Technology
By
www.thegateacademy.com
Syllabus Compiler Design
THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th
Cross, 10th
Main, Jayanagar 4th
Block, Bangalore-11
: 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com
Syllabus for Compiler Design
Lexical analysis, Parsing, Syntax directed translation, Runtime environments, Intermediate and
target code generation, Basics of code optimization.
Analysis of GATE Papers
(Compiler Design)
Year Percentage of marks Overall Percentage
2013 3.00
5.03%
2012 4.00
2011 1.00
2010 4.00
2009 0.66
2008 4.00
2007 7.33
2006 7.33
2005 7.33
2004 3.33
2003 13.33
Contents Compiler Design
THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th
Cross, 10th
Main, Jayanagar 4th
Block, Bangalore-11
: 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page i
CC OO NN TT EE NN TT SS
Chapter Page No.
#1. Introduction to Compilers 1-23
 Compilers 1-5
 The Phases of a Compiler 5-12
 Lexical Analyzer 13-16
 Assignment 1 17-18
 Assignment 2 19-20
 Answer Keys 21
 Explanations 21-23
#2. Parsing 24-99
 Syntax Analysis 24
 The Role of the Parser 24-25
 Context-Free Grammars 25-29
 Writing a Grammar 30-34
 Top-Down Parsing 34-47
 Bottom-Up Parsing 47-51
 Operator-Precedence Parsing 52-63
 LR Parsers 63-83
 Parser Generators 83-90
 Assignment 1 91-92
 Assignment 2 93-95
 Answer Keys 96
 Explanations 96-99
#3. Syntax Directed Translation 100-143
 Syntax Directed Definition 100-106
 Construction of Syntax Trees 106-112
 Bottom-Up Evaluation of S-Attributed Definitions 112-113
 L-Atributed Definitions 114-118
 Top-Down Translation 118-122
 Bottom-Up Evaluation of Inherited Attributes 123-126
 Run Time Environment 126-129
 Assignment 1 130-132
 Assignment 2 132-136
 Answer Keys 137
 Explanations 137-143
Contents Compiler Design
THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th
Cross, 10th
Main, Jayanagar 4th
Block, Bangalore-11
: 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page ii
#4. Intermediate Code Generation 144-177
 Intermediate language 144-150
 Code Generation 150
 Issues in the Design of a Code Generator 151-153
 Target Machine 154-156
 Code Optimization 156-161
 The Principal Sources of Optimization 161-166
 Assignment 1 167-170
 Assignment 2 170-171
 Answer Keys 172
 Explanations 172-177
Module Test 178-188
 Test Questions 178-185
 Answer Keys 186
 Explanations 186-188
Reference Books 189
Chapter 1 Compiler Design
THE GATE ACADEMY PVT.LTD. H.O.: #74, KeshavaKrupa (third Floor), 30th
Cross, 10th
Main, Jayanagar 4th
Block, Bangalore-11
: 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page 1
CHAPTER 1
Introduction to Compilers
Compilers
A compiler is a program that reads a program written in one language – the source language –
and translates it into an equivalent program in another language – the target language (see Fig
1.1). As an important part of this translation process, the compiler reports to its user the
presence of errors in the source program.
Fig. 1.1 a compiler
Compilers are sometimes classified as single pass multi-pass, load and go, debugging or
optimising depending on how they have been constructed or on what function they are
supposed to perform. Despite this apparent complexity, the basic tasks that any compiler must
perform are essentially the same. By understanding these tasks, we can construct compilers for a
variety of source languages and target machines using the same basic techniques.
The Analysis-Synthesis Model of Compilation
There are two parts of compilation: analysis and synthesis. The analysis part breaks up the
source program into constituent pieces and creates an intermediate representation of the source
program. The synthesis part constructs the desired target program from the intermediate
representation. Out of the two parts, synthesis requires the most specialized techniques.
During analysis, the operations implied by the source program are determined and recorded in a
hierarchical structure called as tree. Often, a special kind of tree called as Syntax tree is used, in
which each node represents an operation and the children of node represent the argument of
the operation. For example, a syntax tree for an assignment statement is shown in Fig. 1.2
compiler
source
program
target
program
error
message
s
Chapter 1 Compiler Design
THE GATE ACADEMY PVT.LTD. H.O.: #74, KeshavaKrupa (third Floor), 30th
Cross, 10th
Main, Jayanagar 4th
Block, Bangalore-11
: 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page 2
Fig. 1.2 Syntax tree for position : = initial + rate 60.
The Context of a Compiler
In addition to a 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 sometimes entrusted to a distinct program, called as
Preprocessor. The preprocessor may also expand shorthand’s, called macros, into source
language statements.
Following Fig. 1.3 shows a typical “compilation.” The target program created by the compiler
may require further processing before it can be run. The compiler in Fig. 1.3 creates assembly
code that is translated by an assembler into machine code and then linked together with some
library routines into the code that actually runs on the machine.
Fig.1.3 A language-processing system
skeletal source program
preprocessor
source program
compiler
target assembly program
assembler
relocatable machine code
loader/link editor
absolute machine code
library,
relocatable object file
position
initial
rate
Chapter 1 Compiler Design
THE GATE ACADEMY PVT.LTD. H.O.: #74, KeshavaKrupa (third Floor), 30th
Cross, 10th
Main, Jayanagar 4th
Block, Bangalore-11
: 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page 3
Analysis of the Source Program
1. Linear or Lexical analysis, in which stream of characters making up the source program
is read from left-to-right and grouped into tokens that are sequences of characters
having a collective meaning.
2. Hierarchical or Syntax analysis, in which characters or tokens are grouped hierarchically
into nested collections with collective meanings.
3. Semantic analysis, in which certain checks are performed to ensure that the components
of a program fit together meaningfully.
Lexical Analysis
A token is a string of characters, categorized according to the rules as a symbol (e.g. IDENTIFIER,
NUMBER, COMMA, etc.). The process of forming tokens from an input stream of characters is
called tokenization and the lexer categorizes them according to symbol type. A token can look
like anything that is useful for processing an input text stream or text file.
A lexical analyzer generally does nothing with combinations of tokens, a task left for a parser.
For example, a typical lexical analyzer recognizes parenthesis as tokens, but does nothing to
ensure that each '(' is matched with a ')'.
In a compiler, linear analysis is called lexical analysis or scanning. For example, in lexical
analysis the characters in the assignment statement
position : =initial + rate * 60
would be grouped into the following tokens:
1. The identifier position.
2. The assignment symbol : =
3. The identifier initial.
4. The plus sign
5. The identifier rate.
6. The multiplication sign
7. The number 60
The blanks separating the characters of these tokens would normally be eliminated during
lexical analysis.
Syntax Analysis
Hierarchical analysis is called parsing or syntax analysis. It involves grouping the tokens of the
source program into grammatical phrases that are used by the compiler to synthesize output.
Usually, the grammatical phrases of the source program are represented by a parse tree such as
the one shown in Fig. 1.4.
Chapter 1 Compiler Design
THE GATE ACADEMY PVT.LTD. H.O.: #74, KeshavaKrupa (third Floor), 30th
Cross, 10th
Main, Jayanagar 4th
Block, Bangalore-11
: 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page 4
Fig.1.4 Parse tree for position: = initial + rate * 60
In the expression initial + rate * 60, the phrase rate * 60 is a logical unit because the usual
conventions of arithmetic expressions tell us that multiplication is performed before addition.
Because the expression initial + rate is followed by a *, it is not grouped into a single phrase by
itself in Fig. 1.4.
The hierarchical structure of a program is usually expressed by recursive rules. For example, we
might have the following rules as part of the definition of expressions:
1. Any identifier is an expression.
2. Any number is an expression.
3. If expression1 and expression2 are expressions, then so are
expression1 + expression 2
expression1 * expression 2
(expression1)
Rules (1) and (2) are non-recursive basic rules, while (3) defines expressions in terms of
operators applied to other expressions. Thus, by rule (1), initial and rate are expressions. By rule
(2), 60 is an expression, while by rule (3), we can first infer that rate*60 is an expression and
finally that initial + rate*60 is an expression.
assignment
statement
identifier
position
expression
expression
identifier
initial
expression
expression
identifier
rate
expression
number
60
Chapter 1 Compiler Design
THE GATE ACADEMY PVT.LTD. H.O.: #74, KeshavaKrupa (third Floor), 30th
Cross, 10th
Main, Jayanagar 4th
Block, Bangalore-11
: 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page 5
Fig. 1.5 Semantic analysis inserts a conversion from integer to real.
The parse tree in Fig.1.4 describes the syntactic structure of the input. A more common internal
representation of this syntactic structure is given by the syntax in Fig. 1.5(a). A syntax tree is a
compressed representation of the parse tree in which the operators appear as the interior
nodes, and the operands of an operator are the children of the node for that operator.
Semantic Analysis
The semantic analysis phase checks the source program for semantic errors and gathers type
information for the subsequent code-generation phase. It uses the hierarchical structure
determined by the syntax-analysis phase to identify the operators and operands of expressions
and statements.
An important component of semantic analysis is type checking.
Here the compiler checks that each operator has operands that are permitted by the source
language specification. For example, many programming language definitions require a compiler
to report an error every time a real number is used to index an array. However, the language
specification may permit some operand corrections, for example, when binary arithmetic
operator is applied to an integer and real. In this case, the compiler may need to convert the
integer to a real.
The Phases of a Compiler
Conceptually, a compiler operates in phases, each of which transforms the source program from
one representation to another. A typical decomposition of a compiler is shown in Fig. 1.6. In
practice, some of the phases may be grouped together, and the intermediate representations
between the grouped phases need not be explicitly constructed.
position
initial
rate
int to real
position
initial
rate
(a)
(b)
Chapter 1 Compiler Design
THE GATE ACADEMY PVT.LTD. H.O.: #74, KeshavaKrupa (third Floor), 30th
Cross, 10th
Main, Jayanagar 4th
Block, Bangalore-11
: 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page 6
Phases
Fig. 1.6 Phases of a compiler
The first three forming the bulk of the analysis portion of a compiler, were introduced in the last
section. Two other activities, symbol-table management and error handling, are shown
interacting with the six phases of compilation lexical analysis, syntax analysis, semantic analysis,
intermediate code generation, code optimization, and code generation. Informally, we shall also
call the symbol-table manager and the error handler “phases.”
The 6 phases divided into 2 Groups
1. Front End: Depends on stream of tokens and parsetree
2. Back End: Dependent on Target, Independent of source code
Symbol-Table Management
A symbol table is a data structure containing a record for each identifier, with fields for the
attributes of the identifier. The data structure allows us to find the record for each identifier
quickly and to store or retrieve data from that record quickly.
Symbol table is a Data Structure in a Compiler used for Managing information about variables &
their attributes.
Error Detection and Reporting
Each phase can encounter errors. However, after detecting an error, a phase must somehow deal
with that error, so that compilation can proceed, allowing further errors in the source program
to be detected. A compiler that stops when it finds the first error is not as helpful as it could be.
Source Program
Lexical Analyzer
Syntax Analyzer
Semantic Analyzer
Intermediate Code Generator
Code Optimizer
Target Code Generator
Symbol Table
Manager
Error
Handler
Target Program
Computer Science Engineering : Compiler design, THE GATE ACADEMY

Más contenido relacionado

Destacado

Data Locality
Data LocalityData Locality
Data LocalitySyam Lal
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysisIffat Anjum
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Iffat Anjum
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical AnalysisMunni28
 
About Tokens and Lexemes
About Tokens and LexemesAbout Tokens and Lexemes
About Tokens and LexemesBen Scholzen
 
Compiler Design - Introduction to Compiler
Compiler Design - Introduction to CompilerCompiler Design - Introduction to Compiler
Compiler Design - Introduction to CompilerIffat Anjum
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)Sami Said
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Context free grammars
Context free grammarsContext free grammars
Context free grammarsRonak Thakkar
 
what is compiler and five phases of compiler
what is compiler and five phases of compilerwhat is compiler and five phases of compiler
what is compiler and five phases of compileradilmehmood93
 

Destacado (20)

Data Locality
Data LocalityData Locality
Data Locality
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
Compiler design
Compiler designCompiler design
Compiler design
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Buffers
BuffersBuffers
Buffers
 
Unit 1 cd
Unit 1 cdUnit 1 cd
Unit 1 cd
 
Lecture4 lexical analysis2
Lecture4 lexical analysis2Lecture4 lexical analysis2
Lecture4 lexical analysis2
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
 
Compiler unit 1
Compiler unit 1Compiler unit 1
Compiler unit 1
 
Lexical Analyzers and Parsers
Lexical Analyzers and ParsersLexical Analyzers and Parsers
Lexical Analyzers and Parsers
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
About Tokens and Lexemes
About Tokens and LexemesAbout Tokens and Lexemes
About Tokens and Lexemes
 
Compiler Design - Introduction to Compiler
Compiler Design - Introduction to CompilerCompiler Design - Introduction to Compiler
Compiler Design - Introduction to Compiler
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
what is compiler and five phases of compiler
what is compiler and five phases of compilerwhat is compiler and five phases of compiler
what is compiler and five phases of compiler
 

Más de klirantga

Mechanical Engineering : Engineering mechanics, THE GATE ACADEMY
Mechanical Engineering  : Engineering mechanics, THE GATE ACADEMYMechanical Engineering  : Engineering mechanics, THE GATE ACADEMY
Mechanical Engineering : Engineering mechanics, THE GATE ACADEMYklirantga
 
Instrumentation Engineering : Transducers, THE GATE ACADEMY
 Instrumentation Engineering : Transducers, THE GATE ACADEMY Instrumentation Engineering : Transducers, THE GATE ACADEMY
Instrumentation Engineering : Transducers, THE GATE ACADEMYklirantga
 
Instrumentation Engineering : Signals & systems, THE GATE ACADEMY
Instrumentation Engineering : Signals & systems, THE GATE ACADEMYInstrumentation Engineering : Signals & systems, THE GATE ACADEMY
Instrumentation Engineering : Signals & systems, THE GATE ACADEMYklirantga
 
Electrical and Electronics Engineering : Power electronics, THE GATE ACADEMY
Electrical and Electronics Engineering : Power electronics, THE GATE ACADEMYElectrical and Electronics Engineering : Power electronics, THE GATE ACADEMY
Electrical and Electronics Engineering : Power electronics, THE GATE ACADEMYklirantga
 
Electronics and Communication Engineering : Digital circuits, THE GATE ACADEMY
Electronics and Communication Engineering : Digital circuits, THE GATE ACADEMYElectronics and Communication Engineering : Digital circuits, THE GATE ACADEMY
Electronics and Communication Engineering : Digital circuits, THE GATE ACADEMYklirantga
 
Electronics and Communication Engineering : Control systems, THE GATE ACADEMY
 Electronics and Communication Engineering  : Control systems, THE GATE ACADEMY Electronics and Communication Engineering  : Control systems, THE GATE ACADEMY
Electronics and Communication Engineering : Control systems, THE GATE ACADEMYklirantga
 
Electronics and Communication Engineering : Communications, THE GATE ACADEMY
Electronics and Communication Engineering : Communications, THE GATE ACADEMYElectronics and Communication Engineering : Communications, THE GATE ACADEMY
Electronics and Communication Engineering : Communications, THE GATE ACADEMYklirantga
 
Electronics and Communication Engineering : Analog circuits, THE GATE ACADEMY
 Electronics and Communication Engineering : Analog circuits, THE GATE ACADEMY Electronics and Communication Engineering : Analog circuits, THE GATE ACADEMY
Electronics and Communication Engineering : Analog circuits, THE GATE ACADEMYklirantga
 
Computer Science Engineering: Discrete mathematics & graph theory, THE GATE A...
Computer Science Engineering: Discrete mathematics & graph theory, THE GATE A...Computer Science Engineering: Discrete mathematics & graph theory, THE GATE A...
Computer Science Engineering: Discrete mathematics & graph theory, THE GATE A...klirantga
 
Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY
Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMYComputer Science Engineering : Data structure & algorithm, THE GATE ACADEMY
Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMYklirantga
 
Civil Engineering : Rcc & steel structures, THE GATE ACADEMY
Civil Engineering : Rcc & steel structures, THE GATE ACADEMYCivil Engineering : Rcc & steel structures, THE GATE ACADEMY
Civil Engineering : Rcc & steel structures, THE GATE ACADEMYklirantga
 
Gate material civil engineering, environmental engineering
Gate material   civil engineering, environmental engineeringGate material   civil engineering, environmental engineering
Gate material civil engineering, environmental engineeringklirantga
 

Más de klirantga (12)

Mechanical Engineering : Engineering mechanics, THE GATE ACADEMY
Mechanical Engineering  : Engineering mechanics, THE GATE ACADEMYMechanical Engineering  : Engineering mechanics, THE GATE ACADEMY
Mechanical Engineering : Engineering mechanics, THE GATE ACADEMY
 
Instrumentation Engineering : Transducers, THE GATE ACADEMY
 Instrumentation Engineering : Transducers, THE GATE ACADEMY Instrumentation Engineering : Transducers, THE GATE ACADEMY
Instrumentation Engineering : Transducers, THE GATE ACADEMY
 
Instrumentation Engineering : Signals & systems, THE GATE ACADEMY
Instrumentation Engineering : Signals & systems, THE GATE ACADEMYInstrumentation Engineering : Signals & systems, THE GATE ACADEMY
Instrumentation Engineering : Signals & systems, THE GATE ACADEMY
 
Electrical and Electronics Engineering : Power electronics, THE GATE ACADEMY
Electrical and Electronics Engineering : Power electronics, THE GATE ACADEMYElectrical and Electronics Engineering : Power electronics, THE GATE ACADEMY
Electrical and Electronics Engineering : Power electronics, THE GATE ACADEMY
 
Electronics and Communication Engineering : Digital circuits, THE GATE ACADEMY
Electronics and Communication Engineering : Digital circuits, THE GATE ACADEMYElectronics and Communication Engineering : Digital circuits, THE GATE ACADEMY
Electronics and Communication Engineering : Digital circuits, THE GATE ACADEMY
 
Electronics and Communication Engineering : Control systems, THE GATE ACADEMY
 Electronics and Communication Engineering  : Control systems, THE GATE ACADEMY Electronics and Communication Engineering  : Control systems, THE GATE ACADEMY
Electronics and Communication Engineering : Control systems, THE GATE ACADEMY
 
Electronics and Communication Engineering : Communications, THE GATE ACADEMY
Electronics and Communication Engineering : Communications, THE GATE ACADEMYElectronics and Communication Engineering : Communications, THE GATE ACADEMY
Electronics and Communication Engineering : Communications, THE GATE ACADEMY
 
Electronics and Communication Engineering : Analog circuits, THE GATE ACADEMY
 Electronics and Communication Engineering : Analog circuits, THE GATE ACADEMY Electronics and Communication Engineering : Analog circuits, THE GATE ACADEMY
Electronics and Communication Engineering : Analog circuits, THE GATE ACADEMY
 
Computer Science Engineering: Discrete mathematics & graph theory, THE GATE A...
Computer Science Engineering: Discrete mathematics & graph theory, THE GATE A...Computer Science Engineering: Discrete mathematics & graph theory, THE GATE A...
Computer Science Engineering: Discrete mathematics & graph theory, THE GATE A...
 
Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY
Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMYComputer Science Engineering : Data structure & algorithm, THE GATE ACADEMY
Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY
 
Civil Engineering : Rcc & steel structures, THE GATE ACADEMY
Civil Engineering : Rcc & steel structures, THE GATE ACADEMYCivil Engineering : Rcc & steel structures, THE GATE ACADEMY
Civil Engineering : Rcc & steel structures, THE GATE ACADEMY
 
Gate material civil engineering, environmental engineering
Gate material   civil engineering, environmental engineeringGate material   civil engineering, environmental engineering
Gate material civil engineering, environmental engineering
 

Último

Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxElton John Embodo
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 

Último (20)

Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
EMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docxEMBODO Lesson Plan Grade 9 Law of Sines.docx
EMBODO Lesson Plan Grade 9 Law of Sines.docx
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 

Computer Science Engineering : Compiler design, THE GATE ACADEMY

  • 1.
  • 2. Compiler Design For Computer Science & Information Technology By www.thegateacademy.com
  • 3. Syllabus Compiler Design THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Syllabus for Compiler Design Lexical analysis, Parsing, Syntax directed translation, Runtime environments, Intermediate and target code generation, Basics of code optimization. Analysis of GATE Papers (Compiler Design) Year Percentage of marks Overall Percentage 2013 3.00 5.03% 2012 4.00 2011 1.00 2010 4.00 2009 0.66 2008 4.00 2007 7.33 2006 7.33 2005 7.33 2004 3.33 2003 13.33
  • 4. Contents Compiler Design THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page i CC OO NN TT EE NN TT SS Chapter Page No. #1. Introduction to Compilers 1-23  Compilers 1-5  The Phases of a Compiler 5-12  Lexical Analyzer 13-16  Assignment 1 17-18  Assignment 2 19-20  Answer Keys 21  Explanations 21-23 #2. Parsing 24-99  Syntax Analysis 24  The Role of the Parser 24-25  Context-Free Grammars 25-29  Writing a Grammar 30-34  Top-Down Parsing 34-47  Bottom-Up Parsing 47-51  Operator-Precedence Parsing 52-63  LR Parsers 63-83  Parser Generators 83-90  Assignment 1 91-92  Assignment 2 93-95  Answer Keys 96  Explanations 96-99 #3. Syntax Directed Translation 100-143  Syntax Directed Definition 100-106  Construction of Syntax Trees 106-112  Bottom-Up Evaluation of S-Attributed Definitions 112-113  L-Atributed Definitions 114-118  Top-Down Translation 118-122  Bottom-Up Evaluation of Inherited Attributes 123-126  Run Time Environment 126-129  Assignment 1 130-132  Assignment 2 132-136  Answer Keys 137  Explanations 137-143
  • 5. Contents Compiler Design THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page ii #4. Intermediate Code Generation 144-177  Intermediate language 144-150  Code Generation 150  Issues in the Design of a Code Generator 151-153  Target Machine 154-156  Code Optimization 156-161  The Principal Sources of Optimization 161-166  Assignment 1 167-170  Assignment 2 170-171  Answer Keys 172  Explanations 172-177 Module Test 178-188  Test Questions 178-185  Answer Keys 186  Explanations 186-188 Reference Books 189
  • 6. Chapter 1 Compiler Design THE GATE ACADEMY PVT.LTD. H.O.: #74, KeshavaKrupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page 1 CHAPTER 1 Introduction to Compilers Compilers A compiler is a program that reads a program written in one language – the source language – and translates it into an equivalent program in another language – the target language (see Fig 1.1). As an important part of this translation process, the compiler reports to its user the presence of errors in the source program. Fig. 1.1 a compiler Compilers are sometimes classified as single pass multi-pass, load and go, debugging or optimising depending on how they have been constructed or on what function they are supposed to perform. Despite this apparent complexity, the basic tasks that any compiler must perform are essentially the same. By understanding these tasks, we can construct compilers for a variety of source languages and target machines using the same basic techniques. The Analysis-Synthesis Model of Compilation There are two parts of compilation: analysis and synthesis. The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program. The synthesis part constructs the desired target program from the intermediate representation. Out of the two parts, synthesis requires the most specialized techniques. During analysis, the operations implied by the source program are determined and recorded in a hierarchical structure called as tree. Often, a special kind of tree called as Syntax tree is used, in which each node represents an operation and the children of node represent the argument of the operation. For example, a syntax tree for an assignment statement is shown in Fig. 1.2 compiler source program target program error message s
  • 7. Chapter 1 Compiler Design THE GATE ACADEMY PVT.LTD. H.O.: #74, KeshavaKrupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page 2 Fig. 1.2 Syntax tree for position : = initial + rate 60. The Context of a Compiler In addition to a 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 sometimes entrusted to a distinct program, called as Preprocessor. The preprocessor may also expand shorthand’s, called macros, into source language statements. Following Fig. 1.3 shows a typical “compilation.” The target program created by the compiler may require further processing before it can be run. The compiler in Fig. 1.3 creates assembly code that is translated by an assembler into machine code and then linked together with some library routines into the code that actually runs on the machine. Fig.1.3 A language-processing system skeletal source program preprocessor source program compiler target assembly program assembler relocatable machine code loader/link editor absolute machine code library, relocatable object file position initial rate
  • 8. Chapter 1 Compiler Design THE GATE ACADEMY PVT.LTD. H.O.: #74, KeshavaKrupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page 3 Analysis of the Source Program 1. Linear or Lexical analysis, in which stream of characters making up the source program is read from left-to-right and grouped into tokens that are sequences of characters having a collective meaning. 2. Hierarchical or Syntax analysis, in which characters or tokens are grouped hierarchically into nested collections with collective meanings. 3. Semantic analysis, in which certain checks are performed to ensure that the components of a program fit together meaningfully. Lexical Analysis A token is a string of characters, categorized according to the rules as a symbol (e.g. IDENTIFIER, NUMBER, COMMA, etc.). The process of forming tokens from an input stream of characters is called tokenization and the lexer categorizes them according to symbol type. A token can look like anything that is useful for processing an input text stream or text file. A lexical analyzer generally does nothing with combinations of tokens, a task left for a parser. For example, a typical lexical analyzer recognizes parenthesis as tokens, but does nothing to ensure that each '(' is matched with a ')'. In a compiler, linear analysis is called lexical analysis or scanning. For example, in lexical analysis the characters in the assignment statement position : =initial + rate * 60 would be grouped into the following tokens: 1. The identifier position. 2. The assignment symbol : = 3. The identifier initial. 4. The plus sign 5. The identifier rate. 6. The multiplication sign 7. The number 60 The blanks separating the characters of these tokens would normally be eliminated during lexical analysis. Syntax Analysis Hierarchical analysis is called parsing or syntax analysis. It involves grouping the tokens of the source program into grammatical phrases that are used by the compiler to synthesize output. Usually, the grammatical phrases of the source program are represented by a parse tree such as the one shown in Fig. 1.4.
  • 9. Chapter 1 Compiler Design THE GATE ACADEMY PVT.LTD. H.O.: #74, KeshavaKrupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page 4 Fig.1.4 Parse tree for position: = initial + rate * 60 In the expression initial + rate * 60, the phrase rate * 60 is a logical unit because the usual conventions of arithmetic expressions tell us that multiplication is performed before addition. Because the expression initial + rate is followed by a *, it is not grouped into a single phrase by itself in Fig. 1.4. The hierarchical structure of a program is usually expressed by recursive rules. For example, we might have the following rules as part of the definition of expressions: 1. Any identifier is an expression. 2. Any number is an expression. 3. If expression1 and expression2 are expressions, then so are expression1 + expression 2 expression1 * expression 2 (expression1) Rules (1) and (2) are non-recursive basic rules, while (3) defines expressions in terms of operators applied to other expressions. Thus, by rule (1), initial and rate are expressions. By rule (2), 60 is an expression, while by rule (3), we can first infer that rate*60 is an expression and finally that initial + rate*60 is an expression. assignment statement identifier position expression expression identifier initial expression expression identifier rate expression number 60
  • 10. Chapter 1 Compiler Design THE GATE ACADEMY PVT.LTD. H.O.: #74, KeshavaKrupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page 5 Fig. 1.5 Semantic analysis inserts a conversion from integer to real. The parse tree in Fig.1.4 describes the syntactic structure of the input. A more common internal representation of this syntactic structure is given by the syntax in Fig. 1.5(a). A syntax tree is a compressed representation of the parse tree in which the operators appear as the interior nodes, and the operands of an operator are the children of the node for that operator. Semantic Analysis The semantic analysis phase checks the source program for semantic errors and gathers type information for the subsequent code-generation phase. It uses the hierarchical structure determined by the syntax-analysis phase to identify the operators and operands of expressions and statements. An important component of semantic analysis is type checking. Here the compiler checks that each operator has operands that are permitted by the source language specification. For example, many programming language definitions require a compiler to report an error every time a real number is used to index an array. However, the language specification may permit some operand corrections, for example, when binary arithmetic operator is applied to an integer and real. In this case, the compiler may need to convert the integer to a real. The Phases of a Compiler Conceptually, a compiler operates in phases, each of which transforms the source program from one representation to another. A typical decomposition of a compiler is shown in Fig. 1.6. In practice, some of the phases may be grouped together, and the intermediate representations between the grouped phases need not be explicitly constructed. position initial rate int to real position initial rate (a) (b)
  • 11. Chapter 1 Compiler Design THE GATE ACADEMY PVT.LTD. H.O.: #74, KeshavaKrupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11 : 080-65700750,  info@thegateacademy.com © Copyright reserved. Web: www.thegateacademy.com Page 6 Phases Fig. 1.6 Phases of a compiler The first three forming the bulk of the analysis portion of a compiler, were introduced in the last section. Two other activities, symbol-table management and error handling, are shown interacting with the six phases of compilation lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. Informally, we shall also call the symbol-table manager and the error handler “phases.” The 6 phases divided into 2 Groups 1. Front End: Depends on stream of tokens and parsetree 2. Back End: Dependent on Target, Independent of source code Symbol-Table Management A symbol table is a data structure containing a record for each identifier, with fields for the attributes of the identifier. The data structure allows us to find the record for each identifier quickly and to store or retrieve data from that record quickly. Symbol table is a Data Structure in a Compiler used for Managing information about variables & their attributes. Error Detection and Reporting Each phase can encounter errors. However, after detecting an error, a phase must somehow deal with that error, so that compilation can proceed, allowing further errors in the source program to be detected. A compiler that stops when it finds the first error is not as helpful as it could be. Source Program Lexical Analyzer Syntax Analyzer Semantic Analyzer Intermediate Code Generator Code Optimizer Target Code Generator Symbol Table Manager Error Handler Target Program