SlideShare una empresa de Scribd logo
1 de 15
Syntax-Directed Translation Part II Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
Translation Schemes using Marker Nonterminals S      if   E   M   then   S  { backpatch( M .loc, pc- M .loc) } M         { emit( iconst_0 );  M .loc := pc; emit( if_icmpeq , 0) } S      if   E  { emit( iconst_0 ); push(pc); emit( if_icmpeq , 0) }   then   S  { backpatch(top(), pc-top()); pop() } Need a stack! (for nested if-then) Synthesized attribute (automatically stacked) Insert marker nonterminal
Translation Schemes using Marker Nonterminals in Yacc  S : IF E M THEN S { backpatch($3, pc-$3); }   ; M : /* empty */  { emit(iconst_0);   $$ = pc;   emit3(if_icmpeq, 0);   }   ; …
Replacing Inherited Attributes with Synthesized Lists D      T L  { for all  id      L .list :  addtype ( id .entry,  T .type) } T     int  {  T .type := ‘integer’ } T     real  {  T .type := ‘real’ } L     L 1   ,   id  {  L .list :=  L 1 .list + [ id ] } L     id  {  L .list := [ id ] } D T .type = ‘real’ L .list = [ id 1 , id 2 , id 3 ] L .list = [ id 1 , id 2 ] L .list = [ id 1 ] id 2 .entry id 1 .entry id 3 .entry real , ,
Replacing Inherited Attributes with Synthesized Lists in Yacc %{ typedef struct List { Symbol *entry;   struct List *next; } List; %} %union { int type; List *list; Symbol *sym; } %token <sym> ID %type <list> L %type <type> T %% D : T L  { List *p;   for (p = $2; p; p = p->next) addtype(p->entry, $1); } ; T : INT  { $$ = TYPE_INT; } | REAL { $$ = TYPE_REAL; } ; L : L ‘,’ ID    { $$ = malloc(sizeof(List)); $$->entry = $3;   $$->next = $1; } | ID  { $$ = malloc(sizeof(List)); $$->entry = $1;   $$->next = NULL;   } ;
Concrete and Abstract Syntax Trees ,[object Object],[object Object],E + E T id id id * Concrete syntax tree + * id id id Abstract syntax tree T T
Generating Abstract Syntax Trees E .nptr + E .nptr T .nptr id id id * T .nptr T .nptr + id * id id Synthesize AST
S-Attributed Definitions for Generating Abstract Syntax Trees Production E      E 1   +   T E      E 1   -   T E      T T      T 1   * id T      T 1   /   id T      id Semantic Rule E .nptr := mknode(‘+’,  E 1 .nptr,  T .nptr) E .nptr := mknode(‘-’,  E 1 .nptr,  T .nptr) E .nptr :=  T .nptr T .nptr := mknode(‘*’,  T 1 .nptr, mkleaf( id ,  id .entry)) T .nptr := mknode(‘/’,  T 1 .nptr, mkleaf( id ,  id .entry)) T .nptr := mkleaf( id ,  id .entry)
Generating Abstract Syntax Trees with Yacc %{ typedef struct Node { int op;  /* node op */   Symbol *entry; /* leaf */   struct Node *left, *right; } Node; %} %union { Node *node; Symbol *sym; } %token <sym> ID %type <node> E T F %% E : E ‘+’ T  { $$ = mknode(‘+’, $1, $3); }   | E ‘-’ T  { $$ = mknode(‘-’, $1, $3); }   | T  { $$ = $1; }   ; T : T ‘*’ F  { $$ = mknode(‘*’, $1, $3); }   | T ‘/’ F  { $$ = mknode(‘/’, $1, $3); }    | F  { $$ = $1; }   ; F : ‘(’ E ‘)’ { $$ = $2; }   | ID  { $$ = mkleaf($1); }   ; %%
Eliminating Left Recursion from a Translation Scheme A      A 1   Y {  A .a :=  g ( A 1 .a,  Y .y) } A      X {  A .a :=  f ( X .x) } A      X   {  R .i :=  f ( X .x) }  R  {  A .a :=  R .s } R      Y   {  R 1 .i :=  g ( R .i,  Y .y) }  R 1   {  R .s :=  R 1 .s }  R         {  R .s :=  R .i }
Eliminating Left Recursion from a Translation Scheme (cont’d) A .a =  g ( g ( f ( X .x),  Y 1 .y),  Y 2 .y) Y 2 Y 1 X A .a =  g ( f ( X .x),  Y 1 .y) A .a =  f ( X .x)
Eliminating Left Recursion from a Translation Scheme (cont’d) R 3 .i =  g ( g ( f ( X .x),  Y 1 .y),  Y 2 .y) Y 2 Y 1 X R 2 .i =  g ( f ( X .x),  Y 1 .y) R 1 .i =  f ( X .x) A  1. Flow of inherited attribute values
Eliminating Left Recursion from a Translation Scheme (cont’d) R 3 .s =  R 3 .i =  g ( g ( f ( X .x),  Y 1 .y),  Y 2 .y) Y 2 Y 1 X R 2 .s =  R 3 .s =  g ( g ( f ( X .x),  Y 1 .y),  Y 2 .y) R 1 .s =  R 2 .s =  g ( g ( f ( X .x),  Y 1 .y),  Y 2 .y) A .s =  R 1 .s =  g ( g ( f ( X .x),  Y 1 .y),  Y 2 .y)  2. Flow of synthesized attribute values
Generating Abstract Syntax Trees with Predictive Parsers E      T  {  R .i :=  T .nptr }  R  {  E .nptr :=  R .s } R      +   T  { R 1 .i := mknode(‘+’,  R .i,  T .nptr) }  R 1  {  R .s :=  R 1 .s } R      -   T  { R 1 .i := mknode(‘-’,  R .i,  T .nptr) }  R 1  {  R .s :=  R 1 .s } R        {  R .s :=  R .i } T      id  { T.nptr := mkleaf( id ,  id .entry) } E      E 1   +   T  {  E .nptr := mknode(‘+’,  E 1 .nptr,  T .nptr) } E      E 1   -   T  {  E .nptr := mknode(‘-’,  E 1 .nptr,  T .nptr) } E      T  {  E .nptr :=  T .nptr } T      id  { T.nptr := mkleaf( id ,  id .entry) }
Generating Abstract Syntax Trees with Predictive Parsers (cont’d) Node *R(Node *i) { Node *s, *i1; if (lookahead == ‘+’)   { match(‘+’);   s = T();   i1 = mknode(‘+’, i, s); s = R(i1);   } else if (lookahead == ‘-’)   { …   } else   s = i;   return s; }

