SlideShare a Scribd company logo
Syntax Directed
Translation
1
Syntax-directed translation
Syntax-directed translation (SDT) refers to a method of
compiler implementation where the source language
translation is completely driven by the parser.
The parsing process and parse trees are used to direct
semantic analysis and the translation of the source
program.
We can augment grammar with information to control
the semantic analysis and translation. Such grammars are
called attribute grammars.
2
Syntax-directed translation
Associate attributes with each grammar symbol that
describes its properties.
An attribute has a name and an associated value.
With each production in a grammar, give semantic rules
or actions.
The general approach to syntax-directed translation is to
construct a parse tree or syntax tree and compute the
values of attributes at the nodes of the tree by visiting them
in some order.
3
Syntax-directed translation
There are two ways to represent the semantic rules
associated with grammar symbols.
Syntax-Directed Definitions (SDD)
Syntax-Directed Translation Schemes (SDT)
4
Syntax-Directed Definitions
A syntax-directed definition (SDD) is a context-free
grammar together with attributes and rules.
Attributes are associated with grammar symbols and rules
are associated with productions.
PRODUCTION SEMANTIC RULE
E → E1 + T E.code = E1.code || T.code || ‘+’
5
Syntax-Directed Definitions
SDDs are highly readable and give high-level
specifications for translations.
But they hide many implementation details.
For example, they do not specify order of evaluation of
semantic actions.
Syntax-Directed Translation Schemes (SDT) embeds
program fragments called semantic actions within
production bodies
SDTs are more efficient than SDDs as they indicate the
order of evaluation of semantic actions associated with a
production rule.
6
Inherited Attributes
An INHERITED ATTRIBUTE for a non-terminal B
at a parse-tree node N is defined by a semantic rule
associated with the production at the parent of N. The
production must have B as a symbol in its body.
An inherited attribute at node N is defined only in
terms of attribute values at N's parent, N itself, and N's
siblings.
7
Synthesized Attributes
Each of the non-terminals has a single synthesized attribute, called val.
An SDD that involves only synthesized attributes is called S-attributed
Each rule computes an attribute for the non-terminal at the head of a
production from attributes taken from the body of the production.
8
Evaluating an SDD at the Nodes of a Parse Tree
A parse tree, showing the value(s) of its attribute(s)
is called an annotated parse tree.
With synthesized attributes, evaluate attributes in
bottom-up order.
9
Annotated Parse Tree for 3*5+4n10
give annotated parse trees for the following
expressions:
11
12
13
SDD for expression grammar with
inherited attributes
14
Annotated parse-tree for the input
real id1, id2, id3
15
16
SDD for expression grammar with
inherited attributes
17
Annotated Parse Tree for 3*5 18
1) L -> En L.val = E.val
2) E -> TE' E'.inh = T.val
E.val = E'.syn
3) E' -> +TE_1' E_1'.inh = E'.inh + T.val
E'.syn = E_1'.syn
4) E' -> ε E'.syn = E'.inh
5) T -> FT' T'.inh = F.val
T.val = T'.syn
6) T' -> *FT_1' T_1'.inh = T'.inh * F.val
T'.syn = T_1'.syn
7) T' -> ε T'.syn = T'.inh
8) F -> (E) F.val = E.val
9) F -> digit F.val = digit.lexval
19
20
21
SDD with circular dependencies no guarantee in the
order of evaluation.
22
Evaluation Orders for SDD's
"Dependency graphs“ tool for determining an
evaluation order for the attribute instances in a given
parse tree.
annotated parse tree shows the values of attributes,
a dependency graph helps to determine how those
values can be computed.
23
Dependency graphs
Edges express constraints implied by the semantic
rules.
Each attribute is associated to a node
If a semantic rule associated with a production p
defines the value of synthesized attribute A.b in terms
of the value of X.c, then graph has an edge from X.c to
A.b
If a semantic rule associated with a production p
defines the value of inherited attribute B.c in terms of
value of X.a, then graph has an edge from X.a to B.c
24
25
26
Topological Sort
 A dependency graph characterizes the possible order in
