4
Most read
Compiler Construction Sunita M. Dol, CSE Dept
Page 1
Assignment No. 1
AIM:
Introduction to Compiler.
THEORY:
A compiler is a program takes a program written in a source language and
translates it into an equivalent program in a target language.
There are two parts of compilation
• Analysis part: breaks up the source program into constituents pieces and
creates the intermediate representation of the source program.
– Lexical Analyzer, Syntax Analyzer and Semantic Analyzer are the parts
of this phase.
• Synthesis part: constructs the desired target program from the intermediate
representation
– Intermediate Code Generator, Code Generator, and Code Optimizer are
the parts of this phase.
Language Processing System
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 a preprocessor. The preprocessor may also expand
shorthands, called macros, into source language statements.
The target program created by the compiler may require further processing before
it can be run. The compiler 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.
Compiler Construction Sunita M. Dol, CSE Dept
Page 2
Figure: Language Processing System
Phases of Compiler:
Conceptually, a compiler operates in phases, each of which transforms the source
program from one representation to another.
The first three phases form the bulk of the analysis portion of a compiler. Two
other activities, symbol-table management and error handling, are shown
interacting with the six phases of 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."
Compiler Construction Sunita M. Dol, CSE Dept
Page 3
Figure: Phases of Compiler
Linear analysis: The 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
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.
Compiler Construction Sunita M. Dol, CSE Dept
Page 4
Hierarchical Analysis: characters or tokens are grouped hierarchically into nested
collection with collective meaning. Hierarchical analysis is called parsing or syntax
analysis 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
Figure: Parse Tree
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.
Figure: Syntax Tree
Compiler Construction Sunita M. Dol, CSE Dept
Page 5
Semantic analysis: Certain checks are performed to ensure that the components
of a program fit together meaningfully. A semantic analyzer checks the source
program for semantic errors and collects the type information for the code
generation. Type-checking is an important part of semantic analyzer. Normally
semantic information cannot be represented by a context-free language used in
syntax analyzers. Context-free grammars used in the syntax analysis are integrated
with attributes (semantic rules). The result is a syntax-directed translation and
Attribute grammars.
Figure: Semantic analysis inserts conversion from integer to real.
Intermediate Code Generation: A compiler may produce an explicit intermediate
codes representing the source program. These intermediate codes are generally
machine code (architecture independent). But the level of intermediate codes is
close to the level of machine codes.
Compiler Construction Sunita M. Dol, CSE Dept
Page 6
Code Optimizer (for Intermediate Code Generator): The code optimizer optimizes
the code produced by the intermediate code generator in the terms of time and
space.
Code Generator: It pproduces the target language in a specific architecture. The
target program is normally is a relocatable object file containing the machine
codes.
Symbol Table: An essential function of a compiler is to record the identifiers used
in the source program and collect information about various attributes of each
identifier. These attributes may provide information about the storage allocated
for an identifier, its type, its scope (where in the program it is valid) and, in the
case of procedure names, such things as the number and types of its arguments, the
method of passing each argument (e.g., by reference), and the type returned, if any.
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
Compiler Construction Sunita M. Dol, CSE Dept
Page 7
record for each identifier quickly and to store or retrieve data from that record
quickly.
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. The syntax and semantic analysis phases
usually handle a large fraction of the errors detectable by the compiler.
The lexical phase can detect errors where the characters remaining in the input do
not form any token of the language. Errors where the token stream violates the
structure rules (syntax) of the language are determined by the syntax analysis
phase. During semantic analysis the compiler tries to detect constructs that have
the right syntactic structure but no meaning to the operation involved, e. g., if we
try to add two identifiers, one of which is the name of an array, and the other the
name of a procedure.
Compiler Construction Sunita M. Dol, CSE Dept
Page 8
Compiler Construction Sunita M. Dol, CSE Dept
Page 9
Compiler Construction Tools
Some commonly used compiler-construction tools include
 Parser generators: that automatically produces syntax analyzers from a
grammatical description of a programming language.
 Scanner generators: that produces lexical analyzers from a regular-
expression description of the tokens of a language.
 Syntax-directed translation engines: that produces collections of routines
for walking a parse tree and generating intermediate code.
 Code-generator generators: that produces a code generator from a
collection of rules for translating each operation of the intermediate
language into the machine language for a target machine.
 Data-flow analysis engines: that facilitate the gathering of information
