SlideShare una empresa de Scribd logo
1 de 17
Descargar para leer sin conexión
Syntax-Directed Translation
1
Syntax-Directed Translation
• Grammar symbols are associated with attributes to associate
information with the programming language constructs that
they represent.
• Values of these attributes are evaluated by the semantic
rules associated with the production rules.
• Evaluation of these semantic rules:
– may generate intermediate codes
– may put information into the symbol table
– may perform type checking
– may issue error messages
– may perform some other activities
– in fact, they may perform almost any activities.
2
Syntax-Directed Definitions and Translation Schemes
• When we associate semantic rules with productions, we use two
notations:
– Syntax-Directed Definitions
– Translation Schemes
• Syntax-Directed Definitions:
– give high-level specifications for translations
– hide many implementation details such as order of evaluation of semantic actions.
– We associate a production rule with a set of semantic actions, and we do not say when they
will be evaluated.
• Translation Schemes:
– indicate the order of evaluation of semantic actions associated with a production rule.
– In other words, translation schemes give a little bit information about implementation
details.
3
Syntax-Directed Definitions
• A syntax-directed definition is a generalization of a context-
free grammar in which:
– Each grammar symbol is associated with a set of attributes.
– This set of attributes for a grammar symbol is partitioned into two subsets called
synthesized and inherited attributes of that grammar symbol.
– Each production rule is associated with a set of semantic rules.
• Semantic rules set up dependencies between attributes which can
be represented by a dependency graph.
4
Annotated Parse Tree
• A parse tree showing the values of attributes at each node is
called an annotated parse tree.
• The process of computing the attributes values at the nodes is
called annotating (or decorating) of the parse tree.
• Of course, the order of these computations depends on the
dependency graph induced by the semantic rules.
7
Syntax-Directed Definition -- Example
Production Semantic Rules
L → E return print(E.val)
E → E1 + T E.val = E1.val + T.val
E → T E.val = T.val
T → T1 * F T.val = T1.val * F.val
T → F T.val = F.val
F → ( E ) F.val = E.val
F → digit F.val = digit.lexval
• Symbols E, T, and F are associated with a synthesized attribute val.
• The token digit has a synthesized attribute lexval (it is assumed that it
is evaluated by the lexical analyzer).
8
Annotated Parse Tree -- Example
Input: 5+3*4 L
E.val=17 return
E.val=5 + T.val=12
T.val=5 T.val=3 * F.val=4
F.val=3
F.val=5 digit.lexval=4
digit.lexval=5 digit.lexval=3
9
Dependency Graph
Input: 5+3*4 L
E.val=17
E.val=5 T.val=12
T.val=5 T.val=3 F.val=4
F.val=5 F.val=3 digit.lexval=4
digit.lexval=5 digit.lexval=3
CS308 Compiler Theory 10
Syntax-Directed Definition – Example2
Production Semantic Rules
E → E1 + T E.loc=newtemp(), E.code = E1.code || T.code || add E1.loc,T.loc,E.loc
E → T E.loc = T.loc, E.code=T.code
T → T1 * F T.loc=newtemp(), T.code = T1.code || F.code || mult T1.loc,F.loc,T.loc
T → F T.loc = F.loc, T.code=F.code
F → ( E ) F.loc = E.loc, F.code=E.code
F → id F.loc = id.name, F.code=“”
• Symbols E, T, and F are associated with synthesized attributes loc and
code.
• The token id has a synthesized attribute name (it is assumed that it
is evaluated by the lexical analyzer).
• It is assumed that || is the string concatenation operator.
CS308 Compiler Theory 11
Syntax-Directed Definition – Inherited Attributes
Production Semantic Rules
D → T L L.in = T.type
T → int T.type = integer
T → real T.type = real
L → L1 id L1.in = L.in, addtype(id.entry,L.in)
L → id addtype(id.entry,L.in)
• Symbol T is associated with a synthesized attribute type.
• Symbol L is associated with an inherited attribute in.
CS308 Compiler Theory 12
A Dependency Graph – Inherited Attributes
Input: real p q
D L.in=real
T L T.type=real L1.in=real addtype(q,real)
real L id addtype(p,real) id.entry=q
id id.entry=p
parse tree dependency graph
CS308 Compiler Theory 13
S-Attributed Definitions
• Syntax-directed definitions are used to specify syntax-directed translations.
• To create a translator for an arbitrary syntax-directed definition can be difficult.
• We would like to evaluate the semantic rules during parsing (i.e. in a single pass,
we will parse and we will also evaluate semantic rules during the parsing).
• We will look at two sub-classes of the syntax-directed definitions:
– S-Attributed Definitions: only synthesized attributes used in the syntax-directed
definitions.
– L-Attributed Definitions: in addition to synthesized attributes, we may also use inherited
attributes in a restricted fashion.
• To implement S-Attributed Definitions and L-Attributed Definitions are easy (we
can evaluate semantic rules in a single pass during the parsing).
• Implementations of S-attributed Definitions are a little bit easier than
implementations of L-Attributed Definitions
chp2sds.pdfgh
chp2sds.pdfgh
chp2sds.pdfgh
chp2sds.pdfgh
chp2sds.pdfgh

