SlideShare a Scribd company logo
SYLLABUS
UNIT III SYNTAX DIRECTED TRANSLATION & INTERMEDIATE CODE
GENERATION
Syntax directed Definitions-Construction of Syntax Tree-Bottom-up Evaluation of S-Attribute
Definitions- Design of predictive translator - Type Systems-Specification of a simple type Checker
Equivalence of Type Expressions-Type Conversions. Intermediate Languages: Syntax Tree, Three
Address Code, Types and Declarations, Translation of Expressions, Type Checking, Back patching.
Course Outcomes
CO 3 Apply different parsing algorithms to develop the parsers for a given grammar
K3 LEVEL
INTERMEDIATE CODE GENERATION
The front end translates a source program into an intermediate representation from which the back end generates
target code.
Benefits of using a machine-independent intermediate form are:
1. Retargeting is facilitated. That is, a compiler for a different machine can be created by attaching a back end for
the new machine to an existing front end.
2. A machine-independent code optimizer can be applied to the intermediate representation.
Three ways of intermediate representation:
* Syntax tree
* Postfix notation
* Three address code
INTERMEDIATE LANGUAGES
INTERMEDIATE LANGUAGES
 A syntax tree depicts the natural hierarchical structure of a source program.
 A dag (Directed Acyclic Graph) gives the same information but in a more compact way because common
subexpressions are identified.
INTERMEDIATE LANGUAGES
 Two representations of the syntax tree are as
follows.
 In (a) each node is represented as a record with
a field for its operator and additional fields
for pointers to its children.
 In (b), nodes are allocated from an array of
records and the index or position of the node
serves as the pointer to the node. All the nodes
in the syntax tree can be visited by following
pointers, starting from the root at position 10.
• Postfix Notation:
• Linear representation of a Syntax Tree
• The corresponding operands appear after the operators
• a:=b*-c+b*-c
• a b c UMINUS *b c UMINUS * + :=
INTERMEDIATE LANGUAGES
Three-address code:
Three-address code is a sequence of statements of the general form x : = y op z
• where x, y and z are names, constants, or compiler-generated temporaries; op stands for any
operator, such as a fixed- or floating-point arithmetic operator, or a logical operator on boolean-
valued data.
• Thus a source language expression like x+ y*z might be translated into a sequence where t1 and t2
are compiler-generated temporary names.
INTERMEDIATE LANGUAGES
• Three-address code is a linearized representation of
a syntax tree or a dag in which explicit names
correspond to the interior nodes of the graph.
• The syntax tree and dag are represented by the three-
address code sequences.
• Variable names can appear directly in three address
statements.
INTERMEDIATE LANGUAGES
• The common three-address statements are:
1. Assignment statements of the form x : = y op z, where op is a binary arithmetic or logical operation.
2. Assignment instructions of the form x : = op y, where op is a unary operation.
3. Copy statements of the form x : = y where the value of y is assigned to x.
4. The unconditional jump goto L. The three-address statement with label L is the next to be executed.
5. Conditional jumps such as if x relop y goto L. This instruction applies a relational operator (<, =, >=,
etc. ) to x and y, and executes the statement with label L next if x stands in relation relop to y. If not, the
three-address statement following if x relop y as in the usual sequence.
6. param x and call p, n for procedure calls and return y, where y representing a returned value is optional.
7.Indexed assignments of the form x : = y[i] and x[i] : = y.
8.Address and pointer assignments of the form x : = &y , x : = *y, and *x : = y.
INTERMEDIATE LANGUAGES
Types of Three-Address Statements
Implementation of Three-Address Statements:
A three-address statement is an abstract form of intermediate code.
In a compiler, these statements can be implemented as records with fields for the operator and
the operands.
Three such representations are: Quadruples, Triples, Indirect triples
INTERMEDIATE LANGUAGES
Quadruples:
 A quadruple is a record structure with four fields, which are, op, arg1,
arg2 and result.
 The op field contains an internal code for the operator. The three-
address statement x : = y op z is represented by placing y in arg1, z in
arg2 and x in result.
 The contents of fields arg1, arg2 and result are normally pointers to the
symbol- entries for the names represented by these fields.
 If so, temporary names must be entered into the symbol table as they