Más contenido relacionado

La actualidad más candente

Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
Shashwat Shriparv
 

La actualidad más candente (19)

Left factor put
Left factor putLeft factor put
Left factor put
 
Sdt
SdtSdt
Sdt
 
Syntaxdirected
SyntaxdirectedSyntaxdirected
Syntaxdirected
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
 
Unit iv-syntax-directed-translation
Unit iv-syntax-directed-translationUnit iv-syntax-directed-translation
Unit iv-syntax-directed-translation
 
Compiler Design QA
Compiler Design QACompiler Design QA
Compiler Design QA
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Ch4a
Ch4aCh4a
Ch4a
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Back patching
Back patchingBack patching
Back patching
 

Destacado

Compiler Design
Compiler DesignCompiler Design
Compiler Design
Mir Majid
 
Use case diagrams
Use case diagramsUse case diagrams
Use case diagrams
Mir Majid
 
Discrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IDiscrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part I
Wongyos Keardsri
 
Graph theory 1
Graph theory 1Graph theory 1
Graph theory 1
Tech_MX
 
introduction to graph theory
introduction to graph theoryintroduction to graph theory
introduction to graph theory
Chuckie Balbuena
 
Graphs - CH10 - Discrete Mathematics
Graphs - CH10 - Discrete MathematicsGraphs - CH10 - Discrete Mathematics
Graphs - CH10 - Discrete Mathematics
Omnia A. Abdullah
 

