0% found this document useful (0 votes)
2 views

3.2

This document covers Syntax Directed Translation in compiler design, focusing on Syntax Directed Definitions, S-Attributed and L-Attributed Definitions, and Translation Schemes. It explains the evaluation methods for these definitions, the role of Intermediate Representation (IR) in compilers, and different forms of three-address code. The document also discusses the significance of well-designed IRs and provides examples of postfix notation, three-address code, and syntax trees.

Uploaded by

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

3.2

This document covers Syntax Directed Translation in compiler design, focusing on Syntax Directed Definitions, S-Attributed and L-Attributed Definitions, and Translation Schemes. It explains the evaluation methods for these definitions, the role of Intermediate Representation (IR) in compilers, and different forms of three-address code. The document also discusses the significance of well-designed IRs and provides examples of postfix notation, three-address code, and syntax trees.

Uploaded by

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

Compiler Design

(KCS-502)
Unit-3
Syntax Directed Translation
Contents
• Syntax Directed Translations
• Syntax Directed Definitions
• Implementing Syntax Directed Definitions
– Dependency Graphs
– S-Attributed Definitions
– L-Attributed Definitions
• Translation Schemes
Strongly Non-Circular Syntax Directed
Definitions
• Strongly Non-Circular Syntax Directed Definitions. Formalisms
for which an attribute evaluation order can be fixed at compiler
construction time.
– They form a class that is less general than the class of non-circular
definitions.
– In the following we illustrate two kinds of strictly non-circular
definitions: S-Attributed and L-Attributed Definitions.
Evaluation of S-Attributed Definitions
• Synthesized Attributes can be evaluated by a bottom-up
parser as the input is being analyzed avoiding the
construction of a dependency graph.
• The parser keeps the values of the synthesized attributes in
its stack.
•Whenever a reduction A → α is made, the attribute for A is
computed from the attributes of α which appear on the stack.
• Thus, a translator for an S-Attributed Definition can be simply
implemented by extending the stack of an LR-Parser.
L-Attributed Definitions
• L-Attributed Definitions contain both synthesized and
inherited attributes but do not need to build a dependency
graph to evaluate them.
• Definition. A syntax directed definition is L-Attributed if each
inherited attribute of Xj in a production A → X1 . . . Xj . . . Xn,
depends only on:
1. The attributes of the symbols to the left (this is what L in L-
Attributed stands for) of Xj , i.e., X1 X2 . . . Xj−1, and
2. The inherited attributes of A.
• Theorem. Inherited attributes in L-Attributed Definitions can
be computed by a PreOrder traversal of the parse-tree.
Evaluating L-Attributed Definitions
• L-Attributed Definitions are a class of syntax directed
definitions whose attributes can always be evaluated by
single traversal of the parse-tree.
• The following procedure evaluate L-Attributed Definitions by
mixing PostOrder (synthesized) and PreOrder (inherited)
traversal.
Translation Schemes
• Translation Schemes are more implementation oriented than
syntax directed definitions since they indicate the order in
which semantic rules and attributes are to be evaluated.
• Definition. A Translation Scheme is a context-free grammar in
which
1. Attributes are associated with grammar symbols;
2. 2. Semantic Actions are enclosed between braces {} and are
inserted within the right-hand side of productions.
3. • Yacc uses Translation Schemes.
Contd…
• Translation Schemes deal with both synthesized and inherited
attributes.
• Semantic Actions are treated as terminal symbols: Annotated
parse-trees contain semantic actions as children of the node
standing for the corresponding production.
• Translation Schemes are useful to evaluate L-Attributed
definitions at parsing time (even if they are a general
mechanism).
– An L-Attributed Syntax-Directed Definition can be turned into a
Translation Scheme.
Translation Schemes: An Example
• Consider the Translation Scheme for the L-Attributed
Definition for “type declarations”:
Contd…
Design of Translation Schemes
• When designing a Translation Scheme we must be sure that
an attribute value is available when a semantic action is
executed.
• When the semantic action involves only synthesized
attributes: The action can be put at the end of the production.
• Rules for Implementing L-Attributed SDD’s. If we have an L-
Attibuted Syntax-Directed Definition we must enforce the
following restrictions:
1. An inherited attribute for a symbol in the right-hand side of
a production must be computed in an action before the
symbol;
2. A synthesized attribute for the non terminal on the left-
hand side can only be computed when all the attributes it
references have been computed: The action is usually put
at the end of the production.
Compile-Time Evaluation of Translation
Schemes
• Attributes in a Translation Scheme following the above rules
can be computed at compile time similarly to the evaluation
of S-Attributed Definitions.
• Main Idea. Starting from a Translation Scheme (with
embedded actions) we introduce a transformation that makes
all the actions occur at the right ends of their productions.
– For each embedded semantic action we introduce a new Marker
(i.e., a non terminal, say M) with an empty production ( M → ξ);
– The semantic action is attached at the end of the production M → ξ.
Example
Contd…
Contd…
Intermediate Representation (IR)
• IR is a data structure used internally by a compiler or virtual machine
(VM) while translating a source program
• • Front end analyses a source program and creates an IR
• • Back end analyses the IR and generates target code

