160_Syntax-Directed_Translation

160_Syntax-Directed_Translation - CS143 Handout 16 Summer...

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

View Full Document Right Arrow Icon
CS143 Handout 16 Summer 2011 July 6 th , 2011 Syntax Directed Translation Handout written by Maggie Johnson and revised by Julie Zelenski. Syntax-directed translation refers to a method of compiler implementation where the source language translation is completely driven by the parser. In other words, the parsing process and parse trees are used to direct semantic analysis and the translation of the source program. This can be a separate phase of a compiler or we can augment our conventional grammar with information to control the semantic analysis and translation. Such grammars are called attribute grammars . We augment a grammar by associating attributes with each grammar symbol that describes its properties. An attribute has a name and an associated value: a string, a number, a type, a memory location, an assigned register—whatever information we need. For example, variables may have an attribute "type" (which records the declared type of a variable, useful later in type-checking) or an integer constant may have an attribute "value" (which we will later need to generate code). With each production in a grammar, we give semantic rules or actions , which describe how to compute the attribute values associated with each grammar symbol in a production. The attribute value for a parse node may depend on information from its children nodes below or its siblings and parent node above. Consider this production, augmented with a set of actions that use the "value" attribute for a digit node to store the appropriate numeric value. Below, we use the syntax X.a to refer to the attribute a associated with symbol X . digit –> 0 {digit.value = 0} | 1 {digit.value = 1} | 2 {digit.value = 2} ... | 9 {digit.value = 9} Attributes may be passed up a parse tree to be used by other productions: int 1 –> digit {int 1 .value = digit.value} | int 2 digit {int 1 .value = int 2 .value*10 + digit.value} (We are using subscripts in this example to clarify which attribute we are referring to, so int 1 and int 2 are different instances of the same non-terminal symbol.) There are two types of attributes we might encounter: synthesized or inherited. Synthesized attributes are those attributes that are passed up a parse tree, i.e., the left-
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 side attribute is computed from the right-side attributes. The lexical analyzer usually supplies the attributes of terminals and the synthesized ones are built up for the nonterminals and passed up the tree. X –> Y 1 Y 2 ...Y n X.a = f(Y 1 .a, Y 2 .a, . ..Y n .a) Inherited attributes are those that are passed down a parse tree, i.e., the right-side attributes are derived from the left-side attributes (or other right-side attributes). These attributes are used for passing information about the context to nodes further down the tree. X
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 07/18/2011.

Page1 / 8

160_Syntax-Directed_Translation - CS143 Handout 16 Summer...

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

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