Destacado (20)

Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Dependency Parsing Algorithms Analysis - Major Project
Dependency Parsing Algorithms Analysis - Major Project Dependency Parsing Algorithms Analysis - Major Project
Dependency Parsing Algorithms Analysis - Major Project
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Use case diagrams
Use case diagramsUse case diagrams
Use case diagrams
 
Discrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part IDiscrete-Chapter 11 Graphs Part I
Discrete-Chapter 11 Graphs Part I
 
Discrete Mathematics & Its Applications (Graphs)
Discrete Mathematics & Its Applications (Graphs)Discrete Mathematics & Its Applications (Graphs)
Discrete Mathematics & Its Applications (Graphs)
 
An Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and RuntimeAn Introduction to Java Compiler and Runtime
An Introduction to Java Compiler and Runtime
 
Syntaxdirected (1)
Syntaxdirected (1)Syntaxdirected (1)
Syntaxdirected (1)
 
Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3
 
Syntax
SyntaxSyntax
Syntax
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
Syntax directed-translation
Syntax directed-translationSyntax directed-translation
Syntax directed-translation
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
Graph theory 1
Graph theory 1Graph theory 1
Graph theory 1
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
Relations in Discrete Math
Relations in Discrete MathRelations in Discrete Math
Relations in Discrete Math
 
Sorting Things Out: An Introduction to Card Sorting
Sorting Things Out: An Introduction to Card SortingSorting Things Out: An Introduction to Card Sorting
Sorting Things Out: An Introduction to Card Sorting
 
introduction to graph theory
introduction to graph theoryintroduction to graph theory
introduction to graph theory
 
Graphs - CH10 - Discrete Mathematics
Graphs - CH10 - Discrete MathematicsGraphs - CH10 - Discrete Mathematics
Graphs - CH10 - Discrete Mathematics
 
Code generation
Code generationCode generation
Code generation
 

Similar a Ch5b

Top down and botttom up 2 LATEST.
Top down     and botttom up 2 LATEST.Top down     and botttom up 2 LATEST.
Top down and botttom up 2 LATEST.
Gerwin Ocsena
 
6-Practice Problems - LL(1) parser-16-05-2023.pptx
6-Practice Problems - LL(1) parser-16-05-2023.pptx6-Practice Problems - LL(1) parser-16-05-2023.pptx
6-Practice Problems - LL(1) parser-16-05-2023.pptx
venkatapranaykumarGa
 

Similar a Ch5b (20)

Ch5b.ppt
Ch5b.pptCh5b.ppt
Ch5b.ppt
 
Ch8b
Ch8bCh8b
Ch8b
 
Ch8a
Ch8aCh8a
Ch8a
 
Top down and botttom up 2 LATEST.
Top down     and botttom up 2 LATEST.Top down     and botttom up 2 LATEST.
Top down and botttom up 2 LATEST.
 
ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)ALF 5 - Parser Top-Down (2018)
ALF 5 - Parser Top-Down (2018)
 
Explicit Formula for Riemann Prime Counting Function
Explicit Formula for Riemann Prime Counting FunctionExplicit Formula for Riemann Prime Counting Function
Explicit Formula for Riemann Prime Counting Function
 
Chap05alg
Chap05algChap05alg
Chap05alg
 
Chap05alg
Chap05algChap05alg
Chap05alg
 
ALF 5 - Parser Top-Down
ALF 5 - Parser Top-DownALF 5 - Parser Top-Down
ALF 5 - Parser Top-Down
 
Monadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query ExpressionsMonadic Comprehensions and Functional Composition with Query Expressions
Monadic Comprehensions and Functional Composition with Query Expressions
 
Futzing with actors (etc.)
Futzing with actors (etc.)Futzing with actors (etc.)
Futzing with actors (etc.)
 
Frsa
FrsaFrsa
Frsa
 
