SlideShare a Scribd company logo
1
Syntax-Directed Translation
Part II
Chapter 5
Copyright Robert van Engelen, Florida State University, 2007
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  L1 , id { L.list := L1.list + [id] }
L  id { L.list := [id] }
D
T.type = ‘real’ L.list = [id1,id2,id3]
L.list = [id1,id2]
L.list = [id1] id2.entry
id1.entry
id3.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
Concrete and Abstract Syntax
Trees
• A parse tree is called a concrete syntax tree
• An abstract syntax tree (AST) is defined by
the compiler writer as a more convenient
intermediate representation
E
+
E T
id
id
id
*
Concrete syntax tree
+
*
id
id id
Abstract syntax tree
T T
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  E1 + T
E  E1 - T
E  T
T  T1 * id
T  T1 / id
T  id
Semantic Rule
E.nptr := mknode(‘+’, E1.nptr, T.nptr)
E.nptr := mknode(‘-’, E1.nptr, T.nptr)
E.nptr := T.nptr
T.nptr := mknode(‘*’, T1.nptr, mkleaf(id, id.entry))
T.nptr := mknode(‘/’, T1.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  A1 Y { A.a := g(A1.a, Y.y) }
A  X { A.a := f(X.x) }
A  X { R.i := f(X.x) } R { A.a := R.s }
R  Y { R1.i := g(R.i, Y.y) } R1 { R.s := R1.s }
R   { R.s := R.i }
11
Eliminating Left Recursion from
a Translation Scheme (cont’d)
A.a = g(g(f(X.x), Y1.y), Y2.y)
Y2
Y1
X
A.a = g(f(X.x), Y1.y)
A.a = f(X.x)
12
Eliminating Left Recursion from
a Translation Scheme (cont’d)
R3.i = g(g(f(X.x), Y1.y), Y2.y)
Y2
Y1
X
R2.i = g(f(X.x), Y1.y)
R1.i = f(X.x)
A

1. Flow of inherited
attribute values
13
Eliminating Left Recursion from
a Translation Scheme (cont’d)
R3.s = R3.i = g(g(f(X.x), Y1.y), Y2.y)
Y2
Y1
X
R2.s = R3.s = g(g(f(X.x), Y1.y), Y2.y)
R1.s = R2.s = g(g(f(X.x), Y1.y), Y2.y)
A.s = R1.s = g(g(f(X.x), Y1.y), Y2.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 {R1.i := mknode(‘+’, R.i, T.nptr) } R1 { R.s := R1.s }
R  - T {R1.i := mknode(‘-’, R.i, T.nptr) } R1 { R.s := R1.s }
R   { R.s := R.i }
T  id { T.nptr := mkleaf(id, id.entry) }
E  E1 + T { E.nptr := mknode(‘+’, E1.nptr, T.nptr) }
E  E1 - T { E.nptr := mknode(‘-’, E1.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;
}

More Related Content

Similar to Ch5b.ppt

Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
Radhakrishnan Chinnusamy
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
ayyankhanna6480086
 
Matlab differential
Matlab differentialMatlab differential
Matlab differential
pramodkumar1804
 
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
Sheik Uduman Ali
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.ppt
JigarThummar1
 
Chapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.pptChapter 5 - Syntax Directed Translation.ppt
Chapter 5 - Syntax Directed Translation.ppt
MulugetaGebino
 
R Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdfR Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdf
Timothy McBush Hiele
 
Implementing Software Machines in Go and C
Implementing Software Machines in Go and CImplementing Software Machines in Go and C
Implementing Software Machines in Go and C
Eleanor McHugh
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
d72994185
 
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
Kannan Nambiar
 
A Dimension Abstraction Approach to Vectorization in Matlab
A Dimension Abstraction Approach to Vectorization in MatlabA Dimension Abstraction Approach to Vectorization in Matlab
A Dimension Abstraction Approach to Vectorization in Matlab
aiQUANT
 
Cgo2007 P3 3 Birkbeck
Cgo2007 P3 3 BirkbeckCgo2007 P3 3 Birkbeck
Cgo2007 P3 3 Birkbeck
aiQUANT
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
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
Chris Eargle
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
SajalFayyaz
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
Shakil Ahmed
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.ppt
SatyamVerma61
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dags
indhu mathi
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
vsssuresh
 

Similar to Ch5b.ppt (20)

Chap05alg
Chap05algChap05alg
Chap05alg
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
Matlab differential
Matlab differentialMatlab differential
Matlab differential
 
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.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
 
R Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdfR Cheat Sheet for Data Analysts and Statisticians.pdf
R Cheat Sheet for Data Analysts and Statisticians.pdf
 
Implementing Software Machines in Go and C
Implementing Software Machines in Go and CImplementing Software Machines in Go and C
Implementing Software Machines in Go and C
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
 
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
 
A Dimension Abstraction Approach to Vectorization in Matlab
A Dimension Abstraction Approach to Vectorization in MatlabA Dimension Abstraction Approach to Vectorization in Matlab
A Dimension Abstraction Approach to Vectorization in Matlab
 
Cgo2007 P3 3 Birkbeck
Cgo2007 P3 3 BirkbeckCgo2007 P3 3 Birkbeck
Cgo2007 P3 3 Birkbeck
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
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
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
 
Advanced data structure
Advanced data structureAdvanced data structure
Advanced data structure
 
Chapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.pptChapter_5_Syntax_Directed_Translation.ppt
Chapter_5_Syntax_Directed_Translation.ppt
 
Generating code from dags
Generating code from dagsGenerating code from dags
Generating code from dags
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 

Recently uploaded

The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
laozhuseo02
 
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdfJAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
Javier Lasa
 
test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
Arif0071
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
nirahealhty
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
3ipehhoa
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
JeyaPerumal1
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
laozhuseo02
 
Comptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guideComptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guide
GTProductions1
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
Gal Baras
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
natyesu
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
JungkooksNonexistent
 
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
keoku
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Brad Spiegel Macon GA
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
Rogerio Filho
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
ufdana
 
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
eutxy
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Sanjeev Rampal
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
3ipehhoa
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
3ipehhoa
 

Recently uploaded (20)

The+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptxThe+Prospects+of+E-Commerce+in+China.pptx
The+Prospects+of+E-Commerce+in+China.pptx
 
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdfJAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
JAVIER LASA-EXPERIENCIA digital 1986-2024.pdf
 
test test test test testtest test testtest test testtest test testtest test ...
test test  test test testtest test testtest test testtest test testtest test ...test test  test test testtest test testtest test testtest test testtest test ...
test test test test testtest test testtest test testtest test testtest test ...
 
This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!This 7-second Brain Wave Ritual Attracts Money To You.!
This 7-second Brain Wave Ritual Attracts Money To You.!
 
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
原版仿制(uob毕业证书)英国伯明翰大学毕业证本科学历证书原版一模一样
 
1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...1.Wireless Communication System_Wireless communication is a broad term that i...
1.Wireless Communication System_Wireless communication is a broad term that i...
 
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shopHistory+of+E-commerce+Development+in+China-www.cfye-commerce.shop
History+of+E-commerce+Development+in+China-www.cfye-commerce.shop
 
Comptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guideComptia N+ Standard Networking lesson guide
Comptia N+ Standard Networking lesson guide
 
How to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptxHow to Use Contact Form 7 Like a Pro.pptx
How to Use Contact Form 7 Like a Pro.pptx
 
BASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptxBASIC C++ lecture NOTE C++ lecture 3.pptx
BASIC C++ lecture NOTE C++ lecture 3.pptx
 
Latest trends in computer networking.pptx
Latest trends in computer networking.pptxLatest trends in computer networking.pptx
Latest trends in computer networking.pptx
 
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
一比一原版(SLU毕业证)圣路易斯大学毕业证成绩单专业办理
 
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptxBridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
Bridging the Digital Gap Brad Spiegel Macon, GA Initiative.pptx
 
guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...guildmasters guide to ravnica Dungeons & Dragons 5...
guildmasters guide to ravnica Dungeons & Dragons 5...
 
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
一比一原版(CSU毕业证)加利福尼亚州立大学毕业证成绩单专业办理
 
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
一比一原版(LBS毕业证)伦敦商学院毕业证成绩单专业办理
 
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and GuidelinesMulti-cluster Kubernetes Networking- Patterns, Projects and Guidelines
Multi-cluster Kubernetes Networking- Patterns, Projects and Guidelines
 
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
急速办(bedfordhire毕业证书)英国贝德福特大学毕业证成绩单原版一模一样
 
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
APNIC Foundation, presented by Ellisha Heppner at the PNG DNS Forum 2024
 
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
1比1复刻(bath毕业证书)英国巴斯大学毕业证学位证原版一模一样
 

Ch5b.ppt

  • 1. 1 Syntax-Directed Translation Part II Chapter 5 Copyright Robert van Engelen, Florida State University, 2007
  • 2. 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. 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. 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  L1 , id { L.list := L1.list + [id] } L  id { L.list := [id] } D T.type = ‘real’ L.list = [id1,id2,id3] L.list = [id1,id2] L.list = [id1] id2.entry id1.entry id3.entry real , ,
  • 5. 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. 6 Concrete and Abstract Syntax Trees • A parse tree is called a concrete syntax tree • An abstract syntax tree (AST) is defined by the compiler writer as a more convenient intermediate representation E + E T id id id * Concrete syntax tree + * id id id Abstract syntax tree T T
  • 7. 7 Generating Abstract Syntax Trees E.nptr + E.nptr T.nptr id id id * T.nptr T.nptr + id * id id Synthesize AST
  • 8. 8 S-Attributed Definitions for Generating Abstract Syntax Trees Production E  E1 + T E  E1 - T E  T T  T1 * id T  T1 / id T  id Semantic Rule E.nptr := mknode(‘+’, E1.nptr, T.nptr) E.nptr := mknode(‘-’, E1.nptr, T.nptr) E.nptr := T.nptr T.nptr := mknode(‘*’, T1.nptr, mkleaf(id, id.entry)) T.nptr := mknode(‘/’, T1.nptr, mkleaf(id, id.entry)) T.nptr := mkleaf(id, id.entry)
  • 9. 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. 10 Eliminating Left Recursion from a Translation Scheme A  A1 Y { A.a := g(A1.a, Y.y) } A  X { A.a := f(X.x) } A  X { R.i := f(X.x) } R { A.a := R.s } R  Y { R1.i := g(R.i, Y.y) } R1 { R.s := R1.s } R   { R.s := R.i }
  • 11. 11 Eliminating Left Recursion from a Translation Scheme (cont’d) A.a = g(g(f(X.x), Y1.y), Y2.y) Y2 Y1 X A.a = g(f(X.x), Y1.y) A.a = f(X.x)
  • 12. 12 Eliminating Left Recursion from a Translation Scheme (cont’d) R3.i = g(g(f(X.x), Y1.y), Y2.y) Y2 Y1 X R2.i = g(f(X.x), Y1.y) R1.i = f(X.x) A  1. Flow of inherited attribute values
  • 13. 13 Eliminating Left Recursion from a Translation Scheme (cont’d) R3.s = R3.i = g(g(f(X.x), Y1.y), Y2.y) Y2 Y1 X R2.s = R3.s = g(g(f(X.x), Y1.y), Y2.y) R1.s = R2.s = g(g(f(X.x), Y1.y), Y2.y) A.s = R1.s = g(g(f(X.x), Y1.y), Y2.y)  2. Flow of synthesized attribute values
  • 14. 14 Generating Abstract Syntax Trees with Predictive Parsers E  T { R.i := T.nptr } R { E.nptr := R.s } R  + T {R1.i := mknode(‘+’, R.i, T.nptr) } R1 { R.s := R1.s } R  - T {R1.i := mknode(‘-’, R.i, T.nptr) } R1 { R.s := R1.s } R   { R.s := R.i } T  id { T.nptr := mkleaf(id, id.entry) } E  E1 + T { E.nptr := mknode(‘+’, E1.nptr, T.nptr) } E  E1 - T { E.nptr := mknode(‘-’, E1.nptr, T.nptr) } E  T { E.nptr := T.nptr } T  id { T.nptr := mkleaf(id, id.entry) }
  • 15. 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; }