SlideShare una empresa de Scribd logo
1 de 28
Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
The Structure of our Compiler Revisited Lexical analyzer Syntax-directed translator Character stream Token stream Java bytecode Yacc specification with semantic rules JVM specification Lex specification
Syntax-Directed Definitions ,[object Object],[object Object],[object Object],[object Object]
Example Attribute Grammar L      E   n E     E 1   +   T E    T T     T 1   *   F T    F F     (   E   ) F     digit print ( E. val) E. val   :=  E 1 . val  + T. val E. val   :=  T. val T. val   :=  T 1 . val  * F. val T. val   :=  F. val F. val   :=  E. val F. val   :=   digit .lexval Production Semantic Rule Note: all attributes in this example are of the synthesized type
Example Annotated Parse Tree E. val   =  16 T. val =  2 9 + 5 + 2 E. val   =  14 E. val   =  9 T. val   =  5 F. val =  9 Note: all attributes in this example are of the synthesized type L n T. val =  9 F. val =  5 F. val =  5
Annotating a Parse Tree With Depth-First Traversals procedure  visit ( n  :  node ); begin   for  each child  m  of  n , from left to right  do   visit ( m );   evaluate semantic rules at node  n end
Depth-First Traversals (Example) E. val   =  16 T. val =  2 9 + 5 + 2 E. val   =  14 E. val   =  9 T. val   =  5 F. val =  9 Note: all attributes in this example are of the synthesized type L n print ( 16 ) T. val =  9 F. val =  5 F. val =  5
Attributes ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Synthesized Versus Inherited Attributes ,[object Object],[object Object],[object Object]
Synthesized Versus Inherited Attributes (cont’d) D      T L T     int … L     id L. in   :=  T. type T. type   :=   ‘integer’ … … :=  L. in Production Semantic Rule inherited synthesized
S-Attributed Definitions ,[object Object],[object Object],[object Object]
Example Attribute Grammar in Yacc %token DIGIT %% L : E ‘’ { printf(“%d”, $1); }   ; E : E ‘+’ T { $$ = $1 + $3; }   | T { $$ = $1; }   ; T : T ‘*’ F { $$ = $1 * $3; }   | F { $$ = $1; }   ; F : ‘(’ E ‘)’ { $$ = $2; }   | DIGIT { $$ = $1; }   ; %% Synthesized attribute of parent node  F
Bottom-up Evaluation of S-Attributed Definitions in Yacc Stack $ $   3 $   F $   T $   T  * $   T   *   5 $   T   *   F $   T $   E $  E   + $  E   +   4 $  E   +   F $  E   +   T $   E $  E   n $  L Input 3*5+4n$ *5+4n$ *5+4n$ *5+4n$ 5+4n$ +4n$ +4n$ +4n$  +4n$  4n$ n$ n$ n$ n$ $ $ Action shift reduce  F      digit reduce  T      F shift shift reduce  F      digit   reduce  T      T   *   F reduce  E      T   shift shift reduce  F      digit   reduce  T      F   reduce  E      E   +   T   shift reduce  L      E   n accept val _ 3 3 3 3 _ 3 _ 5 3 _ 5 15 15 15 _ 15 _ 4 15 _ 4 15 _ 4 19 19 _ 19 Semantic Rule $$ = $1 $$ = $1 $$ = $1 $$ = $1 * $3 $$ = $1 $$ = $1 $$ = $1 $$ = $1 + $3 print  $1
Example Attribute Grammar with Synthesized+Inherited Attributes D      T L T     int T     real L     L 1   ,   id L     id L. in   :=  T. type T. type   :=  ‘ integer ’ T. type   :=  ‘ real ’  L 1 . in   :=  L. in;  addtype ( id .entry,  L .in) addtype ( id .entry,  L .in) Production Semantic Rule Synthesized: T .type,  id .entry Inherited: L .in
Acyclic Dependency Graphs for Attributed Parse Trees A      X Y A .a :=  f ( X .x,  Y .y) X .x :=  f ( A .a,  Y .y) Y .y :=  f ( A .a,  X .x) A .a X .x Y .y A .a X .x Y .y A .a X .x Y .y Direction of value dependence
Dependency Graphs with Cycles? ,[object Object],[object Object],A .a :=  f ( X .x)   X .x :=  f ( Y .y) Y .y :=  f ( A .a) A .a X .x Y .y Error: cyclic dependence
Example Annotated Parse Tree D T .type = ‘real’ L .in = ‘real’ L .in = ‘real’ L .in = ‘real’ id 2 .entry id 1 .entry id 3 .entry real , ,
Example Annotated Parse Tree with Dependency Graph D T .type = ‘real’ L .in = ‘real’ L .in = ‘real’ L .in = ‘real’ id 2 .entry id 1 .entry id 3 .entry real , ,
Evaluation Order ,[object Object],[object Object]
Example Parse Tree with Topologically Sorted Actions D T 1 .type = ‘real’ L 1 .in = ‘real’ L 2 .in = ‘real’ L 3 .in = ‘real’ id 2 .entry id 1 .entry id 3 .entry real , , 1 2 3 4 5 6 7 8 9 10 Topological sort: 1.  Get  id 1 .entry 2.  Get  id 2 .entry 3.  Get  id 3 .entry 4.   T 1 .type=‘real’ 5.  L 1 .in= T 1 .type 6.  addtype ( id 3 .entry,  L 1 .in) 7.  L 2 .in= L 1 .in 8.  addtype ( id 2 .entry,  L 2 .in) 9.  L 3 .in= L 2 .in 10.  addtype ( id 1 .entry,  L 3 .in)
Evaluation Methods ,[object Object],[object Object],[object Object]
L-Attributed Definitions ,[object Object],[object Object],[object Object],[object Object],A .a X 1 .x X 2 .x Shown: dependences of inherited attributes
L-Attributed Definitions (cont’d) ,[object Object],[object Object],A      X Y X .i :=  A .i Y .i :=  X .s A .s :=  Y .s A X Y Y .i:= X .s X .i:= A .i A .s:= Y .s
Using Translation Schemes for L-Attributed Definitions D      T L T     int T     real L     L 1   ,   id L     id L. in   :=  T. type T. type   :=  ‘ integer ’ T. type   :=  ‘ real ’  L 1 . in   :=  L. in;  addtype ( id .entry,  L .in) addtype ( id .entry,  L .in) Production Semantic Rule D      T  {  L .in :=  T .type }  L T     int  {  T .type := ‘integer’ } T     real  {  T .type := ‘real’ }   L    {  L 1 .in :=  L .in }  L 1   ,   id  {  addtype ( id .entry,  L .in) } L     id  {  addtype ( id .entry,  L .in) } Translation Scheme
Implementing L-Attributed Definitions in Top-Down Parsers D      T  {  L .in :=  T .type }  L T     int  {  T .type := ‘integer’ } T     real  {  T .type := ‘real’ } void D() { Type Ttype = T();   Type Lin = Ttype;   L(Lin); } Type T() { Type Ttype;   if (lookahead == INT)   { Ttype = TYPE_INT;   match(INT);   } else if (lookahead == REAL)   { Ttype = TYPE_REAL;   match(REAL);   } else error();   return Ttype; } void L(Type Lin) { … } Attributes in L-attributed definitions implemented in translation schemes are passed as arguments to procedures (synthesized) or returned (inherited) Input: inherited attribute Output: synthesized attribute
Implementing L-Attributed Definitions in Bottom-Up Parsers ,[object Object],[object Object],[object Object]
Emulating the Evaluation of L-Attributed Definitions in Yacc D      T  {  L .in :=  T .type }  L T     int  {  T .type := ‘integer’ } T     real  {  T .type := ‘real’ }   L    {  L 1 .in :=  L .in }  L 1   ,   id   {  addtype ( id .entry,  L .in) } L     id  {  addtype ( id .entry,  L .in) } %{ Type Lin; /* global variable */ %} %% D  : Ts L   ; Ts : T  { Lin = $1; }   ; T  : INT  { $$ = TYPE_INT; }   | REAL  { $$ = TYPE_REAL; }   ; L  : L ‘,’ ID { addtype($3, Lin);}   | ID  { addtype($1, Lin);}   ; %%
Rewriting a Grammar to Avoid Inherited Attributes D      id   L T     int T     real L     ,   id  L 1   L     :  T addtype ( id .entry,  L .type) T. type   :=  ‘ integer ’ T. type   :=  ‘ real ’  addtype ( id .entry,  L .type)   L. type   :=  T. type Production Semantic Rule D      L  :  T T     int T     real L     L 1   ,   id L     id Production D T : id,id, … D id,id,… :  T id int int