are created.
INTERMEDIATE LANGUAGES
Triples:
• To avoid entering temporary names into the symbol table, we might refer to a
temporary value by the position of the statement that computes it.
• If we do so, three-address statements can be represented by records with only
three fields: op, arg1 and arg2.
• The fields arg1 and arg2, for the arguments of op, are either pointers to the
symbol table or pointers into the triple structure ( for temporary values ).
• Since three fields are used, this intermediate code format is known as triples.
INTERMEDIATE LANGUAGES
INTERMEDIATE LANGUAGES
Indirect Triples:
• Another implementation of three-address code is that of listing pointers to triples, rather than
listing the triples themselves. This implementation is called indirect triples.
INTERMEDIATE LANGUAGES
Syntax-Directed Translation into Three-Address Code
When three-address code is generated, temporary names
are made up for the interior nodes of a syntax tree.
• The synthesized attribute S.code represents the three-
address code for the assignment S.
• The nonterminal E has two attributes:
1. E.place, the name that will hold the value of E , and
2. E.code, the sequence of three-address statements
evaluating E.
DECLARATIONS
• As the sequence of declarations in a procedure or block is examined, we can lay out storage for names
local to the procedure.
• For each local name, we create a symbol-table entry with information like the type and the relative
address of the storage for the name.
• The relative address consists of an offset from the base of the static data area or the field for local data
in an activation record.
DECLARATIONS
• Declarations in a Procedure:
• In the translation scheme shown below:
 Nonterminal P generates a sequence of declarations of the form id : T.
Before the first declaration is considered, offset is set to 0.
As each new name is seen , that name is entered in the symbol table with offset equal to the current
value of offset, and offset is incremented by the width of the data object denoted by that name.
The procedure enter( name, type, offset )
creates a symbol-table entry for name, gives its type type and relative address offset in its data area.
DECLARATIONS
Attribute type represents a type expression constructed from the basic types integer and real by
applying the type constructors pointer and array. If type expressions are represented by graphs,
then attribute type might be a pointer to the node representing a type expression.
The width of an array is obtained by multiplying the width of each element by the number of
elements in the array. The width of each pointer is assumed to be 4.
DECLARATIONS
DECLARATIONS
Keeping Track of Scope Information:
• When a nested procedure is seen, processing of declarations in the enclosing procedure is
temporarily suspended.
• This approach will be illustrated by adding semantic rules to the following language:
P->D
D->D; D |id: T | proc id; D ; S
• One possible implementation of a symbol table is a linked list of entries for names.
• A new symbol table is created when a procedure declaration D  proc id D1;S is seen, and entries
for the declarations in D1 are created in the new table.
• The new table points back to the symbol table of the enclosing procedure; the name represented by
id itself is local to the enclosing procedure.
• The only change from the treatment of variable declarations is that the procedure enter is told which
symbol table to make an entry in.
DECLARATIONS
The semantic rules are defined in terms of the following operations:
1. mktable(previous) creates a new symbol table and returns a pointer to the new table. The argument previous
points to a previously created symbol table, presumably that for the enclosing procedure.
2. enter(table, name, type, offset) creates a new entry for name name in the symbol table pointed to by table. Again,
enter places type type and relative address offset in fields within the entry.
3. addwidth(table, width) records the cumulative width of all the entries in table in the header associated with this
symbol table.
4. enterproc(table, name, newtable) creates a new entry for procedure name in the symbol table pointed to by table.
The argument newtable points to the symbol table for this procedure name.
DECLARATIONS
Syntax directed translation scheme for nested procedures
P->M D { addwidth ( top( tblptr) , top (offset));
pop (tblptr); pop (offset) }
 M->ɛ { t : = mktable (nil);
push (t,tblptr); push (0,offset) }
D->D1 ; D2
D->proc id ; N D1; S { t : = top (tblptr);
addwidth ( t, top(offset));
pop (tblptr); pop (offset);
enterproc (top (tblptr), id.name, t) }
 D->id : T { enter (top (tblptr), id.name, T.type, top (offset));
top (offset) := top (offset) + T.width }
 N->ɛ { t := mktable (top (tblptr));
push (t, tblptr); push (0,offset) }
Ad

More Related Content

Similar to Syntax directed definition and intermediate code generation (20)

Compiler chapter six .ppt course material
Compiler chapter six .ppt course materialCompiler chapter six .ppt course material
Compiler chapter six .ppt course material
gadisaAdamu
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
Vipul Naik
 
Unit 3 Compiler Design Regulation 2021.pptx
Unit 3 Compiler Design Regulation 2021.pptxUnit 3 Compiler Design Regulation 2021.pptx
Unit 3 Compiler Design Regulation 2021.pptx
jeevitha404389
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
Akshaya Arunan
 
Lecture 21 22
Lecture 21 22Lecture 21 22
Lecture 21 22
Najmul Hassan
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
Shine Raj
 
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Saikrishna Tanguturu
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
RamchandraRegmi
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
Shashwat Shriparv
 
Chapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdfChapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdf
RAnwarpasha
 
Declarations
DeclarationsDeclarations
Declarations
sangeetha r
 
458237.-Compiler-Design-Intermediate-code-generation.ppt
458237.-Compiler-Design-Intermediate-code-generation.ppt458237.-Compiler-Design-Intermediate-code-generation.ppt
458237.-Compiler-Design-Intermediate-code-generation.ppt
PalaniSamyB3
 
Intermediate code representations
Intermediate code representationsIntermediate code representations
Intermediate code representations
ahmed51236
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
Iffat Anjum
 
In the Notes on Programming Language Syntax page, an example par.docx
In the Notes on Programming Language Syntax page, an example par.docxIn the Notes on Programming Language Syntax page, an example par.docx
In the Notes on Programming Language Syntax page, an example par.docx
mecklenburgstrelitzh
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
Dushmanta Nath
 
Module 3 - Semantic Analysis(Compiler Design).pptx
Module 3 - Semantic Analysis(Compiler Design).pptxModule 3 - Semantic Analysis(Compiler Design).pptx
Module 3 - Semantic Analysis(Compiler Design).pptx
muralidharanm2022
 
datypes , operators in c,variables in clanguage formatting input and out put
datypes , operators in c,variables in clanguage formatting input  and out putdatypes , operators in c,variables in clanguage formatting input  and out put
datypes , operators in c,variables in clanguage formatting input and out put
MdAmreen
 
Embedded C The IoT Academy
Embedded C The IoT AcademyEmbedded C The IoT Academy
Embedded C The IoT Academy
The IOT Academy
 
Chapter 6 - Intermediate Languages.pptxjfjgj
Chapter 6 - Intermediate Languages.pptxjfjgjChapter 6 - Intermediate Languages.pptxjfjgj
Chapter 6 - Intermediate Languages.pptxjfjgj
Shemse Shukre
 
Compiler chapter six .ppt course material
Compiler chapter six .ppt course materialCompiler chapter six .ppt course material
Compiler chapter six .ppt course material
gadisaAdamu
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
Vipul Naik
 
Unit 3 Compiler Design Regulation 2021.pptx
Unit 3 Compiler Design Regulation 2021.pptxUnit 3 Compiler Design Regulation 2021.pptx
Unit 3 Compiler Design Regulation 2021.pptx
jeevitha404389
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
Akshaya Arunan
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
Shine Raj
 
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Compiler Design - Ambiguous grammar, LMD & RMD, Infix & Postfix, Implementati...
Saikrishna Tanguturu
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
RamchandraRegmi
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
Shashwat Shriparv
 
Chapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdfChapter 11 - Intermediate Code Generation.pdf
Chapter 11 - Intermediate Code Generation.pdf
RAnwarpasha
 
458237.-Compiler-Design-Intermediate-code-generation.ppt
458237.-Compiler-Design-Intermediate-code-generation.ppt458237.-Compiler-Design-Intermediate-code-generation.ppt
458237.-Compiler-Design-Intermediate-code-generation.ppt
PalaniSamyB3
 
Intermediate code representations
Intermediate code representationsIntermediate code representations
Intermediate code representations
ahmed51236
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
Iffat Anjum
 
In the Notes on Programming Language Syntax page, an example par.docx
In the Notes on Programming Language Syntax page, an example par.docxIn the Notes on Programming Language Syntax page, an example par.docx
In the Notes on Programming Language Syntax page, an example par.docx
mecklenburgstrelitzh
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
Dushmanta Nath
 
Module 3 - Semantic Analysis(Compiler Design).pptx
Module 3 - Semantic Analysis(Compiler Design).pptxModule 3 - Semantic Analysis(Compiler Design).pptx
Module 3 - Semantic Analysis(Compiler Design).pptx
muralidharanm2022
 
datypes , operators in c,variables in clanguage formatting input and out put
datypes , operators in c,variables in clanguage formatting input  and out putdatypes , operators in c,variables in clanguage formatting input  and out put
datypes , operators in c,variables in clanguage formatting input and out put
MdAmreen
 
Embedded C The IoT Academy
Embedded C The IoT AcademyEmbedded C The IoT Academy
Embedded C The IoT Academy
The IOT Academy
 
Chapter 6 - Intermediate Languages.pptxjfjgj
Chapter 6 - Intermediate Languages.pptxjfjgjChapter 6 - Intermediate Languages.pptxjfjgj
Chapter 6 - Intermediate Languages.pptxjfjgj
Shemse Shukre
 

