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

Syntax Directed Definitions in Compiler Design

Uploaded by

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

Syntax Directed Definitions in Compiler Design

Uploaded by

devilandrew177
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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 { [Link] = [Link] + [Link]}

In this, [Link] derive its values from [Link] and [Link]

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
[Link] = [Link]. Hence, [Link] = 4 and since T is parent node of F so, we
get [Link] = 4 from semantic action [Link] = [Link]. Then, for T –> T1 * F
production, the corresponding semantic action is [Link] = [Link] * [Link] .
Hence, [Link] = 4 * 5 = 20

Similarly, combination of [Link] + [Link] becomes [Link] i.e. [Link] = [Link] +


[Link] = 26. Then, the production S –> E is applied to reduce [Link] = 26 and
semantic action associated with it prints the result [Link] . 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 { [Link] = [Link], [Link] = [Link] }

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 [Link] (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 [Link].

Conclusion

Syntax Directed Definitions (SDD) and Syntax Directed Translation


(SDT) are essential tools in the design and implementation of compilers
and interpreters. They allow for the generation of intermediate code, type-
checking, and expression evaluation in a modular and structured manner.
By defining attributes and associating them with grammar productions,
we can control the semantic processing of programming languages
efficiently. Synthesized and inherited attributes form the basis of attribute
evaluation, supporting a broad range of programming language features.

Common questions

Powered by AI

SDDs and SDT are instrumental in efficiently implementing compilers by structuring semantic processing and code translation processes. SDDs provide a mechanism to attach semantic information to syntactic grammar, facilitating intermediate code generation, type-checking, and expression evaluations. SDT complements this by seamlessly integrating semantic actions with parse grammar, translating high-level code to machine or intermediate code. This structured, declarative framework increases compiler reliability, consistency, and modularity .

Semantic actions are crucial in Syntax Directed Translation as they orchestrate the transformation of source code to an intermediate or target language by executing actions at appropriate grammar production points. By embedding these actions within the grammatical framework and aligning them with parsing phases, semantic actions ensure that the logical meaning and operational aspects of the code are preserved and effectively translated. They facilitate precision, modularity, and the proper execution of semantic-based rules .

Annotated parse trees in syntax directed definitions are characterized by their high-level specifications that abstract implementation details. They do not specify explicit evaluation order, providing flexibility in attribute computation based on dependency constraints. These trees encapsulate attribute values within parse nodes for a given input, allowing a clear distinction between structural and semantic components of a language, and facilitating efficient semantic processing by focusing on relevant attributes .

Inherited attributes enable type declarations by propagating type information from parent or sibling nodes within the syntax structure. For instance, if an inherited attribute L is used to declare types such as int or float, it dictates the type assignment for identifiers like 'a' and 'c' in a declaration 'int a, c'. This top-down computation through functions like Enter_type inserts type entries into symbol tables at each respective identifier, ensuring consistent and accurate type checks and validations during compilation .

Synthesized attributes derive their values from children's attributes in a parse tree and are computed in a bottom-up manner. For example, in a grammar rule E --> E1 + T, E.val is derived from E1.val and T.val. Inherited attributes derive values from parent or sibling nodes and are computed top-down. For instance, in the rule A --> BCD, C.in is derived from A.in. Both synthesized and inherited attributes form the backbone of attribute evaluation in SDDs, enabling precise semantic processing .

Within the parse tree for the input '4 * 5 + 6', semantic action computation involves evaluating expressions from leaf to root. Starting with F -> digit, F.val = digit.lexval, assigning F.val = 4. For T -> T1 * F, T.val = T1.val * F.val results in T.val = 4 * 5 = 20. For E -> E1 + T, E.val = E1.val + T.val gives E.val = 6 + 20 = 26, and finally, S -> E reduces E with a result of E.val = 26. These computations showcase the bottom-up evaluation process of synthesized attributes .

The order of evaluation is not explicitly specified in annotated parse trees to maintain high-level specification abstraction, simplifying the representation of semantic rules by hiding implementation details. This approach allows greater flexibility in modifying and extending semantic rules without affecting interface usage. Consequently, compilers can independently determine the most efficient evaluation order, adhering to constraints dictated by dependencies among attributes, thus optimizing performance .

Syntax Directed Definitions (SDDs) in compiler design are formal methods that integrate semantic information into the syntactic structure of a programming language by attaching attribute-based semantic rules to grammar productions. SDDs enhance context-free grammar by allowing semantic rules to compute various language constructs like types, memory locations, or code fragments. They form the foundation for generating intermediate code, performing type-checking, and evaluating expressions in a modular and structured manner, significantly aiding in the development and functionality of compilers .

Computing synthesized attributes in a syntax directed definition involves several steps: First, the semantic rules corresponding to each production in the grammar must be defined. Next, an annotated parse tree is generated for the input string using these rules. Attribute values are then computed in a bottom-up fashion, where each node's attributes are derived from its children, with the root node providing the final computed value. This structured approach ensures all semantic rules are systematically applied .

Syntax Directed Translation (SDT) utilizes Syntax Directed Definitions by embedding semantic actions and attributes in grammatical rules, thereby transforming source language code into an intermediate or target language. SDT intertwines these semantic actions with parsing processes, allowing declarative translation of input while ensuring that actions are triggered synchronously or asynchronously with parsing. This facilitates the structured translation of high-level code into machine or intermediate-level code .

You might also like