0% found this document useful (0 votes)
13 views39 pages

Chap - 4 - Syntax - Directed - Translation - N07 - G10

Uploaded by

daniel
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)
13 views39 pages

Chap - 4 - Syntax - Directed - Translation - N07 - G10

Uploaded by

daniel
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/ 39

Compiler Group

Assignment
a

SYNTAX-DIRECTED
TRANSLATION

PREPARED BY
1.BETELHEM BELETE UU93273R
2.TIHITINA KASSAHUN UU88986R
INTRODUCTION

 Syntax-Directed Translation (SDT): is a key concept in compiler


design, where semantic rules are associated with the grammar of a
language to guide the translation of source code into an intermediate
representation or target code.
 By combining syntax analysis with semantic processing, SDT ensures
that the meaning of the program is preserved while transforming it.
 It utilizes attributes associated with grammar symbols, which can be
synthesized (computed from children nodes) or inherited (passed from
parent or sibling nodes).
 These attributes are evaluated based on semantic rules, often written
as actions embedded within a syntax tree traversal. SDT plays a critical
role in enabling efficient parsing, semantic checking, and code
generation, making it an essential technique in the development of
programming languages and compilers.
To produce semantic rules from the
given production

 We use 2 notations:
1. Syntax-Directed Translations/Translation
Schemes
2. Syntax-Directed Definitions
 Syntax-Directed Definitions:
– give high-level specifications for translations
– hide many implementation details such as order of evaluation of semantic actions.
– We associate a production rule with a set of semantic actions, and we do not say
when they will be evaluated.
 Translation Schemes:
– indicate the order of evaluation of semantic actions associated with a production
rule.
– In other words, translation schemes give a little bit more information about
implementation details
What is Syntax-Directed Translation?

 Syntax-Directed Translation (SDT): is a method used in


compilers to attach semantic actions to grammar productions,
enabling the systematic translation of source code into
intermediate forms or machine code.
 These actions, performed during parsing, use attributes of
grammar symbols to capture the structure and meaning of the
input program.
 SDT simplifies tasks like syntax analysis, semantic checks, and
code generation, making it a core concept in compiler design.
How SDT Work’s?

 Grammar Rules: SDT is based on a formal grammar that defines the structure
and syntax of a programming language.
 These rules specify valid sequences of tokens (such as keywords, identifiers, and
operators) that make up valid constructs in the language.
 Semantic Actions: For each grammar rule, specific actions are defined to
perform tasks during the translation process. These actions include:
 Calculating Values: Evaluating expressions and computing values for
variables or operations.
 Type Checking: Ensuring that operations are performed on compatible data
types.
 Code Generation: Converting the grammar constructs into intermediate code
or machine code.
 Symbol Table Management: Keeping track of variable declarations, types,
EXAMPLE: for input string 5+2*3

 Production  Semantic Rules


E→E+T E.val := E.val + T.val
E→T E.val := T.val
T→T*F T.val := T.val * F.val
T→F T.val := F.val
F → num F.val := num.lexval
Annotated parse tree for 5+2*3

 Syntax-directed Translation we use Top Down Bottom up reading


method to evaluate this example.
Key Purposes Of SDT

 Semantic Analysis: Ensures that the source code follows the logical rules
of the language, verifying that operations are performed on compatible
data types, variables are properly declared, and other semantic
constraints are satisfied.
 Translation: Converts high-level language constructs into intermediate or
machine code, maintaining the program's meaning.
 Error Checking: Identifies and reports errors in the syntax and semantics
of the code during the translation process.
 Code Generation: Facilitates the generation of executable code or
intermediate representations based on the syntactic structure of the
program.
 Symbol Table Management: Keeps track of variables, functions, and
other program-specific details to support correct translation and execution.
Syntax-Directed Definition (SDD)

 Syntax-Directed Definition (SDD): is a formal framework


used in compiler construction that associates each grammar
rule with semantic actions, which are triggered during the
parsing process.
 These semantic rules are used to assign values to attributes of
