0% found this document useful (0 votes)
14 views6 pages

CD Unit3 Part2

Syntax Directed Definitions (SDD) are methods for attaching semantic information to programming language syntax, enhancing context-free grammar with semantic rules for value derivation. Syntax Directed Translation (SDT) translates high-level languages into intermediate or machine languages through semantic actions during parsing. Annotated parse trees represent attribute values at each node, with synthesized and inherited attributes computed through bottom-up and top-down methods, respectively.

Uploaded by

togehol492
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)
14 views6 pages

CD Unit3 Part2

Syntax Directed Definitions (SDD) are methods for attaching semantic information to programming language syntax, enhancing context-free grammar with semantic rules for value derivation. Syntax Directed Translation (SDT) translates high-level languages into intermediate or machine languages through semantic actions during parsing. Annotated parse trees represent attribute values at each node, with synthesized and inherited attributes computed through bottom-up and top-down methods, respectively.

Uploaded by

togehol492
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/ 6

Syntax Directed Definition in Compiler Design

Syntax Directed Definitions (SDD) are formal methods of attaching semantic


information to the syntactic structure of a programming language. SDDs improve the
means of context-free high-level by instances, adding a semantic rule set for every
production of the grammar. The rules described in these definitions state how to
derive values such as types, memory locations, or fragments of code from the
structure of an input object.
Syntax Directed Translation (SDT) is the action of translating a high-level language
program into an intermediate language or machine language according to the
semantic rules imposed by SDDs. Semantic actions in SDT, act in coordination with
the parsing process in order to provide the translation of the input code. These
actions are declarative and they are triggered during the parsing phase of the
message to yield the result.

What is Syntax Directed Translation?


Syntax Directed Translation (SDT), is a technical manner utilized in semantics and
language translation whereby a source language is translated into an intermediate
language or a target language through semantic actions, and attributes attached to
the grammar. The translation process follows the structure of the grammar, besides
the performance of semantic actions is interwoven with the processes of input
parsing.
The semantic actions are normally included in the grammatical rules and can be
either performed synchronously or asynchronously with the parsing actions.
Integrated development systems such as SDT offer tools to compilers such as code
generation, lexical analysis, evaluation of expressions, definition, grammar, and
checking of types among other services.

What are Annotated Parse Trees?

The parse tree containing the values of attributes at each node for given input string
is called annotated or decorated parse tree.

Features of Annotated Parse Trees

 High level specification


 Hides implementation details

 Explicit order of evaluation is not specified


Types of Attributes

There are two types of attributes:


1. Synthesized Attributes: These are those attributes which derive their values
from their children nodes i.e. value of synthesized attribute at node is computed from
the values of attributes at children nodes in parse tree.

Example:

E --> E1 + T { E.val = E1.val + T.val}

In this, E.val derive its values from E1.val and T.val

Computation of Synthesized Attributes

 Write the SDD using appropriate semantic rules for each production in given
grammar.
 The annotated parse tree is generated and attribute values are computed in
bottom up manner.

 The value obtained at root node is the final output.

Example: Consider the following grammar

S --> E

E --> E1 + T
E --> T

T --> T1 * F

T --> F

F --> digit

The SDD for the above grammar can be written as follow


Let us assume an input string 4 * 5 + 6 for computing synthesized attributes. The
annotated parse tree for the input string is

For computation of attributes we start from leftmost bottom node. The rule F –> digit
is used to reduce digit to F and the value of digit is obtained from lexical analyzer
which becomes value of F i.e. from semantic action F.val = digit.lexval. Hence, F.val
= 4 and since T is parent node of F so, we get T.val = 4 from semantic action T.val =
F.val. Then, for T –> T1 * F production, the corresponding semantic action is T.val =
T1.val * F.val . Hence, T.val = 4 * 5 = 20
Similarly, combination of E1.val + T.val becomes E.val i.e. E.val = E1.val + T.val = 26.
Then, the production S –> E is applied to reduce E.val = 26 and semantic action
associated with it prints the result E.val . Hence, the output will be 26.
2. Inherited Attributes: These are the attributes which derive their values from their
parent or sibling nodes i.e. value of inherited attributes are computed by value of
parent or sibling nodes.
Example:

A --> BCD { C.in = A.in, C.type = B.type }

Computation of Inherited Attributes

 Construct the SDD using semantic actions.

 The annotated parse tree is generated and attribute values are computed in
top down manner.

Example: Consider the following grammar

S --> T L

T --> int

T --> float

T --> double
L --> L1, id

L --> id

The SDD for the above grammar can be written as follow


Let us assume an input string int a, c for computing inherited attributes. The
annotated parse tree for the input string is

The value of L nodes is obtained from T.type (sibling) which is basically lexical value
obtained as int, float or double. Then L node gives type of identifiers a and c. The
computation of type is done in top down manner or preorder traversal. Using function
Enter_type the type of identifiers a and c is inserted in symbol table at corresponding
id.entry.

SDD

Syntax-Directed Definitions (SDD) A 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. An attribute has a
name and an associated value: a string, a number, a type, a memory location, an
assigned register, strings. The strings may even be long sequences of code, say
code in the intermediate language used by a compiler. If X is a symbol and a is one
of its attributes, then we write X.a to denote the value of a at a particular parse-tree
node labeled X. If we implement the nodes of the parse tree by records or objects,
then the attributes of X can be implemented by data fields in the records that
represent the nodes for X. The attributes are evaluated by the semantic rules
attached to the productions. Example: PRODUCTION SEMANTIC RULE E -> E1 + T
{E.code = E1.code || T.code || ‘+’} SDDs are highly readable and give high-level
specifications for translations. But they hide many implementation details. For
example, they do not specify order of evaluation of semantic actions.

1.2. Syntax-Directed Translation Schemes (SDT) SDT embeds program fragments


called semantic actions within production bodies. The position of semantic action in a
production body determines the order in which the action is executed. Example: In
the rule E -> E1 + T { print ‘+’ }, the action is positioned after the body of the
production. SDTs are more efficient than SDDs as they indicate the order of
evaluation of semantic actions associated with a production rule. This also gives
some information about implementation details.

You might also like