about how values are transmitted from one part of a program to each other
part. Data-flow analysis is a key part of code optimization.
CONCLUSION:
A compiler is a program takes a program written in a source language and
translates it into an equivalent program in a target language. There are two parts of
compilation
• Analysis part.
– Lexical Analyzer, Syntax Analyzer and Semantic Analyzer are the parts
of this phase.
• Synthesis part.
– Intermediate Code Generator, Code Generator, and Code Optimizer are
the parts of this phase.
REFERENCES:
 Compilers - Principles, Techniques and Tools - A.V. Aho, R. Shethi and J. D.
Ullman (Pearson Education)

More Related Content

PDF
Handout#10
PDF
Assignment11
PDF
Handout#07
PDF
Handout#05
PDF
Handout#04
PDF
Handout#03
PDF
Handout#08
PDF
Handout#02
Handout#10
Assignment11
Handout#07
Handout#05
Handout#04
Handout#03
Handout#08
Handout#02

What's hot (20)

PDF
Assignment4
PDF
Handout#06
PDF
Handout#09
PDF
Handout#11
PDF
Handout#12
PDF
Handout#01
PPTX
Lexical analysis-using-lex
PPTX
PPT
Lexical Analysis
PPTX
Compiler Engineering Lab#1
DOCX
Compiler design important questions
PPT
The smartpath information systems c pro
PPTX
Intermediate code- generation
PPTX
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
PDF
C programming notes
DOC
PPTX
Structure of the compiler
PPT
Unit 4 Foc
DOC
C notes for exam preparation
Assignment4
Handout#06
Handout#09
Handout#11
Handout#12
Handout#01
Lexical analysis-using-lex
Lexical Analysis
Compiler Engineering Lab#1
Compiler design important questions
The smartpath information systems c pro
Intermediate code- generation
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
C programming notes
Structure of the compiler
Unit 4 Foc
C notes for exam preparation
Ad

Similar to Assignment1 (20)

DOCX
Compiler Design Material
PDF
SSD Mod 2 -18CS61_Notes.pdf
PPT
A basic introduction to compiler design.ppt
PPT
A basic introduction to compiler design.ppt
PDF
Compilers Principles, Practice & Tools Compilers
DOCX
Dineshmaterial1 091225091539-phpapp02
PDF
unit1pdf__2021_12_14_12_37_34.pdf
PPTX
Unit2_CD.pptx more about compilation of the day
PDF
Chapter1pdf__2021_11_23_10_53_20.pdf
PDF
Lecture 2.1 - Phase of a Commmmpiler.pdf
PPT
Compiler Construction introduction
PDF
PDF
Chapter#01 cc
PPTX
Ch 1.pptx
PPTX
phase of compiler
PPTX
Chapter-1.pptx compiler Design Course Material
PPTX
role of lexical anaysis
PDF
Phases of Compiler
PDF
3_1_COMPILER_DESIGNGARGREREGREGREGREGREGRGRERE
PDF
design intoduction of_COMPILER_DESIGN.pdf
Compiler Design Material
SSD Mod 2 -18CS61_Notes.pdf
A basic introduction to compiler design.ppt
A basic introduction to compiler design.ppt
Compilers Principles, Practice & Tools Compilers
Dineshmaterial1 091225091539-phpapp02
unit1pdf__2021_12_14_12_37_34.pdf
Unit2_CD.pptx more about compilation of the day
Chapter1pdf__2021_11_23_10_53_20.pdf
Lecture 2.1 - Phase of a Commmmpiler.pdf
Compiler Construction introduction
Chapter#01 cc
Ch 1.pptx
phase of compiler
Chapter-1.pptx compiler Design Course Material
role of lexical anaysis
Phases of Compiler
3_1_COMPILER_DESIGNGARGREREGREGREGREGREGRGRERE
design intoduction of_COMPILER_DESIGN.pdf
Ad

More from Sunita Milind Dol (20)