which we can evaluate the attributes at various nodes of a
parse tree.
 If there is an edge from node M to N, then attribute
corresponding to M first be evaluated before evaluating
N.
 Thus the allowable orders of evaluation are N1, N2, ...,
Nk such that if there is an edge from Ni to Nj then i < j.
 Such an ordering embeds a directed graph into a linear
order, and is called a topological sort of the graph.
 If there is any cycle in the graph, then there are no
topological sorts.
27
S-Attributed
If every attribute is synthesized.
S-attributed SDD can be evaluated in bottom up order of
the nodes of the parse tree.
28
L-attributed definitions
 Synthesized, or
 Inherited, but with the rules limited as follows .
Suppose that there is a production A -> XIX2 · ..
Xn , and that there is an inherited attribute Xi·a
computed by a rule associated with this
production. Then the rule may use only:
29
L-attributed definitions
Inherited attributes associated with the head A.
Either inherited or synthesized attributes
associated with the occurrences of symbols Xl ,
X2 , ••• ,Xi-l located to the left of Xi.
Inherited or synthesized attributes associated
with this occurrence of Xi itself, but only in
such a way that there are no cycles in a
dependency graph formed by the attributes of
this Xi .
30
Semantic Rules with Controlled
Side Effects
Permit incidental side effects that do not disturb
attribute evaluation.
Impose restriction on allowable evaluation orders,
so that the same translation is produced for any
allowable order.
31
Applications of Syntax-Directed
Translations
 Construction of Syntax Trees
 Syntax trees are useful for representing programming language constructs like
expressions and statements.
 Each node of a syntax tree represents a construct; the children of the node
represent the meaningful components of the construct.
 e.g. a syntax-tree node representing an expression E1 + E2 has label + and two
children representing the sub expressions E1 and E2
32
Applications of Syntax-
Directed Translations
 Each node is implemented by objects with suitable number of fields; each
object will have an op field that is the label of the node with additional fields
as follows:
 If the node is a leaf, an additional field holds the lexical value for the leaf . This is
created by function Leaf(op, val)
 If the node is an interior node, there are as many fields as the node has children in
the syntax tree. This is created by function Node(op, c1, 2,...,ck) .
33
34
35
Syntax Directed Translation
Schemes
 SDT can be implemented by first building a parse
tree and then performing the actions in a left-to-
right depth-first order
 Postfix Translation Schemes:each semantic action
can be placed at the end of production and
executed along with the reduction of body to the
head of the production.
36
37
Parser-Stack Implementation
of Postfix SDTs
38
Semantic Actions during
Parsing
 when shifting
 push the value of the terminal on the semantic
stack
 when reducing
 pop k values from the semantic stack, where k is
the number of symbols on production’s RHS
 push the production’s value on the semantic stack
39
SDTs with Actions inside
Productions
 Action can be placed at any position in the production body.
 Action is performed immediately after all symbols left to it are
processed.
 Given B —> X { a } Y , an action a is done after
 we have recognized X (if X is a terminal), or
 all terminals derived from X (if X is a nonterminal).
40
SDTs with Actions inside
Productions
 If bottom-up parser is used, then action a is performed as soon as X appears on
top of the stack.
 If top-down parser is used, then action a is performed
 just before Y is expanded (if Y is nonterminal), or
 check Y on input (if Y is a terminal).
 Any SDT can be implemented as follows:
 Ignoring actions, parse input and produce parse tree.
 Add additional children to node N for action in α, where A → α.
 Perform preorder traversal of the tree, and as soon as a node labeled by an action is
visited, perform that action.
41
Semantic Actions during
Parsing
42
Type
checking
43
Type Checking?
 Type checking is the process of verifying that each operation
executed in a program respects the type system of the
language.
 This generally means that all operands in any