6-Practice Problems - LL(1) parser-16-05-2023.pptx
6-Practice Problems - LL(1) parser-16-05-2023.pptx6-Practice Problems - LL(1) parser-16-05-2023.pptx
6-Practice Problems - LL(1) parser-16-05-2023.pptx
 
Revision Tour 1 and 2 complete.doc
Revision Tour 1 and 2 complete.docRevision Tour 1 and 2 complete.doc
Revision Tour 1 and 2 complete.doc
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
 
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
 
Chap09alg
Chap09algChap09alg
Chap09alg
 
Chap09alg
Chap09algChap09alg
Chap09alg
 
Climbing the Abstract Syntax Tree (PHP UK 2018)
Climbing the Abstract Syntax Tree (PHP UK 2018)Climbing the Abstract Syntax Tree (PHP UK 2018)
Climbing the Abstract Syntax Tree (PHP UK 2018)
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 

Más de kinnarshah8888 (13)

Yuva Msp All
Yuva Msp AllYuva Msp All
Yuva Msp All
 
Yuva Msp Intro
Yuva Msp IntroYuva Msp Intro
Yuva Msp Intro
 
Ch6
Ch6Ch6
Ch6
 
Ch9c
Ch9cCh9c
Ch9c
 
Ch9b
Ch9bCh9b
Ch9b
 
Ch9a
Ch9aCh9a
Ch9a
 
Ch10
Ch10Ch10
Ch10
 
Ch7
Ch7Ch7
Ch7
 
Ch3
Ch3Ch3
Ch3
 
Ch2
Ch2Ch2
Ch2
 
Ch4b
Ch4bCh4b
Ch4b
 
Ch4c
Ch4cCh4c
Ch4c
 
Ch1
Ch1Ch1
Ch1
 

Último

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
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

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
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
"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 ...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
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
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
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
 
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
 
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...
 
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
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
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
 
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 ...
 
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...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 