Más contenido relacionado

La actualidad más candente

Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generationIffat Anjum
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
Lecture 21 problem reduction search ao star search
Lecture 21 problem reduction search ao star searchLecture 21 problem reduction search ao star search
Lecture 21 problem reduction search ao star searchHema Kashyap
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler DesignKuppusamy P
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dagsindhu mathi
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)swapnac12
 
Three Address code
Three Address code Three Address code
Three Address code Pooja Dixit
 
Theory of Computation Grammar Concepts and Problems
Theory of Computation Grammar Concepts and ProblemsTheory of Computation Grammar Concepts and Problems
Theory of Computation Grammar Concepts and ProblemsRushabh2428
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityAakash Singh
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Typesk v
 
State Space Representation and Search
State Space Representation and SearchState Space Representation and Search
State Space Representation and SearchHitesh Mohapatra
 
Finite Automata in compiler design
Finite Automata in compiler designFinite Automata in compiler design
Finite Automata in compiler designRiazul Islam
 

La actualidad más candente (20)

Run time storage
Run time storageRun time storage
Run time storage
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Lecture 21 problem reduction search ao star search
Lecture 21 problem reduction search ao star searchLecture 21 problem reduction search ao star search
Lecture 21 problem reduction search ao star search
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler Design
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dags
 
