0% found this document useful (0 votes)
11 views30 pages

CD Chapter 4

Uploaded by

ephitsegaye7878
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)
11 views30 pages

CD Chapter 4

Uploaded by

ephitsegaye7878
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/ 30

WCU

College of Engineering and Technology


Dep’t : Computer Science

Course: Compiler Design


Chapter 4: Syntax Direct Translation (SDT)

Instructor: Agegnehu Ashenafi (MSc.)


Ch-4:Syntax Direct Translation (SDT)
The translation of languages is guided by context-free grammars.
 We associate information with a programming language construct by attaching attributes to the grammar
symbols representing the construct.
 Values for attributes are computed by "semantic rules" associated with the grammar productions.
There are two notations for associating semantic rules with productions,
1) syntax directed definitions
2) Translation schemes.
 Syntax-directed definitions are high-level specifications for translations.
 They hide many implementations details and free the user from having to specify explicitly the order in
which translation takes place.
 Translation schemes indicate the order, in which semantic rules are to be evaluated,
Conceptually with both syntax-directed definitions and translation schemes, we
 parse the input token stream,
 build the parse tree, and then
 traverse the tree as needed to evaluate the semantic rules at the parse-tree nodes.
 Evaluation of the semantic rules may
 generate code,
 saw information in a symbol table,
 issue error messages, or perform any other activities.

 The translation or the token stream is the result obtained by evaluating the semantic rules,