Más contenido relacionado

Similar a chp2sds.pdfgh

Similar a chp2sds.pdfgh (20)

Syntaxdirected
SyntaxdirectedSyntaxdirected
Syntaxdirected
 
Syntaxdirected (1)
Syntaxdirected (1)Syntaxdirected (1)
Syntaxdirected (1)
 
Ch5a
Ch5aCh5a
Ch5a
 
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...
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.ppt
 
Chapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.pptChapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.ppt
 
Unit iv-syntax-directed-translation
Unit iv-syntax-directed-translationUnit iv-syntax-directed-translation
Unit iv-syntax-directed-translation
 
Unit1.ppt
Unit1.pptUnit1.ppt
Unit1.ppt
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.ppt
 
COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
04 Syntax Analysis - RDP.pptx
04 Syntax Analysis - RDP.pptx04 Syntax Analysis - RDP.pptx
04 Syntax Analysis - RDP.pptx
 
COMPILER DESIGN.pdf
COMPILER DESIGN.pdfCOMPILER DESIGN.pdf
COMPILER DESIGN.pdf
 
Plc part 2
Plc  part 2Plc  part 2
Plc part 2
 
Semantics analysis
Semantics analysisSemantics analysis
Semantics analysis
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 

Más de KamranAli649587

UNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdfUNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdfKamranAli649587
 
Data design and analysis of computing tools
Data design and analysis of computing toolsData design and analysis of computing tools
Data design and analysis of computing toolsKamranAli649587
 
graphs data structure and algorithm link list
graphs data structure and algorithm link listgraphs data structure and algorithm link list
graphs data structure and algorithm link listKamranAli649587
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologyKamranAli649587
 
Encoder-and-decoder.pptx
Encoder-and-decoder.pptxEncoder-and-decoder.pptx
Encoder-and-decoder.pptxKamranAli649587
 
Radio propagation model...pptx
Radio propagation model...pptxRadio propagation model...pptx
Radio propagation model...pptxKamranAli649587
 
Loops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.pptLoops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.pptKamranAli649587
 
Lecture+06-TypesVars.ppt
Lecture+06-TypesVars.pptLecture+06-TypesVars.ppt
Lecture+06-TypesVars.pptKamranAli649587
 
radiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdfradiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdfKamranAli649587
 
Week11-EvaluationMethods.ppt
Week11-EvaluationMethods.pptWeek11-EvaluationMethods.ppt
Week11-EvaluationMethods.pptKamranAli649587
 
Week6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.pptWeek6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.pptKamranAli649587
 

Más de KamranAli649587 (20)

UNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdfUNIT-2-liang-barsky-clipping-algorithm-KM.pdf
UNIT-2-liang-barsky-clipping-algorithm-KM.pdf
 
Data design and analysis of computing tools
Data design and analysis of computing toolsData design and analysis of computing tools
Data design and analysis of computing tools
 
graphs data structure and algorithm link list
graphs data structure and algorithm link listgraphs data structure and algorithm link list
graphs data structure and algorithm link list
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
 
Encoder-and-decoder.pptx
Encoder-and-decoder.pptxEncoder-and-decoder.pptx
Encoder-and-decoder.pptx
 
Radio propagation model...pptx
Radio propagation model...pptxRadio propagation model...pptx
Radio propagation model...pptx
 
Loops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.pptLoops_and_FunctionsWeek4_0.ppt
Loops_and_FunctionsWeek4_0.ppt
 
Lecture+06-TypesVars.ppt
Lecture+06-TypesVars.pptLecture+06-TypesVars.ppt
Lecture+06-TypesVars.ppt
 
C++InputOutput.PPT
C++InputOutput.PPTC++InputOutput.PPT
C++InputOutput.PPT
 
radiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdfradiopropagation-140328202308-phpapp01.pdf
radiopropagation-140328202308-phpapp01.pdf
 
cluster.pptx
cluster.pptxcluster.pptx
cluster.pptx
 
Week11-EvaluationMethods.ppt
Week11-EvaluationMethods.pptWeek11-EvaluationMethods.ppt
Week11-EvaluationMethods.ppt
 