• • Plugin 𝑚 front ends with 𝑛 back ends to come up with 𝑚 × 𝑛


• • An well-designed IR helps ease compiler development

compilers
Intermediate Representation (IR)
• • Compilers may create a number of IRs during the translation process

• • High-level IRs are closer to the source (e.g., syntax trees)


• • Well-suited for tasks like static type checking
• • Low-level IRs are closer to the target ISA
• • Well-suited for tasks like register allocation and instruction selection
Types of IRs
• Postfix notation
• 3 address code
• Syntax tree
Postfix Notation
• The ordinary (infix) way of writing the sum of a and b is with
operator in the middle : a + b
• The postfix notation for the same expression places the
operator at the right end as ab +. In general, if e1 and e2 are
any postfix expressions, and + is any binary operator, the
result of applying + to the values denoted by e1 and e2 is
postfix notation by e1e2 +. No parentheses are needed in
postfix notation because the position and arity (number of
arguments) of the operators permit only one way to decode a
postfix expression. In postfix notation the operator follows
the operand.
• Example – The postfix representation of the expression (a –
b) * (c + d) + (a – b) is : ab – cd + *ab -+.
Three-Address Code
A statement involving no more than three references(two for
operands and one for result) is known as three address
statement. A sequence of three address statements is known as
three address code. Three address statement is of the form x = y
op z , here x, y, z will have address (memory location). Sometimes
a statement might contain less than three references but it is still
called three address statement.
Example – The three address code for the expression a + b * c + d
:
T1=b*c
T2=a+T1
T3=T2+d
T 1 , T 2 , T 3 are temporary variables.
Syntax Tree
• Syntax tree is nothing more than condensed form of a parse
tree. The operator and keyword nodes of the parse tree are
moved to their parents and a chain of single productions is
replaced by single link in syntax tree the internal nodes are
operators and child nodes are operands. To form syntax tree
put parentheses in the expression, this way it's easy to
recognize which operand should come first.
• Example – x = (a + b * c) / (a – b * c)
Contd…
Three-Address Code (3AC)

• At most one operator in the RHS.

• 3AC is a linearized representation of a syntax tree where


explicit names correspond to interior graph nodes
• 3AC is popularly used in code optimization and code generation
• Use of names for intermediate values allows 3AC to be easily
rearranged
DAG and Corresponding 3AC
Contd…..
Forms of 3AC

• Composed of two concepts: addresses and instructions


• Addresses can be program variables, constants, and
temporaries
• Variables can be pointers to the symbol table entries
• Distinct names for each temporary helps in optimization analyses
Forms of 3AC

• i. Assignments of the form 𝑥 = 𝑦 op 𝑧, 𝑥 = op 𝑦, or 𝑥 = 𝑦

• iii. Conditional jumps of the form if 𝑥 goto L, if 𝑥 relop 𝑦


• ii. Unconditional jump goto L

• iv. Procedure calls and returns of the form "param 𝑥", "call
goto L

𝑝,𝑛", "𝑦 = call 𝑝,𝑛", and "return 𝑦"


Contd…
v. Indexed copy instructions of the form 𝑥 = 𝑦[𝑖] and 𝑥 𝑖 = 𝑦
vi. Address and pointer assignments of the form 𝑥 = &𝑦, 𝑥 =
∗𝑦, and ∗𝑥 = 𝑦
The End

30

You might also like