LEARNING MATERIALS, CD, UNIT-4 (Syntax Directed Translation)
LEARNING MATERIALS, CD, UNIT-4 (Syntax Directed Translation)
LEARNING MATERIALS
Department : Computer Science & Technology Semester : 5TH
UNIT : (4) Syntax directed translation Course Code : Contact Periods : 04 Hrs
__________________________________________________________________________
Content:
Syntax director definitions, Construction of syntax trees, Bottom-up evaluation of S attributed definitions, L
attributed definitions, Bottom-up evaluation of inherited attributes.
…………………………………………………………………………………………………………………..
float x = 10.1;
float y = x*30;
In the above example integer 30 will be typecasted to float 30.0 before multiplication, by semantic
analyzer.
Static and Dynamic Semantics:
1. Static Semantics –
It is named so because of the fact that these are checked at compile time. The static semantics
and meaning of program during execution, are indirectly related.
2. Dynamic Semantic Analysis –
It defines the meaning of different units of program like expressions and statements. These are
checked at runtime unlike static semantics.
SDT
In syntax directed translation, along with the grammar we associate some informal notations and these
notations are called as semantic rules.
Example
Example
S→E$ { printE.VAL }
SDT is implementing by parse the input and produce a parse tree as a result.
Example
S→E$ { printE.VAL }
Problem:01
Consider the following grammer and its corresponding Syntax directed Translation(SDT):
E->E+T { printf(“+”); }
What will be the output if we carry out this SDT on an input string “2+3*4” using –
i) Top down parser ii) Bottom up parser
Draw and explain the parse tree in each case.
Solution:
Let’s take a string to see how semantic analysis happens – S = 2+3*4. Parse tree corresponding to
S would be
Above diagram shows how semantic analysis could happen. The flow of information happens bottom-up and all the
children attributes are computed before parents, as discussed above. Right hand side nodes are sometimes annotated
with subscript 1 to distinguish between children and parent.
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.
Let us assume an input string int a, c for computing inherited attributes. The
annotated parse tree for the input string is
1. Define Syntax-Directed Definitions (SDDs) and explain their role in compiler design.
Answer: Syntax-Directed Definitions (SDDs) are formal systems used to define the syntax and semantics of
programming languages. An SDD associates each production rule of a context-free grammar with a set of
semantic rules that describe how to compute attributes for the non-terminals. These attributes can be used to
capture various aspects of the language's semantics, such as type information, values, or context.
Semantic Analysis: They facilitate the semantic analysis phase, ensuring that the code adheres to the rules
of the language.
Attribute Evaluation: They define how attributes are computed during parsing, allowing for the construction
of semantic information along with the syntax tree.
Modularity: By separating syntax from semantics, SDDs enhance the modularity and clarity of compiler de-
sign.
Answer: A syntax tree, or parse tree, is a hierarchical tree structure that represents the syntactic structure of
a source code based on a given grammar. Each internal node of the tree represents a non-terminal symbol,
while the leaves represent terminal symbols (tokens). The structure of the tree reflects the derivation of the
string in accordance with the grammar's production rules.
Visual Representation: Syntax trees provide a visual representation of the source code, making it easier to
understand its structure.
Facilitate Semantic Analysis: They serve as the foundation for semantic analysis, allowing for the evaluation
of expressions, scope resolution, and type checking.
Code Generation: Syntax trees are often used as input for code generation, where the compiler translates
the high-level constructs into lower-level code or intermediate representations.
Optimizations: They can be used for various optimizations during the compilation process, such as simplify-
ing expressions or eliminating unnecessary computations.
Answer: Bottom-Up Evaluation of synthesized attributes involves computing attribute values during the
parsing process, starting from the leaves (terminal symbols) of the syntax tree and moving up towards the
root. This method is often used in conjunction with bottom-up parsing techniques like LR parsing.
Answer: L-Attributed Definitions are a specific type of syntax-directed definitions where synthesized
attributes are evaluated in a manner that respects the left-to-right order of the parse tree. In L-attributed
definitions, attributes can be defined in such a way that:
1. Synthesized Attributes: These can depend on the attributes of the children of a node.
2. Inherited Attributes: These can depend on the attributes of the parent and siblings, but must be evaluated
in a left-to-right order.
Ease of Implementation: L-attributed definitions can be easily implemented in a single pass over the syntax
tree, which is useful for top-down parsers.
Semantic Constraints: They provide a structured way to impose semantic constraints and ensure correct
evaluation of attributes.
Enhanced Clarity: By clearly defining the flow of information between nodes, L-attributed definitions con-
tribute to better clarity and maintainability of compiler implementations.
1. Initialization: Inherited attributes are often initialized at the root of the parse tree or through semantic rules
applied during parsing.
2. Propagation: As the parser reduces production rules, it computes inherited attributes based on the values of
the parent and sibling nodes. For example, the inherited attribute for a child node might be calculated from
an attribute of the parent.
3. Use Cases: This technique is useful for handling scope information, type propagation, or controlling visibility
rules in programming languages.
If A.scope is the scope in which BBB is defined, then the inherited attribute for B can be defined as:
B.scope=A.scope During the parsing process, as the attributes of A are determined, they are propagated
down to B based on the rules defined.
Answer: In bottom-up parsing, syntax trees play a crucial role in the evaluation of syntactic constructs as
the parser builds the tree from the leaves up to the root. The significance of syntax trees in this context
includes:
Structure Representation: They represent the structure of the input source code, allowing the parser to sys-
tematically handle reductions based on grammar rules.
Semantic Evaluation: As the tree is constructed, attribute values can be computed, enabling semantic
checks to occur alongside parsing.
Facilitating Reductions: The structure of the syntax tree guides the parser in determining which production
rules can be applied at each step, aiding in the proper reduction of input symbols.
Integration with Code Generation: Once the parse tree is constructed, it serves as a foundation for generat-
ing intermediate code or target code, making it a vital component of the compilation process.