expression are of appropriate types and number.
 Much of what we do in the semantic analysis phase is type
checking.
44
45
Static checks 46
type system
 The compiler must then determine that these type
expressions conform to a collection of logic al
rules that is called the type system for the source
language
47
Type Expression 48
Type Expression 49
Type Expression 50
Type System 51
Static and Dynamic
checking
52
Error Recovery 53
Coercions 54
Ad

More Related Content

What's hot (20)

Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
United International University
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
Anusuya123
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
MAHASREEM
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
Sudip Singh
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
Radhakrishnan Chinnusamy
 
Code generation
Code generationCode generation
Code generation
Aparna Nayak
 
Loop optimization
Loop optimizationLoop optimization
Loop optimization
Vivek Gandhi
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
Akhil Kaushik
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
Jothi Lakshmi
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
Dattatray Gandhmal
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
Dattatray Gandhmal
 
Type checking compiler construction Chapter #6
Type checking compiler construction Chapter #6Type checking compiler construction Chapter #6
Type checking compiler construction Chapter #6
Daniyal Mughal
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
Kuppusamy P
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest path
Arafat Hossan
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
Mahbubur Rahman
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
Dattatray Gandhmal
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
Akshaya Arunan
 
Reduction & Handle Pruning
Reduction & Handle PruningReduction & Handle Pruning
Reduction & Handle Pruning
MdAshikJiddney
 
Parsing
ParsingParsing
Parsing
khush_boo31
 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical Analyzer
Archana Gopinath
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
Anusuya123
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
MAHASREEM
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
Sudip Singh
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
Akhil Kaushik
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
Jothi Lakshmi
 
Type checking compiler construction Chapter #6
Type checking compiler construction Chapter #6Type checking compiler construction Chapter #6
Type checking compiler construction Chapter #6
Daniyal Mughal
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
Kuppusamy P
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest path
Arafat Hossan
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
Mahbubur Rahman
 
Reduction & Handle Pruning
Reduction & Handle PruningReduction & Handle Pruning
Reduction & Handle Pruning
MdAshikJiddney
 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical Analyzer
Archana Gopinath
 

Similar to Syntax directed translation (20)

Compiler design selective dissemination of information syntax direct translat...
Compiler design selective dissemination of information syntax direct translat...Compiler design selective dissemination of information syntax direct translat...
Compiler design selective dissemination of information syntax direct translat...
ganeshjaggineni1927
 
Chapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.pptChapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.ppt
MulugetaGebino
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.ppt
JigarThummar1
 
Chapte - Syntax Directed Translation.ppt
Chapte - Syntax Directed Translation.pptChapte - Syntax Directed Translation.ppt
Chapte - Syntax Directed Translation.ppt
PraveenaFppt
 
Chapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.pptChapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.ppt
FamiDan
 
Compiler Design UNIT III PPT (SDD, SDT).pdf
Compiler Design UNIT III PPT (SDD, SDT).pdfCompiler Design UNIT III PPT (SDD, SDT).pdf
Compiler Design UNIT III PPT (SDD, SDT).pdf
Shiva350357
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.ppt
SatyamVerma61
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
Iffat Anjum
 
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
venkatapranaykumarGa
 
Unit iv-syntax-directed-translation
Unit iv-syntax-directed-translationUnit iv-syntax-directed-translation
Unit iv-syntax-directed-translation
Ajith kumar M P
 
Chapter _4_Semantic Analysis .pptx
Chapter _4_Semantic Analysis .pptxChapter _4_Semantic Analysis .pptx
Chapter _4_Semantic Analysis .pptx
ArebuMaruf
 
Chapter -4.pptx
Chapter -4.pptxChapter -4.pptx
Chapter -4.pptx
woldu2
 
syntax-directed-translation.ppt syntax-directed-translation.ppt
syntax-directed-translation.ppt syntax-directed-translation.pptsyntax-directed-translation.ppt syntax-directed-translation.ppt
syntax-directed-translation.ppt syntax-directed-translation.ppt
dejusertse
 
