Chap - 4 - Syntax - Directed - Translation - N07 - G10
Chap - 4 - Syntax - Directed - Translation - N07 - G10
Assignment
a
SYNTAX-DIRECTED
TRANSLATION
PREPARED BY
1.BETELHEM BELETE UU93273R
2.TIHITINA KASSAHUN UU88986R
INTRODUCTION
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?
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
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)
1. Synthesized Attributes
2. Inherited Attributes
Synthesized Attributes
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
1. S-Attributed Definition
2. L-Attributed Definition
S-Attributed Definitions
Example: 2+3*4