Week6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.pptWeek6-Sectionsofapaper.ppt
Week6-Sectionsofapaper.ppt
 
null-13.pdf
null-13.pdfnull-13.pdf
null-13.pdf
 
Reaches
ReachesReaches
Reaches
 
null-6.pdf
null-6.pdfnull-6.pdf
null-6.pdf
 
null-1.pptx
null-1.pptxnull-1.pptx
null-1.pptx
 
Lect-01.ppt
Lect-01.pptLect-01.ppt
Lect-01.ppt
 
Db_05.ppt
Db_05.pptDb_05.ppt
Db_05.ppt
 
ch7.ppt
ch7.pptch7.ppt
ch7.ppt
 

Último

Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...allensay1
 
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesMysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesDipal Arora
 
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxB.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxpriyanshujha201
 
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...lizamodels9
 
John Halpern sued for sexual assault.pdf
John Halpern sued for sexual assault.pdfJohn Halpern sued for sexual assault.pdf
John Halpern sued for sexual assault.pdfAmzadHosen3
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayNZSG
 
Uneak White's Personal Brand Exploration Presentation
Uneak White's Personal Brand Exploration PresentationUneak White's Personal Brand Exploration Presentation
Uneak White's Personal Brand Exploration Presentationuneakwhite
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with CultureSeta Wicaksana
 
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...lizamodels9
 
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLMONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLSeo
 
Value Proposition canvas- Customer needs and pains
Value Proposition canvas- Customer needs and painsValue Proposition canvas- Customer needs and pains
Value Proposition canvas- Customer needs and painsP&CO
 
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...Sheetaleventcompany
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangaloreamitlee9823
 
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...Aggregage
 
Falcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to ProsperityFalcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to Prosperityhemanthkumar470700
 
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756dollysharma2066
 
RSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors DataRSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors DataExhibitors Data
 