PDF
Assignment No. 10 on Unit-IV Set Theory, Relations and Function
PDF
Assignment No. 9 on Unit-IV Set Theory, Relations and Function
PDF
Assignment No. 8 on Unit-IV Set Theory, Relations and Function
PDF
Assignment No. 7 on Unit-IV - Set Theory, Relations and Function
PDF
Assignment No. 6 on Representation of Expression
PDF
Assignment No. 5 on Unit-III Representation of Expression
PDF
Assignment No. 4 on Unit-II Mathematical Logic
PDF
Assignment No. 3 on Unit-II Mathematical Logic
PDF
Assignment No. 2 on Unit-I Mathematical Induction
PDF
Assignment No. 1 on Unit-I Fundamental Principles of Counting
PDF
Unit Number 5 - Research Ethics, IPR and Publishing
PDF
Unit Number 4 - Research reports and Thesis writing
PDF
Unit Number 3 - Data collection and Statistical Analysis
PDF
Unit Number 2 - Research Problem Formulation and Methods
PDF
Unit Number 1 - Introduction to Research
PDF
Unit Number 5 - Research Ethics, IPR and Publishing
PDF
Unit Number 5 - Research reports and Thesis writing
PDF
Unit Number 3 - Data collection and Statistical Analysis
PDF
Unit Number 2 - Research Problem Formulation and Methods
PDF
Unit Number 1 : Introduction to Research
Assignment No. 10 on Unit-IV Set Theory, Relations and Function
Assignment No. 9 on Unit-IV Set Theory, Relations and Function
Assignment No. 8 on Unit-IV Set Theory, Relations and Function
Assignment No. 7 on Unit-IV - Set Theory, Relations and Function
Assignment No. 6 on Representation of Expression
Assignment No. 5 on Unit-III Representation of Expression
Assignment No. 4 on Unit-II Mathematical Logic
Assignment No. 3 on Unit-II Mathematical Logic
Assignment No. 2 on Unit-I Mathematical Induction
Assignment No. 1 on Unit-I Fundamental Principles of Counting
Unit Number 5 - Research Ethics, IPR and Publishing
Unit Number 4 - Research reports and Thesis writing
Unit Number 3 - Data collection and Statistical Analysis
Unit Number 2 - Research Problem Formulation and Methods
Unit Number 1 - Introduction to Research
Unit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research reports and Thesis writing
Unit Number 3 - Data collection and Statistical Analysis
Unit Number 2 - Research Problem Formulation and Methods
Unit Number 1 : Introduction to Research

Recently uploaded (20)

PPT
Chapter 1 - Introduction to Manufacturing Technology_2.ppt
PPTX
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PDF
20250617 - IR - Global Guide for HR - 51 pages.pdf
PPTX
Chapter 2 -Technology and Enginerring Materials + Composites.pptx
PDF
August 2025 - Top 10 Read Articles in Network Security & Its Applications
PPTX
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
PDF
Soil Improvement Techniques Note - Rabbi
PDF
First part_B-Image Processing - 1 of 2).pdf
PPTX
Software Engineering and software moduleing
PPTX
Amdahl’s law is explained in the above power point presentations
PPTX
mechattonicsand iotwith sensor and actuator
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PDF
Design of Material Handling Equipment Lecture Note
PDF
Introduction to Power System StabilityPS
PDF
Computer System Architecture 3rd Edition-M Morris Mano.pdf
DOC
T Pandian CV Madurai pandi kokkaf illaya
PDF
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
PDF
Applications of Equal_Area_Criterion.pdf
Chapter 1 - Introduction to Manufacturing Technology_2.ppt
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
Exploratory_Data_Analysis_Fundamentals.pdf
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
20250617 - IR - Global Guide for HR - 51 pages.pdf
Chapter 2 -Technology and Enginerring Materials + Composites.pptx
August 2025 - Top 10 Read Articles in Network Security & Its Applications
Chemical Technological Processes, Feasibility Study and Chemical Process Indu...
Soil Improvement Techniques Note - Rabbi
First part_B-Image Processing - 1 of 2).pdf
Software Engineering and software moduleing
Amdahl’s law is explained in the above power point presentations
mechattonicsand iotwith sensor and actuator
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
Design of Material Handling Equipment Lecture Note
Introduction to Power System StabilityPS
Computer System Architecture 3rd Edition-M Morris Mano.pdf
T Pandian CV Madurai pandi kokkaf illaya
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
Applications of Equal_Area_Criterion.pdf