Ch-4:Syntax Direct Translation (SDT)
• Special cases of syntax-directed definitions can be implemented in a single pass by evaluating semantic rules
during parsing, without explicitly constructing a parse tree or a graph showing dependencies between
attributes.
• Since single pass implementation is important for compile time efficiency.
4.1 Syntax direct definition
A syntax-directed definition is a generalization of a context-free grammar in which each grammar symbol
has an associated set of attributes, partitioned into two subsets called the synthesized and inherited
attributes of that grammar symbol.
If we think of a node for the grammar symbol in a parse tree as a record with fields for holding information,
then an attribute corresponds to the name of a field.
An attribute can represent anything we choose: a string, a number, a type, a memory location, or
whatever.
The value of an attribute at a parse-tree node is defined by a semantic rule associated with the production
used at that node.
a) The value of a synthesized attribute at a node is computed from the values of attributes at the children of that node in
the parse tree;
b) The value of an inherited attribute at a node is computed from the values of attributes at the siblings and parent of
that node.
Semantic rule set up dependencies between attributes that will be represented by a graph.
From the dependency graph, we derive an evaluation order for the semantic rules; evaluation of the
semantic rules defines the values of the attributes at the nodes in the parse tree for the input string.
4.1 Syntax direct definition …
• A parse tree showing the values of attributes at each node is called an
annotated parse tree.
• The process of computing the attribute values at the nodes is called
annotating or decorating the parse tree.
4.1 Syntax direct definition …
4.1 Syntax direct definition …
• The token digit has a synthesized attribute lexval whose value is assumed to Be supplied by the lexical
analyzer.
• The rule associated with the production LEn for the starting nonterminal L is just a procedure that
prints as output the value of the arithmetic expression generated by E; we can think of this rule as
defining a dummy attribute for the nonterminal L.
• In a syntax-directed definition, terminals are assumed to have synthesized attributes only, as the
definition does not provide any semantic rules fix terminals.
• Values for attributes of terminals are usually supplied by the lexical analyzer.
• The start symbol is assumed not to have any inherited attributes. Unless otherwise stated.
a) Synthesized Attribute
• A syntax-directed definition that uses synthesized attributes exclusively is said to be an S attribute
definition. A parse tree for an S-attributed definition can always be annotated by evaluating the semantic
rules for the attributes at each node bottom up, from the leaves to the root.
• Example 5.2. The S-attributed definition in Example 5.1 specifies a desk calculator
• that reads an input line containing an arithmetic expression involving Digits, parentheses, the operators +
and *, followed by a newline character n, and prints the value of the expression.
• For example, given the expression 3*5+4 followed by a newline, the program prints the value 19. Figure
5.3 contains an annotated parse tree for the input 3*5+4n. The output, printed at the root of the tree, is
the value of E.val at the first child of the root.
a) Synthesized Attribute
b) Inherited Attribute
b) Inherited Attribute …
The start symbol is assumed not to have any inherited attributes.
b) Inherited Attribute …
Dependency Graph …
Evaluation Order
Evaluation Order
• The underlying grammar is used to construct a parse tree for the input. The
dependency graph is constructed as discussed above. From a topological sort of the
dependency graph, we obtain an evaluation order for the semantic rules. Evaluation of
the semantic rules in this order yields the translation of the input string.
Constructing Syntax Trees for Expressions
Constructing Syntax Trees for Expressions
Directed Acyclic Graph(DAG)
DAG … cont’d
A Directed Acyclic Graph, or DAG, is a concept from mathematics and computer science that describes a specific type of graph.
• Directed: This means that the connections between the points in the graph (known as nodes) have a direction. In a directed graph, each
connection (edge) points from one node to another, indicating a one-way relationship.
• Acyclic: This term means that there are no cycles in the graph. A cycle occurs when you can start at one node and follow a path of edges
that eventually loops back to the starting node. In a DAG, loops aren't allowed.
• Graph: In this context, a graph is a collection of nodes (which can represent various objects like tasks, events, or states) and edges (which
represent the relationships or connections between these nodes).
Key Characteristics of a DAG
• Directionality: Each edge has a direction, showing the relationship from one node to another.
• No Cycles: Once you have left a node, you can't go back to it again. This prevents the chance of circling back to the node through the edges.
• Topological Ordering: Since DAGs have directed edges and no cycles, you can list the nodes in a linear order. This order respects the
direction of the edges, meaning for every directed edge from node A to node B, A appears before B in order.
Applications of DAGs
DAGs are incredibly useful in scenarios where you need to represent tasks that must be done in a specific order.
Here are a few areas where DAGs are commonly used:
• Project Scheduling: In project management, tasks that depend on the completion of other tasks can be represented using a DAG. This helps
in planning the order of operations.
• Data Processing: Many data processing workflows involve steps that depend on the outputs of previous steps. DAGs can help in mapping
out these dependencies clearly.
• Blockchain Technology: Some newer cryptocurrencies use DAGs instead of traditional blockchain structures to record transactions. This can
offer improvements in scalability and speed.
An L-attributed SDD, attributes
may be inherited or synthesized,
which is referred to as an L-
attribute definition.
• In an S-attributed SDD, all
attributes are synthesized,
which is called an S-attribute
definition.
• S-attributed SDDs are useful for
bottom-up parsing, while L-
attributed grammars allow
attribute evaluation in left-to-
right traversal
Types of attributes – Attributes may be of two types – Synthesized or Inherited.
• Synthesized attributes – A Synthesized attribute is an attribute of the nonterminal on the left-hand side of a production.
Synthesized attributes represent information that is being passed up the parse tree. The attribute can take value only from
its children (Variables in the RHS of the production). The non-terminal concerned must be in the head (LHS) of production.
For e.g. let’s say A -> BC is a production of a grammar, and A’s attribute is dependent on B’s attributes or C’s attributes
then it will be synthesized attribute.
• Inherited attributes – An attribute of a nonterminal on the right-hand side of a production is called an inherited attribute.
The attribute can take value either from its parent or from its siblings (variables in the LHS or RHS of the production). The
nonterminal concerned must be in the body (RHS) of production. For example, let’s say A -> BC is a production of a
grammar and B’s attribute is dependent on A’s attributes or C’s attributes then it will be inherited attribute because A is a
parent here, and C is a sibling.
Now, let’s discuss about S-attributed and L-attributed SDT.
• S-attributed SDT :
• If an SDT uses only synthesized attributes, it is called as S-attributed SDT.
• S-attributed SDTs are evaluated in bottom-up parsing, as the values of the parent nodes depend upon the values of the child nodes.
• Semantic actions are placed in rightmost place of RHS.
• L-attributed SDT:
• If an SDT uses both synthesized attributes and inherited attributes with a restriction that inherited attribute can inherit values from
left siblings only, it is called as L-attributed SDT.
• Attributes in L-attributed SDTs are evaluated by depth-first and left-to-right parsing manner.
• Semantic actions are placed anywhere in RHS.
• Example : S->ABC, Here attribute B can only obtain its value either from the parent – S or its left sibling A but It can’t inherit from its
right sibling C. Same goes for A & C – A can only get its value from its parent S, & C can get its value from S, A, & B as well because C is
the rightmost attribute in the given production.
Thank You !
… Question?

You might also like