Último (20)

Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
Call Girls Service In Old Town Dubai ((0551707352)) Old Town Dubai Call Girl ...
 
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best ServicesMysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
Mysore Call Girls 8617370543 WhatsApp Number 24x7 Best Services
 
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptxB.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
B.COM Unit – 4 ( CORPORATE SOCIAL RESPONSIBILITY ( CSR ).pptx
 
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
Russian Call Girls In Gurgaon ❤️8448577510 ⊹Best Escorts Service In 24/7 Delh...
 
John Halpern sued for sexual assault.pdf
John Halpern sued for sexual assault.pdfJohn Halpern sued for sexual assault.pdf
John Halpern sued for sexual assault.pdf
 
It will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 MayIt will be International Nurses' Day on 12 May
It will be International Nurses' Day on 12 May
 
Uneak White's Personal Brand Exploration Presentation
Uneak White's Personal Brand Exploration PresentationUneak White's Personal Brand Exploration Presentation
Uneak White's Personal Brand Exploration Presentation
 
Falcon Invoice Discounting platform in india
Falcon Invoice Discounting platform in indiaFalcon Invoice Discounting platform in india
Falcon Invoice Discounting platform in india
 
Organizational Transformation Lead with Culture
Organizational Transformation Lead with CultureOrganizational Transformation Lead with Culture
Organizational Transformation Lead with Culture
 
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
Call Girls From Pari Chowk Greater Noida ❤️8448577510 ⊹Best Escorts Service I...
 
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRLMONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
MONA 98765-12871 CALL GIRLS IN LUDHIANA LUDHIANA CALL GIRL
 
Value Proposition canvas- Customer needs and pains
Value Proposition canvas- Customer needs and painsValue Proposition canvas- Customer needs and pains
Value Proposition canvas- Customer needs and pains
 
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabiunwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
unwanted pregnancy Kit [+918133066128] Abortion Pills IN Dubai UAE Abudhabi
 
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
Chandigarh Escorts Service 📞8868886958📞 Just📲 Call Nihal Chandigarh Call Girl...
 
(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7
(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7
(Anamika) VIP Call Girls Napur Call Now 8617697112 Napur Escorts 24x7
 
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Hebbal Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
The Path to Product Excellence: Avoiding Common Pitfalls and Enhancing Commun...
 
Falcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to ProsperityFalcon's Invoice Discounting: Your Path to Prosperity
Falcon's Invoice Discounting: Your Path to Prosperity
 
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Majnu Ka Tilla, Delhi Contact Us 8377877756
 
RSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors DataRSA Conference Exhibitor List 2024 - Exhibitors Data
RSA Conference Exhibitor List 2024 - Exhibitors Data
 

chp2sds.pdfgh

  • 2. Syntax-Directed Translation • Grammar symbols are associated with attributes to associate information with the programming language constructs that they represent. • Values of these attributes are evaluated by the semantic rules associated with the production rules. • Evaluation of these semantic rules: – may generate intermediate codes – may put information into the symbol table – may perform type checking – may issue error messages – may perform some other activities – in fact, they may perform almost any activities. 2
  • 3. Syntax-Directed Definitions and Translation Schemes • When we associate semantic rules with productions, we use two notations: – Syntax-Directed Definitions – Translation Schemes • Syntax-Directed Definitions: – give high-level specifications for translations – hide many implementation details such as order of evaluation of semantic actions. – We associate a production rule with a set of semantic actions, and we do not say when they will be evaluated. • Translation Schemes: – indicate the order of evaluation of semantic actions associated with a production rule. – In other words, translation schemes give a little bit information about implementation details. 3
  • 4. Syntax-Directed Definitions • A syntax-directed definition is a generalization of a context- free grammar in which: – Each grammar symbol is associated with a set of attributes. – This set of attributes for a grammar symbol is partitioned into two subsets called synthesized and inherited attributes of that grammar symbol. – Each production rule is associated with a set of semantic rules. • Semantic rules set up dependencies between attributes which can be represented by a dependency graph. 4
  • 5. Annotated Parse Tree • A parse tree showing the values of attributes at each node is called an annotated parse tree. • The process of computing the attributes values at the nodes is called annotating (or decorating) of the parse tree. • Of course, the order of these computations depends on the dependency graph induced by the semantic rules. 7
  • 6. Syntax-Directed Definition -- Example Production Semantic Rules L → E return print(E.val) E → E1 + T E.val = E1.val + T.val E → T E.val = T.val T → T1 * F T.val = T1.val * F.val T → F T.val = F.val F → ( E ) F.val = E.val F → digit F.val = digit.lexval • Symbols E, T, and F are associated with a synthesized attribute val. • The token digit has a synthesized attribute lexval (it is assumed that it is evaluated by the lexical analyzer). 8
  • 7. Annotated Parse Tree -- Example Input: 5+3*4 L E.val=17 return E.val=5 + T.val=12 T.val=5 T.val=3 * F.val=4 F.val=3 F.val=5 digit.lexval=4 digit.lexval=5 digit.lexval=3 9
  • 8. Dependency Graph Input: 5+3*4 L E.val=17 E.val=5 T.val=12 T.val=5 T.val=3 F.val=4 F.val=5 F.val=3 digit.lexval=4 digit.lexval=5 digit.lexval=3 CS308 Compiler Theory 10
  • 9. Syntax-Directed Definition – Example2 Production Semantic Rules E → E1 + T E.loc=newtemp(), E.code = E1.code || T.code || add E1.loc,T.loc,E.loc E → T E.loc = T.loc, E.code=T.code T → T1 * F T.loc=newtemp(), T.code = T1.code || F.code || mult T1.loc,F.loc,T.loc T → F T.loc = F.loc, T.code=F.code F → ( E ) F.loc = E.loc, F.code=E.code F → id F.loc = id.name, F.code=“” • Symbols E, T, and F are associated with synthesized attributes loc and code. • The token id has a synthesized attribute name (it is assumed that it is evaluated by the lexical analyzer). • It is assumed that || is the string concatenation operator. CS308 Compiler Theory 11
  • 10. Syntax-Directed Definition – Inherited Attributes Production Semantic Rules D → T L L.in = T.type T → int T.type = integer T → real T.type = real L → L1 id L1.in = L.in, addtype(id.entry,L.in) L → id addtype(id.entry,L.in) • Symbol T is associated with a synthesized attribute type. • Symbol L is associated with an inherited attribute in. CS308 Compiler Theory 12
  • 11. A Dependency Graph – Inherited Attributes Input: real p q D L.in=real T L T.type=real L1.in=real addtype(q,real) real L id addtype(p,real) id.entry=q id id.entry=p parse tree dependency graph CS308 Compiler Theory 13
  • 12. S-Attributed Definitions • Syntax-directed definitions are used to specify syntax-directed translations. • To create a translator for an arbitrary syntax-directed definition can be difficult. • We would like to evaluate the semantic rules during parsing (i.e. in a single pass, we will parse and we will also evaluate semantic rules during the parsing). • We will look at two sub-classes of the syntax-directed definitions: – S-Attributed Definitions: only synthesized attributes used in the syntax-directed definitions. – L-Attributed Definitions: in addition to synthesized attributes, we may also use inherited attributes in a restricted fashion. • To implement S-Attributed Definitions and L-Attributed Definitions are easy (we can evaluate semantic rules in a single pass during the parsing). • Implementations of S-attributed Definitions are a little bit easier than implementations of L-Attributed Definitions