Syntax Directed Definition and its applications
Syntax Directed Definition and its applicationsSyntax Directed Definition and its applications
Syntax Directed Definition and its applications
ShivanandManjaragi2
 
compiler design course focus of chapter 3 and 4 part
compiler design course focus of chapter 3 and 4 partcompiler design course focus of chapter 3 and 4 part
compiler design course focus of chapter 3 and 4 part
abel185080
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01
Iffat Anjum
 
SyntaxDirectedTranslation in Compiler Design
SyntaxDirectedTranslation in Compiler DesignSyntaxDirectedTranslation in Compiler Design
SyntaxDirectedTranslation in Compiler Design
Ratnakar Mikkili
 
chp2sds.pdfgh
chp2sds.pdfghchp2sds.pdfgh
chp2sds.pdfgh
KamranAli649587
 
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
 
lect-05.pdf
lect-05.pdflect-05.pdf
lect-05.pdf
KamranAli649587
 
Compiler design selective dissemination of information syntax direct translat...
Compiler design selective dissemination of information syntax direct translat...Compiler design selective dissemination of information syntax direct translat...
Compiler design selective dissemination of information syntax direct translat...
ganeshjaggineni1927
 
Chapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.pptChapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.ppt
MulugetaGebino
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.ppt
JigarThummar1
 
Chapte - Syntax Directed Translation.ppt
Chapte - Syntax Directed Translation.pptChapte - Syntax Directed Translation.ppt
Chapte - Syntax Directed Translation.ppt
PraveenaFppt
 
Chapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.pptChapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.ppt
FamiDan
 
Compiler Design UNIT III PPT (SDD, SDT).pdf
Compiler Design UNIT III PPT (SDD, SDT).pdfCompiler Design UNIT III PPT (SDD, SDT).pdf
Compiler Design UNIT III PPT (SDD, SDT).pdf
Shiva350357
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.ppt
SatyamVerma61
 
Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2Lecture 11 semantic analysis 2
Lecture 11 semantic analysis 2
Iffat Anjum
 
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
venkatapranaykumarGa
 
Unit iv-syntax-directed-translation
Unit iv-syntax-directed-translationUnit iv-syntax-directed-translation
Unit iv-syntax-directed-translation
Ajith kumar M P
 
Chapter _4_Semantic Analysis .pptx
Chapter _4_Semantic Analysis .pptxChapter _4_Semantic Analysis .pptx
Chapter _4_Semantic Analysis .pptx
ArebuMaruf
 
Chapter -4.pptx
Chapter -4.pptxChapter -4.pptx
Chapter -4.pptx
woldu2
 
syntax-directed-translation.ppt syntax-directed-translation.ppt
syntax-directed-translation.ppt syntax-directed-translation.pptsyntax-directed-translation.ppt syntax-directed-translation.ppt
syntax-directed-translation.ppt syntax-directed-translation.ppt
dejusertse
 
Syntax Directed Definition and its applications
Syntax Directed Definition and its applicationsSyntax Directed Definition and its applications
Syntax Directed Definition and its applications
ShivanandManjaragi2
 
compiler design course focus of chapter 3 and 4 part
compiler design course focus of chapter 3 and 4 partcompiler design course focus of chapter 3 and 4 part
compiler design course focus of chapter 3 and 4 part
abel185080
 
Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01Lecture 10 semantic analysis 01
Lecture 10 semantic analysis 01
Iffat Anjum
 
SyntaxDirectedTranslation in Compiler Design
SyntaxDirectedTranslation in Compiler DesignSyntaxDirectedTranslation in Compiler Design
SyntaxDirectedTranslation in Compiler Design
Ratnakar Mikkili
 
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
 
Ad

More from Akshaya Arunan (9)

