Unit 3 RRJ TAC
Unit 3 RRJ TAC
Semantic Analysis
UNIT – 3: Check semantics
Error reporting
SYNTAX DIRECTED TRANSLATION
Disambiguate overloaded
& operators
INTERMEDIATE CODE GENERATION Type coercion
Static checking
◦ Type checking
◦ Control flow checking
◦ Unique ness checking
◦ Name checks
1 2
Translation of languages guided by context-free grammars.
Attach attributes to the grammar symbols.
Syntax-Directed Definitions
Values of the attributes are computed by semantic rules 1. A syntax-directed definition is a generalization of a context-free grammar in
which:
associated with the grammar productions. ◦ Each grammar symbol is associated with a set of attributes.
Two notations: ◦ This set of attributes for a grammar symbol is partitioned into two subsets
called
1. Syntax–directed definitions: Each production has a set synthesized and
of semantic rules associated with it. inherited attributes of that grammar symbol.
◦ Each production rule is associated with a set of semantic rules.
2. Translation Schemes: Embed the semantic actions
within the productions. 2. Semantic rules set up dependencies between attributes which can
be represented by a dependency graph.
Conceptually we parse the input token stream, build the
parse tree and then traverse the tree as needed to evaluate 3. This dependency graph determines the evaluation order of these semantic
the semantic rules. rules.
The rules may generate code, save information in the symbol 4. Evaluation of a semantic rule defines the value of an attribute. But a
table, issue error messages, etc. semantic rule may also have some side effects such as printing a value.
3 4
1
March 27, 2023
L-Attributed Definitions
Monday, March 27, 2023 5Prepared By Dr. Rekh Ram Janghel Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 6
5 6
Syntax-Directed Translation
1. Values of these attributes are evaluated by the semantic rules
associated with the production rules. Synthesized attribute: The value of the attribute at a
parent node can be found from the attributes of its children.
2. Evaluation of these semantic rules: It can be evaluated by traversing the tree bottom-up.
1. may generate intermediate codes
2. may put information into the symbol table Inherited attribute: The value of the attribute at a node
3. may perform type checking can be found from the attributes at its parent node and/or its
sibling nodes. Can be evaluated by traversing the tree top-
4. may issue error messages down.
5. may perform some other activities
in fact, they may perform almost any activities.
7 8
2
March 27, 2023
Synthesized attributes
Syntax-directed definitions
These attributes get values from the attribute values of their
The SYNTAX-DIRECTED DEFINITION (SDD) is a child nodes.
generalization of the context-free grammar. To illustrate, assume the following production:
S → ABC If S is taking values from its child nodes
Each grammar symbol in the CFG is given a set of ATTRIBUTES. (A,B,C),then it is said to be a synthesized attribute,
Attributes can be any type (string, number, memory loc, etc) as the values of ABC are synthesized to S.
SYNTHESIZED attributes are computed from the values of the As in our previous example (E → E + T),
CHILDREN of a node in the parse tree. the parent node E gets its value from its child node.
Synthesized attributes never take values
from their parent nodes or any sibling nodes.
Monday, March 27, 2023 9Prepared By Dr. Rekh Ram Janghel Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 10
9 10
Inherited attributes
L-attributed SDT
INHERITED attributes are computed from the attributes
of the parents and/or siblings of a node in the parse tree. As in the following production
S → ABC
inherited attributes can take values from parent and/or S can take values from A, B, and C (synthesized).
siblings. A can take values from S only.
B can take values from S and A.
As in the following production, C can get values from S, A, and B.
S → ABC No non-terminal can get values from the sibling to its
A can get values from S, B and C. right.
B can take values from S, A, and C.
Attributes in L-attributed SDTs are evaluated by depth-first and
Likewise, C can take values from S, A, and B.
left-to-right parsing manner.
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 11 Monday, March 27, 2023 12
Prepared By Dr. Rekh Ram Janghel
11 12
3
March 27, 2023
S-attributed SDT
S-Attributed Definitions If SDT uses only synthesized attributes, it is called as S-
1. We will look at two sub-classes of the syntax-directed definitions: attributed SDT.
◦ L-Attributed Definitions: in addition to synthesized attributes, we
may also use inherited attributes in a restricted fashion. These attributes are evaluated using S-attributed SDT that
◦ S-Attributed Definitions: only synthesized attributes used in the have their semantic actions written after the production
syntax-directed definitions.
(right hand side).
2. To implement S-Attributed Definitions and L-Attributed Definitions we As depicted, attributes in S-attributed SDTs are evaluated
can evaluate semantic rules in a single pass during the parsing. in bottom-up parsing, as the values of the parent nodes
depend upon the values of the child nodes.
3. Implementations of S-attributed Definitions are a little bit easier than
implementations of L-Attributed Definitions
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 13 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 14
13 14
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 15 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 16
15 16
4
March 27, 2023
17 18
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 19 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 20
19 20
5
March 27, 2023
Example 3*5+4n
Calculating synthesized attributes Draw the Tree
Input string: 3*5+4 newline L
Annotated tree:
Print(19)
E val=19
T val=15
T val=3 T val=4
21 22
E.val=17
E.val=5 T.val=12
digit.lexval=5 digit.lexval=3
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 23 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 24
23 24
6
March 27, 2023
Fall 2003 CS416 Compiler Design 25 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 26
25 26
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 27 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 28
27 28
7
March 27, 2023
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 29 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 30
29 30
Syntax notation
a*-(b+c) -
* +
a b c
(1) t1 = b + c
(2) t2 = -t1
(3) t3 = a *t2
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 31 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 32
31 32
8
March 27, 2023
33 34
3-addr code
Write three address code, Triplex , Quadruples and
Triple (a+b )*(c +d*(a+b))
Indirect for the following expression
op arg1 arg2 Op arg1 arg2
(1) + a b
(a+b )*(c +d*(a+b)) (2) + a b
TAC (1) t1 = a + b (3) * d (2)
(2) t2 = a + b (4) + c (3)
(3) t3 = d *t2 Quadruple (5) * (1) (4)
(4) t4 = c +t3
(5) t5 = t1 *t4 op arg1 arg2 arg3
Op arg1 arg2 arg3
(1) + a b t1
(2) + a b t2
(3) * d t2 t3
(4) + c t3 t4
(5) * t1 t4 t5
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 35 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 36
35 36
9
March 27, 2023
Write three address code, Triplex , Quadruples and Write three address code, Triplex , Quadruples and
Indirect for the following expression Indirect for the following expression
a=b*-c+b*-c a=b*-c+b*-c
TAC (1) t1 = -c
(2) t2 = b*t1
(3) t3 = -c
(4) t4=t3*b
(5) t5 = t2 +t4
(6) a= t5
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 37 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 38
37 38
3-addr code
3AC implementation Triple
op arg1 arg2
Code:
a := b * -c + b * -c a=b*-c+b*-c
Op arg1 arg2
3AC: (1) - c
(2) * b (1)
t1 := -c
(3) + (2) b
t2 := b * t1 (4) - c .
t3 := -c (5) * (3) (4)
t4 := b * t3 (6) = (5) a
t5 := t2 + t4
a := t5
39 40
10
March 27, 2023
Quadruple
op arg1 arg2 arg3
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 41 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 42
41 42
(a+b+c)*(c + d*e)
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 43 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 44
43 44
11
March 27, 2023
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 45 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 46
45 46
Write three address code, Triplex and Quadruples for Write three address code, Triplex and Quadruples for
the following expression the following expression
A= -B*(C+D) A= -B*(C+D)
TAC (1) t1 = C+D
(2) t2 = -B
(3) t3 = t2 *t4
(4) A= t3
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 47 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 48
47 48
12
March 27, 2023
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 49 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 50
49 50
b) DAG
51 52
13
March 27, 2023
1. TAC
2. Triple
3. Quadraple
4. DAG
5. Syntax Tree
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 53 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 54
53 54
t1 = b*c
t2 = a+t1
a = -b * d + c + (-b) * d
t3 = b*c
t4 = d/t3
t5 = t2-t4 1. TAC
2. Triple
3. Quadraple
4. DAG
5. Syntax Tree
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 55 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 56
55 56
14
March 27, 2023
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 57 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 58
57 58
Indirect Triples
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 59 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 60
59 60
15