SlideShare a Scribd company logo
1 of 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; }

More Related Content

What's hot

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

What's hot (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
 

Viewers also liked

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
 

Viewers also liked (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 to 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 to 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
 

More from 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
 

Recently uploaded

Recently uploaded (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
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...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
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...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

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; }