Ch5a - 1 ! Syntax-Directed Translation" Part I!...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 The Structure of our Compiler Revisited Lexical analyzer Syntax-directed translator Character stream Token stream Java bytecode Yacc speciFcation with semantic rules JVM speciFcation Lex speciFcation
Background image of page 2
3 Syntax-Directed Defnitions • A syntax-directed defnition (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-frst 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
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Attributes • Attribute values may represent – Numbers (literal constants) – Strings (literal constants) – Memory locations, such as a frame index of a local variable or function argument – A data type for type checking of expressions – Scoping information for local declarations – Intermediate program representations
Background image of page 8
9 Synthesized Versus Inherited Attributes • Given a production A ! " then each semantic rule is of the form b := f ( c 1 , c 2 ,…, c k ) where f is a function and c i are attributes of A and " , and either b is a synthesized attribute of A b is an inherited attribute of one of the grammar symbols in "
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 28

Ch5a - 1 ! Syntax-Directed Translation" Part I!...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online