Assignment1

  • 1. Compiler Construction Sunita M. Dol, CSE Dept Page 1 Assignment No. 1 AIM: Introduction to Compiler. THEORY: A compiler is a program takes a program written in a source language and translates it into an equivalent program in a target language. There are two parts of compilation • Analysis part: breaks up the source program into constituents pieces and creates the intermediate representation of the source program. – Lexical Analyzer, Syntax Analyzer and Semantic Analyzer are the parts of this phase. • Synthesis part: constructs the desired target program from the intermediate representation – Intermediate Code Generator, Code Generator, and Code Optimizer are the parts of this phase. Language Processing System 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 a preprocessor. The preprocessor may also expand shorthands, called macros, into source language statements. The target program created by the compiler may require further processing before it can be run. The compiler 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.
  • 2. Compiler Construction Sunita M. Dol, CSE Dept Page 2 Figure: Language Processing System Phases of Compiler: Conceptually, a compiler operates in phases, each of which transforms the source program from one representation to another. The first three phases form the bulk of the analysis portion of a compiler. Two other activities, symbol-table management and error handling, are shown interacting with the six phases of 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."
  • 3. Compiler Construction Sunita M. Dol, CSE Dept Page 3 Figure: Phases of Compiler Linear analysis: The 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 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.
  • 4. Compiler Construction Sunita M. Dol, CSE Dept Page 4 Hierarchical Analysis: characters or tokens are grouped hierarchically into nested collection with collective meaning. Hierarchical analysis is called parsing or syntax analysis 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 Figure: Parse Tree 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. Figure: Syntax Tree
  • 5. Compiler Construction Sunita M. Dol, CSE Dept Page 5 Semantic analysis: Certain checks are performed to ensure that the components of a program fit together meaningfully. A semantic analyzer checks the source program for semantic errors and collects the type information for the code generation. Type-checking is an important part of semantic analyzer. Normally semantic information cannot be represented by a context-free language used in syntax analyzers. Context-free grammars used in the syntax analysis are integrated with attributes (semantic rules). The result is a syntax-directed translation and Attribute grammars. Figure: Semantic analysis inserts conversion from integer to real. Intermediate Code Generation: A compiler may produce an explicit intermediate codes representing the source program. These intermediate codes are generally machine code (architecture independent). But the level of intermediate codes is close to the level of machine codes.
  • 6. Compiler Construction Sunita M. Dol, CSE Dept Page 6 Code Optimizer (for Intermediate Code Generator): The code optimizer optimizes the code produced by the intermediate code generator in the terms of time and space. Code Generator: It pproduces the target language in a specific architecture. The target program is normally is a relocatable object file containing the machine codes. Symbol Table: An essential function of a compiler is to record the identifiers used in the source program and collect information about various attributes of each identifier. These attributes may provide information about the storage allocated for an identifier, its type, its scope (where in the program it is valid) and, in the case of procedure names, such things as the number and types of its arguments, the method of passing each argument (e.g., by reference), and the type returned, if any. 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
  • 7. Compiler Construction Sunita M. Dol, CSE Dept Page 7 record for each identifier quickly and to store or retrieve data from that record quickly. 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. The syntax and semantic analysis phases usually handle a large fraction of the errors detectable by the compiler. The lexical phase can detect errors where the characters remaining in the input do not form any token of the language. Errors where the token stream violates the structure rules (syntax) of the language are determined by the syntax analysis phase. During semantic analysis the compiler tries to detect constructs that have the right syntactic structure but no meaning to the operation involved, e. g., if we try to add two identifiers, one of which is the name of an array, and the other the name of a procedure.
  • 8. Compiler Construction Sunita M. Dol, CSE Dept Page 8
  • 9. Compiler Construction Sunita M. Dol, CSE Dept Page 9 Compiler Construction Tools Some commonly used compiler-construction tools include  Parser generators: that automatically produces syntax analyzers from a grammatical description of a programming language.  Scanner generators: that produces lexical analyzers from a regular- expression description of the tokens of a language.  Syntax-directed translation engines: that produces collections of routines for walking a parse tree and generating intermediate code.  Code-generator generators: that produces a code generator from a collection of rules for translating each operation of the intermediate language into the machine language for a target machine.  Data-flow analysis engines: that facilitate the gathering of information about how values are transmitted from one part of a program to each other part. Data-flow analysis is a key part of code optimization. CONCLUSION: A compiler is a program takes a program written in a source language and translates it into an equivalent program in a target language. There are two parts of compilation • Analysis part. – Lexical Analyzer, Syntax Analyzer and Semantic Analyzer are the parts of this phase. • Synthesis part. – Intermediate Code Generator, Code Generator, and Code Optimizer are the parts of this phase. REFERENCES:  Compilers - Principles, Techniques and Tools - A.V. Aho, R. Shethi and J. D. Ullman (Pearson Education)