0% found this document useful (0 votes)
67 views5 pages

Syntax-Directed Translation - In-Class Assignment: A Dependency Graph

1. A syntax-directed definition (SDD) specifies a translation by augmenting a context-free grammar with attributes and rules. There are two types of attributes: synthesized attributes define nodes based on children, inherited attributes define nodes based on parents. 2. An SDD is L-attributed if it uses only synthesized or inherited attributes. The SDD from the example is L-attributed because it only uses synthesized attributes. 3. For a binary to decimal conversion grammar, an L-attributed SDD can be designed using an inherited 'side' attribute to specify whether bits are left or right of the decimal.

Uploaded by

Mmm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views5 pages

Syntax-Directed Translation - In-Class Assignment: A Dependency Graph

1. A syntax-directed definition (SDD) specifies a translation by augmenting a context-free grammar with attributes and rules. There are two types of attributes: synthesized attributes define nodes based on children, inherited attributes define nodes based on parents. 2. An SDD is L-attributed if it uses only synthesized or inherited attributes. The SDD from the example is L-attributed because it only uses synthesized attributes. 3. For a binary to decimal conversion grammar, an L-attributed SDD can be designed using an inherited 'side' attribute to specify whether bits are left or right of the decimal.

Uploaded by

Mmm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Syntax-Directed Translation – In-Class Assignment

1. Read carefully the following definitions


Syntax-directed definition (SDD) is a context-free grammar together with attributes and rules. Attributes are
associated with grammar symbols and rules are associated with productions.
Example: for an infix to postfix translator, we might add to the production T → T1 + F the rule T.code = T1.code ||
F.code || '+'
There are two kinds of attributes for nonterminals:
A synthesized attribute for a nonterminal A at a parse-tree node N is defined by a semantic rule associated with the
production at N. Note that the production must have A as its head. A synthesized attribute at node N is defined only
in terms of attribute values at the children of N and at N itself.
An inherited attribute for a nonterminal B at a parse-tree node N is defined by a semantic rule associated with the
production at the parent of N. Note that the production must have B as a symbol in its body. An inherited attribute at
node N is defined only in terms of attribute values at N's parent, N itself, and N's siblings.
Terminals can have synthesized attributes, but not inherited attributes.
To visualize the translation specified by an SDD, it helps to work with parse trees. A parse tree, showing the
value(s) of its attribute(s) is called an annotated parse tree.
Now, for the below SDD, give the annotated parse trees for the following expression: 2 * 8.

2. Read carefully the following definitions


A dependency graph depicts the flow of information among the attribute instances in a particular parse tree; an edge
from one attribute instance to another means that the value of the first is needed to compute the second.
A SDD is S-attributed if every attribute is synthesized. S-attributed definitions can be implemented during bottom-
up parsing.
A SDD is L-attributed if every
attribute is either: synthesized or
inherited. The rules for inherited
attributed are mentioned in the
nearby box.

Is the SDD from exercise 1 L-


attributed? Explain.

How about a SDD containing the


following rules? Explain your
answer.

3. For the grammar below design an L-attributed SDD to compute S.val, the decimal value of an input string in
binary. For example the translation of the string 101.101 (whose parse tree is also shown below) should be the
decimal number 5.625. Hint: Use an inherited attribute L.side that tells which side of the decimal point a given bit is
on. For all symbols specific their attribute, if they are inherited or synthesized and the various semantic rules. Also
show your work for the parse tree example given by indication the values of the symbol attributes.
4. Read carefully the following information
Syntax-Directed Translation (SDT): A translation scheme is another way of specifying a syntax-directed translation:
semantic actions (enclosed in braces) are embedded within the right-sides of the productions of a context-free
grammar. For example, rest --> + term { print ('+') } rest1. This indicates that a plus sign should be printed between
the depth-first traversal of the term node and the depth-first traversal of the rest1 node of the parse tree.
How to implement a syntax-directed translation using a predictive parser? It is not obvious how to do this, since the
predictive parser works by building the parse tree top-down, while the syntax directed translation needs to be
computed bottom-up.

Exercise:
For the following grammar, give (a) translation rules, (b) translation actions with numbers, and (c) a CFG
with action numbers, so that the translation of an input expression is the value of the expression. Is the
grammar LL(1)?

Transform the grammar rules with actions that you wrote to LL(1) form. Trace the actions of the predictive
parser on the input 2 + 3 * 4.

You might also like