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) }

More Related Content

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

PPTX
Compiler Design_Intermediate code generation new ppt.pptx
RushaliDeshmukh2
 
PPTX
Automata compiler design ppt for btech students
keerthanatalluri1404
 
PPTX
Lecture 12 intermediate code generation
Iffat Anjum
 
PDF
INTERMEDIATE CODE GENERTION-CD UNIT-3.pdf
Ranjeet Reddy
 
PPTX
Unit 3 Compiler Design Regulation 2021.pptx
jeevitha404389
 
PPT
Intermediate code generation
RamchandraRegmi
 
PPTX
Chapter 6 - Intermediate Languages.pptxjfjgj
Shemse Shukre
 
PDF
Intermediate code generation
Akshaya Arunan
 
PDF
Assignment12
Sunita Milind Dol
 
PPT
Compiler chapter six .ppt course material
gadisaAdamu
 
PPTX
Three address code In Compiler Design
Shine Raj
 
PPT
Chapter 6 intermediate code generation
Vipul Naik
 
PPTX
Intermediate code
Vishal Agarwal
 
PPTX
complier design unit 4 for helping students
aniketsugandhi1
 
PDF
Project presentation PPT.pdf this is help for student who doing this complier...
AmitSingh395981
 
PPT
Lecture 21 22
Najmul Hassan
 
PPT
Chapter 6 Intermediate Code Generation
Radhakrishnan Chinnusamy
 
PPTX
Assignment statements
Divya Devan
 
PPTX
Intermediate code- generation
rawan_z
 
PPT
Chapter Eight(1)
bolovv
 
Compiler Design_Intermediate code generation new ppt.pptx
RushaliDeshmukh2
 
Automata compiler design ppt for btech students
keerthanatalluri1404
 
Lecture 12 intermediate code generation
Iffat Anjum
 
INTERMEDIATE CODE GENERTION-CD UNIT-3.pdf
Ranjeet Reddy
 
Unit 3 Compiler Design Regulation 2021.pptx
jeevitha404389
 
Intermediate code generation
RamchandraRegmi
 
Chapter 6 - Intermediate Languages.pptxjfjgj
Shemse Shukre
 
Intermediate code generation
Akshaya Arunan
 
Assignment12
Sunita Milind Dol
 
Compiler chapter six .ppt course material
gadisaAdamu
 
Three address code In Compiler Design
Shine Raj
 
Chapter 6 intermediate code generation
Vipul Naik
 
Intermediate code
Vishal Agarwal
 
complier design unit 4 for helping students
aniketsugandhi1
 
Project presentation PPT.pdf this is help for student who doing this complier...
AmitSingh395981
 
Lecture 21 22
Najmul Hassan
 
Chapter 6 Intermediate Code Generation
Radhakrishnan Chinnusamy
 
Assignment statements
Divya Devan
 
Intermediate code- generation
rawan_z
 
Chapter Eight(1)
bolovv
 

Recently uploaded (20)

PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PPTX
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
PDF
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
PDF
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PPTX
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPT
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
PDF
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PPT
Testing and final inspection of a solar PV system
MuhammadSanni2
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
Digital water marking system project report
Kamal Acharya
 
PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PPTX
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
Water Resources Engineering (CVE 728)--Slide 4.pptx
mohammedado3
 
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Testing and final inspection of a solar PV system
MuhammadSanni2
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Digital water marking system project report
Kamal Acharya
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
MODULE 05 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
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) }