CD Unit 3
CD Unit 3
Syntax-Directed Translation
1
Prof Anil Kumar 2023
2
Prof Anil Kumar 2023
An inherited attribute at node N is defined only in terms of attribute values at N’s parent, N itself and
N’s siblings
Each grammar symbol has two kinds of associated attributes:
– Synthesized: available from children (RHS) of grammar rule;
e.g., for E E1+ T, have
E.val = E1.val + T.val
– Inherited: available from siblings or parent of RHS symbols;
e.g., for ML dec id : type, id gets its type from type.
Attributes of terminals are specified by the lexical analyzer.
3
Prof Anil Kumar 2023
4
Prof Anil Kumar 2023
5
Prof Anil Kumar 2023
6
Prof Anil Kumar 2023
Evaluating Expression – 3 * 5 + 4n
Production Semantic Rule
LEn L.val = E.val
E Ei + T E.val = F..val + T.val
E - T E.val = T.val
T Ti * F T.val = Ti.val * F.val
T - F T.val = F.val
F (E) F.val = E.val
F digit F.val = digit.lexval
A parse tree showing the values of its attributes is called Annomated Parse Tree.
7
Prof Anil Kumar 2023
8
Prof Anil Kumar 2023
9
Prof Anil Kumar 2023
10
Prof Anil Kumar 2023
11
Prof Anil Kumar 2023
12
Prof Anil Kumar 2023
Operation:
13
Prof Anil Kumar 2023
E ---> T
T ---> F
14
Prof Anil Kumar 2023
F ---> digit
3*5+4$ - -
*5+4$ 3 3
*5+4$ T 3 T ---> F
5+4$ T* 3
15
Prof Anil Kumar 2023
+4$ T*5 3 * 5
+4$ T 15 T ---> T * F
+4$ E 15 E ---> T
4$ E+ 15
$ E+4 15 + 4
$ E+T 15 4 T ---> F
$ E 19 E ---> E + T
E 19
L 19 L ---> E $
S-attributed grammar are a class of attribute grammars characterized by having no inherited attributes.
Inherited attributes, which must be passed down from parent nodes of children nodes of the abstract syntax tree
during semantic analysis of the parsing process, are a problem of bottom-up parsing, because bottom-up parsing, the
parent nodes of the abstract syntax tree are created after creation of all of their children.
Attributes evaluation is S-attributed grammars can be incorporated conveniently in both top-down and bottom-up
parsing. YACC is based on the S-attributes approach.
16
Prof Anil Kumar 2023
Intermediate code generator receives input from its predecessor phase, semantic analyzer, in the
form of an annotated syntax tree. That syntax tree then can be converted into a linear representation,
e.g., postfix notation.
Intermediate code tends to be machine independent code.
A source code can directly be translated into its target machine code, then why at all we need to
translate the source code into an intermediate code which is then translated to its target code? Let us
see the reasons why we need an intermediate code.
17
Prof Anil Kumar 2023
PUSH 2
PUSH Y
MULTIPLY
PUSH X
SUBSTRACT
18
Prof Anil Kumar 2023
Syntax Tree: (Abstract syntax tree – retain essential structure of parse tree, eliminate unneeded
node)
19
Prof Anil Kumar 2023
Three-Address Code
Intermediate code generator receives input from its predecessor phase, semantic analyzer, in the
form of an annotated syntax tree. That syntax tree then can be converted into a linear representation,
e.g., postfix notation. Intermediate code tends to be machine independent code.
Therefore, code generator assumes to have unlimited number of memory storage register to
generate code.
For example:
a = b + c * d;
The intermediate code generator will try to divide this expression into sub-expressions and then
generate the corresponding code.
r1 = c * d;
r2 = b + r1;
r3 = r2 + r1;
20
Prof Anil Kumar 2023
a = r3
r being used as registers in the target program.
A three-address code has at most three address locations to calculate the expression. A three
address code can be represented in two forms : quadruples and triples.
Quadruples
Each instruction in quadruples presentation is divided into four fields: operator, arg1, arg2, and
result. The above example is represented below in quadruples format:
Op arg1 arg2 result
* c d r1
+ b r1 r2
+ r2 r1 r3
= r3 a
Triples
Each instruction in triples presentation has three fields : op, arg1, and arg2.The results of respective
sub-expressions are denoted by the position of expression.
Triples represent similarity with DAG and syntax tree. They are equivalent to
Declarations
A variable or procedure has to be declared before it can be used. Declaration involves allocation of
space in memory and entry of type and name in the symbol table. A program may be coded and
designed keeping the target machine structure in mind, but it may not always be possible to
accurately convert a source code to its target language.
Taking the whole program as a collection of procedures and sub-procedures, it becomes possible to
declare all the names local to the procedure. Memory allocation is done in a consecutive manner
and names are allocated to memory in the sequence they are declared in the program.
We use offset variable and set it to zero {offset = 0} that denote the base address.
21
Prof Anil Kumar 2023
The source programming language and the target machine architecture may vary in the way names
are stored, so relative addressing is used. While the first name is allocated memory starting from the
memory location 0 {offset=0}, the next name declared later, should be allocated memory next to
the first one.
Example:
We take the example of C programming language where an integer variable is assigned 2 bytes of
memory and a float variable is assigned 4 bytes of memory.
int a;
float b;
Allocation process:
{offset = 0}
int a;
id.type = int
id.width = 2
offset = offset + id.width
{offset = 2}
float b;
id.type = float
id.width = 4
offset = offset + id.width
{offset = 6}
To enter this detail in a symbol table, a procedure enter can be used. This method may have the
following structure:
enter(nam e, type, offset)
This procedure should create an entry in the symbol table, for variable name, having its type set to
type and relative address offset in its data area.
Semantic analysis
• Semantic analysis is the task of ensuring that the declarations and statements of a program are semantically
correct,
• i.e, that their meaning is clear and consistent with the way in which control structures and data types are
supposed to be used.
• Semantic analysis is the process of drawing meaning from text.
22
Prof Anil Kumar 2023
• It allows computers to understand and interpret sentences, paragraphs, or whole documents, by analyzing
their grammatical structure, and identifying relationships between individual words in a particular context.
• semantics errors that the semantic analyzer is expected to recognize:
• Type mismatch
• Undeclared variable
• Reserved identifier misuse.
• Multiple declaration of variable in a scope.
• Accessing an out of scope variable.
• Actual and formal parameter mismatch.
23