Ch5b

  • 1. Syntax-Directed Translation Part II Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
  • 2. Translation Schemes using Marker Nonterminals S  if E M then S { backpatch( M .loc, pc- M .loc) } M   { emit( iconst_0 ); M .loc := pc; emit( if_icmpeq , 0) } S  if E { emit( iconst_0 ); push(pc); emit( if_icmpeq , 0) } then S { backpatch(top(), pc-top()); pop() } Need a stack! (for nested if-then) Synthesized attribute (automatically stacked) Insert marker nonterminal
  • 3. Translation Schemes using Marker Nonterminals in Yacc S : IF E M THEN S { backpatch($3, pc-$3); } ; M : /* empty */ { emit(iconst_0); $$ = pc; emit3(if_icmpeq, 0); } ; …
  • 4. Replacing Inherited Attributes with Synthesized Lists D  T L { for all id  L .list : addtype ( id .entry, T .type) } T  int { T .type := ‘integer’ } T  real { T .type := ‘real’ } L  L 1 , id { L .list := L 1 .list + [ id ] } L  id { L .list := [ id ] } D T .type = ‘real’ L .list = [ id 1 , id 2 , id 3 ] L .list = [ id 1 , id 2 ] L .list = [ id 1 ] id 2 .entry id 1 .entry id 3 .entry real , ,
  • 5. Replacing Inherited Attributes with Synthesized Lists in Yacc %{ typedef struct List { Symbol *entry; struct List *next; } List; %} %union { int type; List *list; Symbol *sym; } %token <sym> ID %type <list> L %type <type> T %% D : T L { List *p; for (p = $2; p; p = p->next) addtype(p->entry, $1); } ; T : INT { $$ = TYPE_INT; } | REAL { $$ = TYPE_REAL; } ; L : L ‘,’ ID { $$ = malloc(sizeof(List)); $$->entry = $3; $$->next = $1; } | ID { $$ = malloc(sizeof(List)); $$->entry = $1; $$->next = NULL; } ;
  • 6.
  • 7. Generating Abstract Syntax Trees E .nptr + E .nptr T .nptr id id id * T .nptr T .nptr + id * id id Synthesize AST
  • 8. S-Attributed Definitions for Generating Abstract Syntax Trees Production E  E 1 + T E  E 1 - T E  T T  T 1 * id T  T 1 / id T  id Semantic Rule E .nptr := mknode(‘+’, E 1 .nptr, T .nptr) E .nptr := mknode(‘-’, E 1 .nptr, T .nptr) E .nptr := T .nptr T .nptr := mknode(‘*’, T 1 .nptr, mkleaf( id , id .entry)) T .nptr := mknode(‘/’, T 1 .nptr, mkleaf( id , id .entry)) T .nptr := mkleaf( id , id .entry)
  • 9. Generating Abstract Syntax Trees with Yacc %{ typedef struct Node { int op; /* node op */ Symbol *entry; /* leaf */ struct Node *left, *right; } Node; %} %union { Node *node; Symbol *sym; } %token <sym> ID %type <node> E T F %% E : E ‘+’ T { $$ = mknode(‘+’, $1, $3); } | E ‘-’ T { $$ = mknode(‘-’, $1, $3); } | T { $$ = $1; } ; T : T ‘*’ F { $$ = mknode(‘*’, $1, $3); } | T ‘/’ F { $$ = mknode(‘/’, $1, $3); } | F { $$ = $1; } ; F : ‘(’ E ‘)’ { $$ = $2; } | ID { $$ = mkleaf($1); } ; %%
  • 10. Eliminating Left Recursion from a Translation Scheme A  A 1 Y { A .a := g ( A 1 .a, Y .y) } A  X { A .a := f ( X .x) } A  X { R .i := f ( X .x) } R { A .a := R .s } R  Y { R 1 .i := g ( R .i, Y .y) } R 1 { R .s := R 1 .s } R   { R .s := R .i }
  • 11. Eliminating Left Recursion from a Translation Scheme (cont’d) A .a = g ( g ( f ( X .x), Y 1 .y), Y 2 .y) Y 2 Y 1 X A .a = g ( f ( X .x), Y 1 .y) A .a = f ( X .x)
  • 12. Eliminating Left Recursion from a Translation Scheme (cont’d) R 3 .i = g ( g ( f ( X .x), Y 1 .y), Y 2 .y) Y 2 Y 1 X R 2 .i = g ( f ( X .x), Y 1 .y) R 1 .i = f ( X .x) A  1. Flow of inherited attribute values
  • 13. Eliminating Left Recursion from a Translation Scheme (cont’d) R 3 .s = R 3 .i = g ( g ( f ( X .x), Y 1 .y), Y 2 .y) Y 2 Y 1 X R 2 .s = R 3 .s = g ( g ( f ( X .x), Y 1 .y), Y 2 .y) R 1 .s = R 2 .s = g ( g ( f ( X .x), Y 1 .y), Y 2 .y) A .s = R 1 .s = g ( g ( f ( X .x), Y 1 .y), Y 2 .y)  2. Flow of synthesized attribute values
  • 14. Generating Abstract Syntax Trees with Predictive Parsers E  T { R .i := T .nptr } R { E .nptr := R .s } R  + T { R 1 .i := mknode(‘+’, R .i, T .nptr) } R 1 { R .s := R 1 .s } R  - T { R 1 .i := mknode(‘-’, R .i, T .nptr) } R 1 { R .s := R 1 .s } R   { R .s := R .i } T  id { T.nptr := mkleaf( id , id .entry) } E  E 1 + T { E .nptr := mknode(‘+’, E 1 .nptr, T .nptr) } E  E 1 - T { E .nptr := mknode(‘-’, E 1 .nptr, T .nptr) } E  T { E .nptr := T .nptr } T  id { T.nptr := mkleaf( id , id .entry) }
  • 15. Generating Abstract Syntax Trees with Predictive Parsers (cont’d) Node *R(Node *i) { Node *s, *i1; if (lookahead == ‘+’) { match(‘+’); s = T(); i1 = mknode(‘+’, i, s); s = R(i1); } else if (lookahead == ‘-’) { … } else s = i; return s; }