SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
raosir123
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Ehtisham Ali
 

Was ist angesagt? (20)

Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler Design
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Query optimization
Query optimizationQuery optimization
Query optimization
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
Control structures in c++
Control structures in c++Control structures in c++
Control structures in c++
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
DBMS Canonical cover
DBMS Canonical coverDBMS Canonical cover
DBMS Canonical cover
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
Object Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesObject Oriented Programming Lecture Notes
Object Oriented Programming Lecture Notes
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 

Ähnlich wie Ch5a

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
SatyamVerma61
 
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 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
 
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
 

Ähnlich wie 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
 
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
 
relational model.pptx
relational model.pptxrelational model.pptx
relational model.pptx
 

Mehr von 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
 

Kürzlich hochgeladen

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Kürzlich hochgeladen (20)

Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 

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