Recently uploaded (20)

Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Journal of Soft Computing in Civil Engineering
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
ELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdfELectronics Boards & Product Testing_Shiju.pdf
ELectronics Boards & Product Testing_Shiju.pdf
Shiju Jacob
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E..."Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
"Boiler Feed Pump (BFP): Working, Applications, Advantages, and Limitations E...
Infopitaara
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design ThinkingDT REPORT by Tech titan GROUP to introduce the subject design Thinking
DT REPORT by Tech titan GROUP to introduce the subject design Thinking
DhruvChotaliya2
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Value Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous SecurityValue Stream Mapping Worskshops for Intelligent Continuous Security
Value Stream Mapping Worskshops for Intelligent Continuous Security
Marc Hornbeek
 
Ad

Syntax directed definition and intermediate code generation

  • 1. SYLLABUS UNIT III SYNTAX DIRECTED TRANSLATION & INTERMEDIATE CODE GENERATION Syntax directed Definitions-Construction of Syntax Tree-Bottom-up Evaluation of S-Attribute Definitions- Design of predictive translator - Type Systems-Specification of a simple type Checker Equivalence of Type Expressions-Type Conversions. Intermediate Languages: Syntax Tree, Three Address Code, Types and Declarations, Translation of Expressions, Type Checking, Back patching.
  • 2. Course Outcomes CO 3 Apply different parsing algorithms to develop the parsers for a given grammar K3 LEVEL
  • 3. INTERMEDIATE CODE GENERATION The front end translates a source program into an intermediate representation from which the back end generates target code. Benefits of using a machine-independent intermediate form are: 1. Retargeting is facilitated. That is, a compiler for a different machine can be created by attaching a back end for the new machine to an existing front end. 2. A machine-independent code optimizer can be applied to the intermediate representation.
  • 4. Three ways of intermediate representation: * Syntax tree * Postfix notation * Three address code INTERMEDIATE LANGUAGES
  • 5. INTERMEDIATE LANGUAGES  A syntax tree depicts the natural hierarchical structure of a source program.  A dag (Directed Acyclic Graph) gives the same information but in a more compact way because common subexpressions are identified.
  • 6. INTERMEDIATE LANGUAGES  Two representations of the syntax tree are as follows.  In (a) each node is represented as a record with a field for its operator and additional fields for pointers to its children.  In (b), nodes are allocated from an array of records and the index or position of the node serves as the pointer to the node. All the nodes in the syntax tree can be visited by following pointers, starting from the root at position 10.
  • 7. • Postfix Notation: • Linear representation of a Syntax Tree • The corresponding operands appear after the operators • a:=b*-c+b*-c • a b c UMINUS *b c UMINUS * + := INTERMEDIATE LANGUAGES
  • 8. Three-address code: Three-address code is a sequence of statements of the general form x : = y op z • where x, y and z are names, constants, or compiler-generated temporaries; op stands for any operator, such as a fixed- or floating-point arithmetic operator, or a logical operator on boolean- valued data. • Thus a source language expression like x+ y*z might be translated into a sequence where t1 and t2 are compiler-generated temporary names. INTERMEDIATE LANGUAGES
  • 9. • Three-address code is a linearized representation of a syntax tree or a dag in which explicit names correspond to the interior nodes of the graph. • The syntax tree and dag are represented by the three- address code sequences. • Variable names can appear directly in three address statements. INTERMEDIATE LANGUAGES
  • 10. • The common three-address statements are: 1. Assignment statements of the form x : = y op z, where op is a binary arithmetic or logical operation. 2. Assignment instructions of the form x : = op y, where op is a unary operation. 3. Copy statements of the form x : = y where the value of y is assigned to x. 4. The unconditional jump goto L. The three-address statement with label L is the next to be executed. 5. Conditional jumps such as if x relop y goto L. This instruction applies a relational operator (<, =, >=, etc. ) to x and y, and executes the statement with label L next if x stands in relation relop to y. If not, the three-address statement following if x relop y as in the usual sequence. 6. param x and call p, n for procedure calls and return y, where y representing a returned value is optional. 7.Indexed assignments of the form x : = y[i] and x[i] : = y. 8.Address and pointer assignments of the form x : = &y , x : = *y, and *x : = y. INTERMEDIATE LANGUAGES Types of Three-Address Statements
  • 11. Implementation of Three-Address Statements: A three-address statement is an abstract form of intermediate code. In a compiler, these statements can be implemented as records with fields for the operator and the operands. Three such representations are: Quadruples, Triples, Indirect triples INTERMEDIATE LANGUAGES
  • 12. Quadruples:  A quadruple is a record structure with four fields, which are, op, arg1, arg2 and result.  The op field contains an internal code for the operator. The three- address statement x : = y op z is represented by placing y in arg1, z in arg2 and x in result.  The contents of fields arg1, arg2 and result are normally pointers to the symbol- entries for the names represented by these fields.  If so, temporary names must be entered into the symbol table as they are created. INTERMEDIATE LANGUAGES
  • 13. Triples: • To avoid entering temporary names into the symbol table, we might refer to a temporary value by the position of the statement that computes it. • If we do so, three-address statements can be represented by records with only three fields: op, arg1 and arg2. • The fields arg1 and arg2, for the arguments of op, are either pointers to the symbol table or pointers into the triple structure ( for temporary values ). • Since three fields are used, this intermediate code format is known as triples. INTERMEDIATE LANGUAGES
  • 14. INTERMEDIATE LANGUAGES Indirect Triples: • Another implementation of three-address code is that of listing pointers to triples, rather than listing the triples themselves. This implementation is called indirect triples.
  • 15. INTERMEDIATE LANGUAGES Syntax-Directed Translation into Three-Address Code When three-address code is generated, temporary names are made up for the interior nodes of a syntax tree. • The synthesized attribute S.code represents the three- address code for the assignment S. • The nonterminal E has two attributes: 1. E.place, the name that will hold the value of E , and 2. E.code, the sequence of three-address statements evaluating E.
  • 16. DECLARATIONS • As the sequence of declarations in a procedure or block is examined, we can lay out storage for names local to the procedure. • For each local name, we create a symbol-table entry with information like the type and the relative address of the storage for the name. • The relative address consists of an offset from the base of the static data area or the field for local data in an activation record.
  • 17. DECLARATIONS • Declarations in a Procedure: • In the translation scheme shown below:  Nonterminal P generates a sequence of declarations of the form id : T. Before the first declaration is considered, offset is set to 0. As each new name is seen , that name is entered in the symbol table with offset equal to the current value of offset, and offset is incremented by the width of the data object denoted by that name. The procedure enter( name, type, offset ) creates a symbol-table entry for name, gives its type type and relative address offset in its data area.
  • 18. DECLARATIONS Attribute type represents a type expression constructed from the basic types integer and real by applying the type constructors pointer and array. If type expressions are represented by graphs, then attribute type might be a pointer to the node representing a type expression. The width of an array is obtained by multiplying the width of each element by the number of elements in the array. The width of each pointer is assumed to be 4.
  • 20. DECLARATIONS Keeping Track of Scope Information: • When a nested procedure is seen, processing of declarations in the enclosing procedure is temporarily suspended. • This approach will be illustrated by adding semantic rules to the following language: P->D D->D; D |id: T | proc id; D ; S • One possible implementation of a symbol table is a linked list of entries for names. • A new symbol table is created when a procedure declaration D  proc id D1;S is seen, and entries for the declarations in D1 are created in the new table. • The new table points back to the symbol table of the enclosing procedure; the name represented by id itself is local to the enclosing procedure. • The only change from the treatment of variable declarations is that the procedure enter is told which symbol table to make an entry in.
  • 21. DECLARATIONS The semantic rules are defined in terms of the following operations: 1. mktable(previous) creates a new symbol table and returns a pointer to the new table. The argument previous points to a previously created symbol table, presumably that for the enclosing procedure. 2. enter(table, name, type, offset) creates a new entry for name name in the symbol table pointed to by table. Again, enter places type type and relative address offset in fields within the entry. 3. addwidth(table, width) records the cumulative width of all the entries in table in the header associated with this symbol table. 4. enterproc(table, name, newtable) creates a new entry for procedure name in the symbol table pointed to by table. The argument newtable points to the symbol table for this procedure name.
  • 22. DECLARATIONS Syntax directed translation scheme for nested procedures P->M D { addwidth ( top( tblptr) , top (offset)); pop (tblptr); pop (offset) }  M->ɛ { t : = mktable (nil); push (t,tblptr); push (0,offset) } D->D1 ; D2 D->proc id ; N D1; S { t : = top (tblptr); addwidth ( t, top(offset)); pop (tblptr); pop (offset); enterproc (top (tblptr), id.name, t) }  D->id : T { enter (top (tblptr), id.name, T.type, top (offset)); top (offset) := top (offset) + T.width }  N->ɛ { t := mktable (top (tblptr)); push (t, tblptr); push (0,offset) }