the grammar symbols, providing meaning to the syntactic
structures.
 SDD specifies how to compute or propagate information, such as
type checking, code generation, or intermediate
representations, based on the structure of the input program.
 It combines syntax (structure) with semantics (meaning) to
ensure that both are preserved and translated correctly during
compilation.
ATTRIBUTES

 Attribute: is a value or property associated with a grammar symbol


that provides meaning to the syntactic constructs.
 It can represent information like data types, values, or memory
locations used during translation and semantic analysis.
 Attributes helps to ensure that the program’s semantics (meanings)
are correctly translated from the source code to the target code.
Example: production E → E1 + T
semantic rule E.value := E1.value + T.value
Here:
 E.value,E1.value and T.value are attributes that represent the
values of the expressions.
 The semantic rule describes to compute the value of the expression E
by adding the values of E1 and T.
Types Of ATTRIBUTES in SDD

 There are two main types of attributes in Syntax-


Directed Definitions (SDD):

1. Synthesized Attributes
2. Inherited Attributes
Synthesized Attributes

 Synthesized Attributes: are attributes that are computed


from the attributes of the child nodes in a parse tree. These
attributes are passed upward in the tree, from the leaves to
the root.
 They often represent the result of operations or calculations
in the grammar, such as the value of an expression, the type
of an expression, or the generated intermediate code.
 They help in collecting and propagating information about
the syntactic constructs as the parsing process proceeds.
 Flow : Bottom up
EXAMPLE
Annotated parse tree for 3*5+4n
Inherited Attributes:

 Inherited Attributes: attributes are passed from the parent or


sibling nodes down to the child nodes in the parse tree. They
propagate information from the root to the leaves. Inherited
attributes are passed from parent or sibling nodes downward to
their children.
 They are often used to pass context-specific information like type,
scope, symbol table data,variable type or function name that is
necessary for processing specific parts of the syntax tree and to
ensure consistency across the program.
 Flow: Top down
EXAMPLE
Annotated parse tree for
real,id1,id2,id3
Syntax Tree

 A syntax tree, also known as a parse tree, is a hierarchical tree structure that
represents the syntactic structure of a source program according to a formal
grammar. Each node in the tree corresponds to a grammatical construct (such as a
symbol, expression, or statement), and the tree illustrates how the source code is
derived from the grammar.
 Root Node: The root of the tree represents the start symbol of the grammar,
typically corresponding to the entire program or expression.
 Internal Nodes: These nodes represent non-terminal symbols from the grammar,
such as expressions, statements, or operations.
 Leaf Nodes: The leaves of the tree represent terminal symbols, which correspond
to the actual tokens in the source code (like keywords, identifiers, operators, and
constants).
 Edges: The edges between nodes represent the relationships between symbols in
the grammar, indicating how the non-terminal symbols break down into smaller
parts.
Example

 arithmetic expression: a + b * c  syntax tree


 production E
/ | \
E→E+T
E + T
E→ T
| / | \
T→T*F
T T * F
T→F | | |
F → num F F c
| |
a b
Dependencies of Attributes

• In the semantic rule


b := f(c1, c2, …, ck)
we say b depends on c1, c2, …, ck
• The semantic rule for b must be evaluated after the
semantic rules for c1, c2, …, ck evaluated.
• The dependencies of attributes can be represented by a
directed graph called dependency graph.
Example

Production semantic rules


E → E1 + E2 E.val := E1.val + E2.val

E1 → E1 * E2 E1.val := E1.val * E2.val


Dependency Graphs
Evaluation of Semantic Rules

 Parse-tree methods (general approach for any acyclic dependency graphs)


1. Build a parse tree for each input
2. Build a dependency graph from the parse tree
3. Obtain evaluation order from a topological order of the dependency
graph
 Rule-based methods (parsing time)
1. Predetermine the order of attribute evaluation for each production,
e.g., translation schemes
 Oblivious methods
1. Evaluation order is independent of semantic rules
2. Evaluation order forced by parsing methods
3. Restrictive in acceptable attribute definitions
 E.g.: S-Attributed Definitions and L-Attributed Definitions