Code generation
Code generationCode generation
Code generation
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
Three Address code
Three Address code Three Address code
Three Address code
 
Time complexity
Time complexityTime complexity
Time complexity
 
Theory of Computation Grammar Concepts and Problems
Theory of Computation Grammar Concepts and ProblemsTheory of Computation Grammar Concepts and Problems
Theory of Computation Grammar Concepts and Problems
 
COMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time EnvironmentsCOMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time Environments
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Type Conversion, Precedence and Associativity
Type Conversion, Precedence and AssociativityType Conversion, Precedence and Associativity
Type Conversion, Precedence and Associativity
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Concept Of C++ Data Types
Concept Of C++ Data TypesConcept Of C++ Data Types
Concept Of C++ Data Types
 
State Space Representation and Search
State Space Representation and SearchState Space Representation and Search
State Space Representation and Search
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
Finite Automata in compiler design
Finite Automata in compiler designFinite Automata in compiler design
Finite Automata in compiler design
 

Similar a Ch5a

Chapter -4.pptx
Chapter -4.pptxChapter -4.pptx
Chapter -4.pptxwoldu2
 
Chapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.pptChapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.pptMulugetaGebino
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptJigarThummar1
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptSatyamVerma61
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Sheik Uduman Ali
 
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.pptFamiDan
 
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
 
12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdfSHUJEHASSAN
 
IMP PPT- Python programming fundamentals.pptx
IMP PPT- Python programming fundamentals.pptxIMP PPT- Python programming fundamentals.pptx
IMP PPT- Python programming fundamentals.pptxlemonchoos
 
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.pptvenkatapranaykumarGa
 
Morel, a Functional Query Language
Morel, a Functional Query LanguageMorel, a Functional Query Language
Morel, a Functional Query LanguageJulian Hyde
 

Similar a Ch5a (20)

Ch5b
Ch5bCh5b
Ch5b
 
Ch3
Ch3Ch3
Ch3
 
Chapter -4.pptx
Chapter -4.pptxChapter -4.pptx
Chapter -4.pptx
 
chp2sds.pdfgh
chp2sds.pdfghchp2sds.pdfgh
chp2sds.pdfgh
 
lect-05.pdf
lect-05.pdflect-05.pdf
lect-05.pdf
 
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
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.ppt
 
Ch5b.ppt
Ch5b.pptCh5b.ppt
Ch5b.ppt
 
Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0Let Us Learn Lambda Using C# 3.0
Let Us Learn Lambda Using C# 3.0
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Ch6
Ch6Ch6
Ch6
 
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
 
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...
 
12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdf
 
IMP PPT- Python programming fundamentals.pptx
IMP PPT- Python programming fundamentals.pptxIMP PPT- Python programming fundamentals.pptx
IMP PPT- Python programming fundamentals.pptx
 
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
 
Ch2
Ch2Ch2
Ch2
 