Traffic Based Malicious Switch and DDoS Detection in Software Defined Network
Traffic Based Malicious Switch and DDoS Detection in Software Defined NetworkTraffic Based Malicious Switch and DDoS Detection in Software Defined Network
Traffic Based Malicious Switch and DDoS Detection in Software Defined Network
Akshaya Arunan
 
Enhanced Traffic Based Malicious Switch Detection in SDN
Enhanced Traffic Based Malicious Switch Detection in SDNEnhanced Traffic Based Malicious Switch Detection in SDN
Enhanced Traffic Based Malicious Switch Detection in SDN
Akshaya Arunan
 
Akshayappt
AkshayapptAkshayappt
Akshayappt
Akshaya Arunan
 
OpenSec Policy-Based Security Using
OpenSec Policy-Based Security UsingOpenSec Policy-Based Security Using
OpenSec Policy-Based Security Using
Akshaya Arunan
 
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES FOR NETWORK FORENSIC ANALYSIS
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES  FOR NETWORK FORENSIC ANALYSISA TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES  FOR NETWORK FORENSIC ANALYSIS
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES FOR NETWORK FORENSIC ANALYSIS
Akshaya Arunan
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
Akshaya Arunan
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
Akshaya Arunan
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
Akshaya Arunan
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
Akshaya Arunan
 
Traffic Based Malicious Switch and DDoS Detection in Software Defined Network
Traffic Based Malicious Switch and DDoS Detection in Software Defined NetworkTraffic Based Malicious Switch and DDoS Detection in Software Defined Network
Traffic Based Malicious Switch and DDoS Detection in Software Defined Network
Akshaya Arunan
 
Enhanced Traffic Based Malicious Switch Detection in SDN
Enhanced Traffic Based Malicious Switch Detection in SDNEnhanced Traffic Based Malicious Switch Detection in SDN
Enhanced Traffic Based Malicious Switch Detection in SDN
Akshaya Arunan
 
OpenSec Policy-Based Security Using
OpenSec Policy-Based Security UsingOpenSec Policy-Based Security Using
OpenSec Policy-Based Security Using
Akshaya Arunan
 
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES FOR NETWORK FORENSIC ANALYSIS
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES  FOR NETWORK FORENSIC ANALYSISA TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES  FOR NETWORK FORENSIC ANALYSIS
A TRANSDUCTIVE SCHEME BASED INFERENCE TECHNIQUES FOR NETWORK FORENSIC ANALYSIS
Akshaya Arunan
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
Akshaya Arunan
 
Ad

Recently uploaded (20)

Diabetic neuropathy peripheral autonomic
Diabetic neuropathy peripheral autonomicDiabetic neuropathy peripheral autonomic
Diabetic neuropathy peripheral autonomic
Pankaj Patawari
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-26-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-26-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-26-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-26-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
High Performance Liquid Chromatography .pptx
High Performance Liquid Chromatography .pptxHigh Performance Liquid Chromatography .pptx
High Performance Liquid Chromatography .pptx
Ayush Srivastava
 
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd yearVitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
ARUN KUMAR
 
Open Access: Revamping Library Learning Resources.
Open Access: Revamping Library Learning Resources.Open Access: Revamping Library Learning Resources.
Open Access: Revamping Library Learning Resources.
Rishi Bankim Chandra Evening College, Naihati, North 24 Parganas, West Bengal, India
 
Diabetic neuropathy peripheral autonomic
Diabetic neuropathy peripheral autonomicDiabetic neuropathy peripheral autonomic
Diabetic neuropathy peripheral autonomic
Pankaj Patawari
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
High Performance Liquid Chromatography .pptx
High Performance Liquid Chromatography .pptxHigh Performance Liquid Chromatography .pptx
High Performance Liquid Chromatography .pptx
Ayush Srivastava
 
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd yearVitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
ARUN KUMAR
 

