Lecture 16 Chapter 5 Syntax Directed Translation SyntaxDirected

  • Slides: 9
Download presentation
Lecture # 16 Chapter # 5: Syntax Directed Translation

Lecture # 16 Chapter # 5: Syntax Directed Translation

Syntax-Directed Translation • Uses a CFG to specify the syntactic structure of the language

Syntax-Directed Translation • Uses a CFG to specify the syntactic structure of the language • It associates a set of attributes with the terminals and nonterminals of the grammar • It associates with each production a set of semantic rules to compute values of attributes • A parse tree is traversed and semantic rules applied: after the computations are completed the attributes contain the translated form of the input 2

Synthesized and Inherited Attributes • An attribute is said to be … – synthesized

Synthesized and Inherited Attributes • An attribute is said to be … – synthesized if its value at a parse-tree node is determined from the attribute values at the children of the node – inherited if its value at a parse-tree node is determined by the parent (by enforcing the parent’s semantic rules) 3

Syntax-Directed Definitions • A syntax-directed definition (or attribute grammar) binds a set of semantic

Syntax-Directed Definitions • A syntax-directed definition (or attribute grammar) binds a set of semantic rules to productions • Terminals and nonterminals have attributes holding values set by the semantic rules • A depth-first traversal algorithm traverses the parse tree thereby executing semantic rules to assign attribute values • After the traversal is complete the attributes contain the translated form of the input 4

Example Attribute Grammar Production Semantic Rule L En E E 1 + T E

Example Attribute Grammar Production Semantic Rule L En 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 1. val * F. val T. val : = F. val : = E. val F. val : = digit. lexval Note: all attributes in this example are of 5 the synthesized type

Depth-First Traversals (Example) L print(16) E. val = 16 E. val = 14 T.

Depth-First Traversals (Example) L print(16) E. val = 16 E. val = 14 T. val = 2 E. val = 9 T. val = 5 T. val = 9 F. val = 5 F. val = 9 9 + 5 + 2 n Note: all attributes in this example are of 6 the synthesized type

Example Attribute Grammar String concat operator Production Semantic Rule expr 1 + term expr

Example Attribute Grammar String concat operator Production Semantic Rule expr 1 + term expr 1 - term expr term 0 term 1 … term 9 expr. t : = expr 1. t // term. t // “+” expr. t : = expr 1. t // term. t // “-” expr. t : = term. t : = “ 0” term. t : = “ 1” … term. t : = “ 9” 7

Example Annotated Parse Tree expr. t = “ 95 -2+” expr. t = “

Example Annotated Parse Tree expr. t = “ 95 -2+” expr. t = “ 95 -” expr. t = “ 9” term. t = “ 2” term. t = “ 5” term. t = “ 9” 9 - 5 + 2 8

Depth-First Traversals procedure visit(n : node); begin for each child m of n, from

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 9