Types of SDD

 There are two main types in Syntax-Directed


Definitions (SDD):

1. S-Attributed Definition
2. L-Attributed Definition
S-Attributed Definitions

 S-attributed definition refers to a type of semantic rule used in the


context of syntax-directed translation, particularly in compiler
design.
 In an S-attributed definition,attributes are assigned values based on
the structure of a syntax tree, and the attributes are computed only in
a bottom-up manner (i.e., from the leaves to the root of the tree).
 The "S" in S-attributed indicates that all attributes are synthesized
—meaning that the value of an attribute at a node is computed from
its children.
 The evaluation of attributes happens in a bottom-up fashion, starting
from the leaf nodes and moving toward the root.
 Example: A -> XYZ A.a=f(X.x,Y.y,Z.z) where all attributes are synthesized.
E.g 2: input string 4*5+6
L-Attributed Definitions

 L-attributed definitions are a specific type of attribute grammar


used in compiler design and formal language theory.
 They define how attributes (properties or values associated with
grammar symbols) can be evaluated in a language.
 The key feature of L-attributed definitions is that they restrict the
evaluation of attributes to be "left-to-right," meaning that the
attributes of a node can only depend on the node itself or on its left
children in the syntax tree.
 In an L-attributed definition, attributes are evaluated in a way that
makes sure each attribute can only depend on the left side of the
grammar rule (or on values already computed for the left children).
 Example: A -> LM {L.i=e(A.i), M.i=m(L.s), M.i=m(A.s)} all attributes
are inherited.
E.g2: Construct SDD for the input
string int a, c

 As we sayed in L attribute SDD, the attributes drive their value from


their parent or sibling.
annotated parse tree and attribute
values computed in top down
manner
Bottom-Up Evaluation of S-Attributed

 Production Semantic Rules


L→E print(val[top-1])
E → E1 + T val[top] = val[top-2] + val[top]
E→T
T → T1 * F val[top] = val[top-2] * val[top]
T→F
F→(E) val[top] = val[top-1]
F → digit
 At each shift of digit, we also push digit.lexval into val-stack.
 At all other shifts, we do not put anything into val-stack because other
terminals do not have attributes (but we increment the
stack pointer for val-stack).
Abstract Syntax Tree (AST)

 An Abstract Syntax Tree (AST) is a


condensed representation of a parse tree.
 They are more compact than a parse tree and can be easly used
by compiler.
Example: A*B+C
Parse tree Abstract syntax tree
Translation Schemes

 The syntax directed translation scheme is a context


free Grammer .
 Or is used to evaluate the order of semantic rules
plus action .
 In translation scheme, the semantic rules are
embedded within the right side of the production.
 The position at which an action is to be executed is
shown by enclosed between braces . Or is written
within the right side of the production.
EXAMPLE Convert (A+B)∗C−D to
Postfix
 Step-by-Step Process
1.input: (A+B)*C-D .
Output: (empty)
Stack: ( empty)

2.Symbol ( : push to stack .


Output: (empty)
Stack: (

3.Symbol A: Operand, append to output.


Output: A
Stack: (
4. Symbol +: push to stack.
Output: A
Stack: ( +

5. Symbol B: Operand, append to output.


Output: AB
Stack: ( +

6.Input ): pop stack to output until ( is found.


Output: AB +
Stack: (empty)
7. Symbol *: push to stack.
Output: AB +
Stack: *

8. Symbol C: Operand, append to output.


Output: AB + C
Stack: *

9. Symbol -: pop stack to output(higher precedence * is popped),then


push –.
Output: AB + C *
Stack: -
10. Symbol D: Operand, append to output.
Output: AB + C * D
Stack: -

11. End :pop remaining operators from stack to output.


Output: AB +C * D -
Stack: (empty)
The postfix expression for(A+B)*C –D is:
AB+C*D-
SDT to convert infix to postfix

 Example: 2+3*4

2+3*4 -> 234*+


THANK YOU

You might also like