Syntax directed translation

  • 2. Syntax-directed translation Syntax-directed translation (SDT) refers to a method of compiler implementation where the source language translation is completely driven by the parser. The parsing process and parse trees are used to direct semantic analysis and the translation of the source program. We can augment grammar with information to control the semantic analysis and translation. Such grammars are called attribute grammars. 2
  • 3. Syntax-directed translation Associate attributes with each grammar symbol that describes its properties. An attribute has a name and an associated value. With each production in a grammar, give semantic rules or actions. The general approach to syntax-directed translation is to construct a parse tree or syntax tree and compute the values of attributes at the nodes of the tree by visiting them in some order. 3
  • 4. Syntax-directed translation There are two ways to represent the semantic rules associated with grammar symbols. Syntax-Directed Definitions (SDD) Syntax-Directed Translation Schemes (SDT) 4
  • 5. Syntax-Directed Definitions A syntax-directed definition (SDD) is a context-free grammar together with attributes and rules. Attributes are associated with grammar symbols and rules are associated with productions. PRODUCTION SEMANTIC RULE E → E1 + T E.code = E1.code || T.code || ‘+’ 5
  • 6. Syntax-Directed Definitions SDDs are highly readable and give high-level specifications for translations. But they hide many implementation details. For example, they do not specify order of evaluation of semantic actions. Syntax-Directed Translation Schemes (SDT) embeds program fragments called semantic actions within production bodies SDTs are more efficient than SDDs as they indicate the order of evaluation of semantic actions associated with a production rule. 6
  • 7. Inherited Attributes An INHERITED ATTRIBUTE for a non-terminal B at a parse-tree node N is defined by a semantic rule associated with the production at the parent of N. The production must have B as a symbol in its body. An inherited attribute at node N is defined only in terms of attribute values at N's parent, N itself, and N's siblings. 7
  • 8. Synthesized Attributes Each of the non-terminals has a single synthesized attribute, called val. An SDD that involves only synthesized attributes is called S-attributed Each rule computes an attribute for the non-terminal at the head of a production from attributes taken from the body of the production. 8
  • 9. Evaluating an SDD at the Nodes of a Parse Tree A parse tree, showing the value(s) of its attribute(s) is called an annotated parse tree. With synthesized attributes, evaluate attributes in bottom-up order. 9
  • 10. Annotated Parse Tree for 3*5+4n10
  • 11. give annotated parse trees for the following expressions: 11
  • 12. 12
  • 13. 13
  • 14. SDD for expression grammar with inherited attributes 14
  • 15. Annotated parse-tree for the input real id1, id2, id3 15
  • 16. 16
  • 17. SDD for expression grammar with inherited attributes 17
  • 18. Annotated Parse Tree for 3*5 18
  • 19. 1) L -> En L.val = E.val 2) E -> TE' E'.inh = T.val E.val = E'.syn 3) E' -> +TE_1' E_1'.inh = E'.inh + T.val E'.syn = E_1'.syn 4) E' -> ε E'.syn = E'.inh 5) T -> FT' T'.inh = F.val T.val = T'.syn 6) T' -> *FT_1' T_1'.inh = T'.inh * F.val T'.syn = T_1'.syn 7) T' -> ε T'.syn = T'.inh 8) F -> (E) F.val = E.val 9) F -> digit F.val = digit.lexval 19
  • 20. 20
  • 21. 21
  • 22. SDD with circular dependencies no guarantee in the order of evaluation. 22
  • 23. Evaluation Orders for SDD's "Dependency graphs“ tool for determining an evaluation order for the attribute instances in a given parse tree. annotated parse tree shows the values of attributes, a dependency graph helps to determine how those values can be computed. 23
  • 24. Dependency graphs Edges express constraints implied by the semantic rules. Each attribute is associated to a node If a semantic rule associated with a production p defines the value of synthesized attribute A.b in terms of the value of X.c, then graph has an edge from X.c to A.b If a semantic rule associated with a production p defines the value of inherited attribute B.c in terms of value of X.a, then graph has an edge from X.a to B.c 24
  • 25. 25
  • 26. 26
  • 27. Topological Sort  A dependency graph characterizes the possible order in which we can evaluate the attributes at various nodes of a parse tree.  If there is an edge from node M to N, then attribute corresponding to M first be evaluated before evaluating N.  Thus the allowable orders of evaluation are N1, N2, ..., Nk such that if there is an edge from Ni to Nj then i < j.  Such an ordering embeds a directed graph into a linear order, and is called a topological sort of the graph.  If there is any cycle in the graph, then there are no topological sorts. 27
  • 28. S-Attributed If every attribute is synthesized. S-attributed SDD can be evaluated in bottom up order of the nodes of the parse tree. 28
  • 29. L-attributed definitions  Synthesized, or  Inherited, but with the rules limited as follows . Suppose that there is a production A -> XIX2 · .. Xn , and that there is an inherited attribute Xi·a computed by a rule associated with this production. Then the rule may use only: 29
  • 30. L-attributed definitions Inherited attributes associated with the head A. Either inherited or synthesized attributes associated with the occurrences of symbols Xl , X2 , ••• ,Xi-l located to the left of Xi. Inherited or synthesized attributes associated with this occurrence of Xi itself, but only in such a way that there are no cycles in a dependency graph formed by the attributes of this Xi . 30
  • 31. Semantic Rules with Controlled Side Effects Permit incidental side effects that do not disturb attribute evaluation. Impose restriction on allowable evaluation orders, so that the same translation is produced for any allowable order. 31
  • 32. Applications of Syntax-Directed Translations  Construction of Syntax Trees  Syntax trees are useful for representing programming language constructs like expressions and statements.  Each node of a syntax tree represents a construct; the children of the node represent the meaningful components of the construct.  e.g. a syntax-tree node representing an expression E1 + E2 has label + and two children representing the sub expressions E1 and E2 32
  • 33. Applications of Syntax- Directed Translations  Each node is implemented by objects with suitable number of fields; each object will have an op field that is the label of the node with additional fields as follows:  If the node is a leaf, an additional field holds the lexical value for the leaf . This is created by function Leaf(op, val)  If the node is an interior node, there are as many fields as the node has children in the syntax tree. This is created by function Node(op, c1, 2,...,ck) . 33
  • 34. 34
  • 35. 35
  • 36. Syntax Directed Translation Schemes  SDT can be implemented by first building a parse tree and then performing the actions in a left-to- right depth-first order  Postfix Translation Schemes:each semantic action can be placed at the end of production and executed along with the reduction of body to the head of the production. 36
  • 37. 37
  • 39. Semantic Actions during Parsing  when shifting  push the value of the terminal on the semantic stack  when reducing  pop k values from the semantic stack, where k is the number of symbols on production’s RHS  push the production’s value on the semantic stack 39
  • 40. SDTs with Actions inside Productions  Action can be placed at any position in the production body.  Action is performed immediately after all symbols left to it are processed.  Given B —> X { a } Y , an action a is done after  we have recognized X (if X is a terminal), or  all terminals derived from X (if X is a nonterminal). 40
  • 41. SDTs with Actions inside Productions  If bottom-up parser is used, then action a is performed as soon as X appears on top of the stack.  If top-down parser is used, then action a is performed  just before Y is expanded (if Y is nonterminal), or  check Y on input (if Y is a terminal).  Any SDT can be implemented as follows:  Ignoring actions, parse input and produce parse tree.  Add additional children to node N for action in α, where A → α.  Perform preorder traversal of the tree, and as soon as a node labeled by an action is visited, perform that action. 41
  • 44. Type Checking?  Type checking is the process of verifying that each operation executed in a program respects the type system of the language.  This generally means that all operands in any expression are of appropriate types and number.  Much of what we do in the semantic analysis phase is type checking. 44
  • 45. 45
  • 47. type system  The compiler must then determine that these type expressions conform to a collection of logic al rules that is called the type system for the source language 47