Morel, a Functional Query Language
Morel, a Functional Query LanguageMorel, a Functional Query Language
Morel, a Functional Query Language
 
Ch6.ppt
Ch6.pptCh6.ppt
Ch6.ppt
 

Más de kinnarshah8888 (14)

Yuva Msp All
Yuva Msp AllYuva Msp All
Yuva Msp All
 
Yuva Msp Intro
Yuva Msp IntroYuva Msp Intro
Yuva Msp Intro
 
Ch9c
Ch9cCh9c
Ch9c
 
Ch8a
Ch8aCh8a
Ch8a
 
Ch9b
Ch9bCh9b
Ch9b
 
Ch9a
Ch9aCh9a
Ch9a
 
Ch10
Ch10Ch10
Ch10
 
Ch7
Ch7Ch7
Ch7
 
Ch2
Ch2Ch2
Ch2
 
Ch4b
Ch4bCh4b
Ch4b
 
Ch4a
Ch4aCh4a
Ch4a
 
Ch8b
Ch8bCh8b
Ch8b
 
Ch4c
Ch4cCh4c
Ch4c
 
Ch1
Ch1Ch1
Ch1
 

Último

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 

Último (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 

Ch5a

  • 1. Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
  • 2. The Structure of our Compiler Revisited Lexical analyzer Syntax-directed translator Character stream Token stream Java bytecode Yacc specification with semantic rules JVM specification Lex specification
  • 3.
  • 4. Example Attribute Grammar L  E n E  E 1 + T E  T T  T 1 * F T  F F  ( E ) F  digit print ( E. val) E. val := E 1 . val + T. val E. val := T. val T. val := T 1 . val * F. val T. val := F. val F. val := E. val F. val := digit .lexval Production Semantic Rule Note: all attributes in this example are of the synthesized type
  • 5. Example Annotated Parse Tree E. val = 16 T. val = 2 9 + 5 + 2 E. val = 14 E. val = 9 T. val = 5 F. val = 9 Note: all attributes in this example are of the synthesized type L n T. val = 9 F. val = 5 F. val = 5
  • 6. Annotating a Parse Tree With Depth-First Traversals procedure visit ( n : node ); begin for each child m of n , from left to right do visit ( m ); evaluate semantic rules at node n end
  • 7. Depth-First Traversals (Example) E. val = 16 T. val = 2 9 + 5 + 2 E. val = 14 E. val = 9 T. val = 5 F. val = 9 Note: all attributes in this example are of the synthesized type L n print ( 16 ) T. val = 9 F. val = 5 F. val = 5
  • 8.
  • 9.
  • 10. Synthesized Versus Inherited Attributes (cont’d) D  T L T  int … L  id L. in := T. type T. type := ‘integer’ … … := L. in Production Semantic Rule inherited synthesized
  • 11.
  • 12. Example Attribute Grammar in Yacc %token DIGIT %% L : E ‘’ { printf(“%d”, $1); } ; E : E ‘+’ T { $$ = $1 + $3; } | T { $$ = $1; } ; T : T ‘*’ F { $$ = $1 * $3; } | F { $$ = $1; } ; F : ‘(’ E ‘)’ { $$ = $2; } | DIGIT { $$ = $1; } ; %% Synthesized attribute of parent node F
  • 13. Bottom-up Evaluation of S-Attributed Definitions in Yacc Stack $ $ 3 $ F $ T $ T * $ T * 5 $ T * F $ T $ E $ E + $ E + 4 $ E + F $ E + T $ E $ E n $ L Input 3*5+4n$ *5+4n$ *5+4n$ *5+4n$ 5+4n$ +4n$ +4n$ +4n$ +4n$ 4n$ n$ n$ n$ n$ $ $ Action shift reduce F  digit reduce T  F shift shift reduce F  digit reduce T  T * F reduce E  T shift shift reduce F  digit reduce T  F reduce E  E + T shift reduce L  E n accept val _ 3 3 3 3 _ 3 _ 5 3 _ 5 15 15 15 _ 15 _ 4 15 _ 4 15 _ 4 19 19 _ 19 Semantic Rule $$ = $1 $$ = $1 $$ = $1 $$ = $1 * $3 $$ = $1 $$ = $1 $$ = $1 $$ = $1 + $3 print $1
  • 14. Example Attribute Grammar with Synthesized+Inherited Attributes D  T L T  int T  real L  L 1 , id L  id L. in := T. type T. type := ‘ integer ’ T. type := ‘ real ’ L 1 . in := L. in; addtype ( id .entry, L .in) addtype ( id .entry, L .in) Production Semantic Rule Synthesized: T .type, id .entry Inherited: L .in
  • 15. Acyclic Dependency Graphs for Attributed Parse Trees A  X Y A .a := f ( X .x, Y .y) X .x := f ( A .a, Y .y) Y .y := f ( A .a, X .x) A .a X .x Y .y A .a X .x Y .y A .a X .x Y .y Direction of value dependence
  • 16.
  • 17. Example Annotated Parse Tree D T .type = ‘real’ L .in = ‘real’ L .in = ‘real’ L .in = ‘real’ id 2 .entry id 1 .entry id 3 .entry real , ,
  • 18. Example Annotated Parse Tree with Dependency Graph D T .type = ‘real’ L .in = ‘real’ L .in = ‘real’ L .in = ‘real’ id 2 .entry id 1 .entry id 3 .entry real , ,
  • 19.
  • 20. Example Parse Tree with Topologically Sorted Actions D T 1 .type = ‘real’ L 1 .in = ‘real’ L 2 .in = ‘real’ L 3 .in = ‘real’ id 2 .entry id 1 .entry id 3 .entry real , , 1 2 3 4 5 6 7 8 9 10 Topological sort: 1. Get id 1 .entry 2. Get id 2 .entry 3. Get id 3 .entry 4. T 1 .type=‘real’ 5. L 1 .in= T 1 .type 6. addtype ( id 3 .entry, L 1 .in) 7. L 2 .in= L 1 .in 8. addtype ( id 2 .entry, L 2 .in) 9. L 3 .in= L 2 .in 10. addtype ( id 1 .entry, L 3 .in)
  • 21.
  • 22.
  • 23.
  • 24. Using Translation Schemes for L-Attributed Definitions D  T L T  int T  real L  L 1 , id L  id L. in := T. type T. type := ‘ integer ’ T. type := ‘ real ’ L 1 . in := L. in; addtype ( id .entry, L .in) addtype ( id .entry, L .in) Production Semantic Rule D  T { L .in := T .type } L T  int { T .type := ‘integer’ } T  real { T .type := ‘real’ } L  { L 1 .in := L .in } L 1 , id { addtype ( id .entry, L .in) } L  id { addtype ( id .entry, L .in) } Translation Scheme
  • 25. Implementing L-Attributed Definitions in Top-Down Parsers D  T { L .in := T .type } L T  int { T .type := ‘integer’ } T  real { T .type := ‘real’ } void D() { Type Ttype = T(); Type Lin = Ttype; L(Lin); } Type T() { Type Ttype; if (lookahead == INT) { Ttype = TYPE_INT; match(INT); } else if (lookahead == REAL) { Ttype = TYPE_REAL; match(REAL); } else error(); return Ttype; } void L(Type Lin) { … } Attributes in L-attributed definitions implemented in translation schemes are passed as arguments to procedures (synthesized) or returned (inherited) Input: inherited attribute Output: synthesized attribute
  • 26.
  • 27. Emulating the Evaluation of L-Attributed Definitions in Yacc D  T { L .in := T .type } L T  int { T .type := ‘integer’ } T  real { T .type := ‘real’ } L  { L 1 .in := L .in } L 1 , id { addtype ( id .entry, L .in) } L  id { addtype ( id .entry, L .in) } %{ Type Lin; /* global variable */ %} %% D : Ts L ; Ts : T { Lin = $1; } ; T : INT { $$ = TYPE_INT; } | REAL { $$ = TYPE_REAL; } ; L : L ‘,’ ID { addtype($3, Lin);} | ID { addtype($1, Lin);} ; %%
  • 28. Rewriting a Grammar to Avoid Inherited Attributes D  id L T  int T  real L  , id L 1 L  : T addtype ( id .entry, L .type) T. type := ‘ integer ’ T. type := ‘ real ’ addtype ( id .entry, L .type) L. type := T. type Production Semantic Rule D  L : T T  int T  real L  L 1 , id L  id Production D T : id,id, … D id,id,… : T id int int