0% found this document useful (0 votes)
42 views15 pages

Unit 3 RRJ TAC

The document discusses syntax-directed translation and intermediate code generation. It defines syntax-directed definitions as a generalization of context-free grammars where grammar symbols are associated with attributes. The values of attributes are computed by semantic rules associated with grammar productions. Syntax-directed translation attaches attributes to grammar symbols and uses semantic rules to evaluate attribute values, which can generate code, check types, and issue errors. Synthesized attributes get their values from child nodes while inherited attributes get values from parent nodes.

Uploaded by

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

Unit 3 RRJ TAC

The document discusses syntax-directed translation and intermediate code generation. It defines syntax-directed definitions as a generalization of context-free grammars where grammar symbols are associated with attributes. The values of attributes are computed by semantic rules associated with grammar productions. Syntax-directed translation attaches attributes to grammar symbols and uses semantic rules to evaluate attribute values, which can generate code, check types, and issue errors. Synthesized attributes get their values from child nodes while inherited attributes get values from parent nodes.

Uploaded by

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

March 27, 2023

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

Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 1


2

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.

Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 4

3 4

1
March 27, 2023

Implementing Syntax Directed Definitions


Syntax-directed translation
 In syntax-directed translation, we attach ATTRIBUTES to grammar
symbols.
 The values of attributes are computed by SEMANTIC RULES associated  Dependency Graphs
with grammar productions.
 Conceptually, we have the following flow:
 S-Attributed Definitions

 L-Attributed Definitions

 In practice, however, we do everything in a single pass.

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.

3. An attribute may hold almost any thing.


◦ a string, a number, a memory location, a complex record.

Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 7

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

 S-attributed definition: A syntax-directed definition where all


attributes are synthesized (the “S” stands for synthesized).

Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 13 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 14

13 14

 We may conclude that if a definition is S-attributed,


then it is also L-attributed as L-attributed definition
encloses S-attributed definitions.

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

Attribute dependencies Annotated Parse Tree


 Given a SDT, we can describe the dependencies 1. A parse tree showing the values of attributes at each node is
called an annotated parse tree.
between the attributes with a DEPENDENCY
GRAPH.
2. The process of computing the attributes values at the nodes is
called annotating (or decorating) of the parse tree.
 A parse tree annotated with attributes is called an
ANNOTATED parse tree. 3. Of course, the order of these computations depends on the
dependency graph induced by the semantic rules.

 Computing the attributes of the nodes in a parse


tree is called ANNOTATING or DECORATING the
tree.

Monday, March 27, 2023 17


Prepared By Dr. Rekh Ram Janghel Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 18

17 18

Syntax-Directed Definition -- Example


Production Semantic Rules
L → E return print(E.val)
E → E1 + T E.val = E1.val + T.val
E →T E.val = T.val
T → T1 * F T.val = T1.val * F.val
T→F T.val = F.val
F→(E) F.val = E.val
F → digit F.val = digit.lexval

1. Symbols E, T, and F are associated with a synthesized attribute val.


2. The token digit has a synthesized attribute lexval (it is assumed that it is
evaluated by the lexical analyzer).

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

F val=3 F val=5 F val=4

digit digit digit


lexval =3 * lexval =5
+ lexval =4 n
Monday, March 27, 2023 21
Prepared By Dr. Rekh Ram Janghel Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 22

21 22

Annotated tree/ Dependency Graph Dependency Graph


Input: 5+3*4 L
Input: 5+3*4

E.val=17

E.val=5 T.val=12

T.val=5 T.val=3 F.val=4

F.val=5 F.val=3 digit.lexval=4

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

Intermediate Code Generation Data structure of three address code


 Intermediate codes are machine independent codes, but they
are close to machine instructions.  Three address code instructions can be implemented as objects or as records with
fields for the operator and the operands. Three such representations are called
 The given program in a source language is converted to an ◦ Quadruples A quadruple (or just "quad') has four fields, which we call op, arg,,
equivalent program in an intermediate language by the arg2, and result
intermediate code generator. ◦ Triples: A triple has only three fields, which we call op, arg1, and arg2. the DAG
 Intermediate language can be many different languages, and and triple representations of expressions are equivalent
the designer of the compiler decides this intermediate ◦ Indirect Triples: consist of a listing of pointers to triples, rather than a listing of
triples themselves.
language.
1. syntax trees can be used as an intermediate language.
 The benefit of Quadruples over Triples can be seen in an optimizing compiler,
2. postfix notation can be used as an intermediate language.
where instructions are often moved around.
3. three-address code, (Quadraples) can be used as an intermediate language
 With quadruples, if we move an instruction that computes a temporary t, then the
instructions that use t require no change. With triples, the result of an operation is
referred to by its position, so moving an instruction may require to change all
references to that result. This problem does not occur with indirect triples.

Fall 2003 CS416 Compiler Design 25 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 26

25 26

Translation Schemes: Translation Schemes:


1) postfix form  1. postfix form
2) Syntax notation Example
a+b ab+
3) Three address code(TAC)
(a+b)*c ab+c*
a+b*c abc*+
a:=b*c+b*d abc*bd*+:=

(+) simple and concise


(+) good for driving an interpreter
(- ) Not good for optimization or code generation

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

Translation Schemes: Translation Schemes:


 1. postfix form  1. postfix form
Example Example
a*-(b+c) a*-(b+c)
a*-(bc+)
a* (bc+) –
a (bc+) -*

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

3-addr code 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

 3-addr code Write three address code, Triplex , Quadruples and


a := b*c + b*d
 Triple Indirect for the following expression
op arg1 arg2 Op arg1 arg2
(a+b )*(c +d*(a+b))
(1) * b c
(2) * b d
(3) + (1) (2)
(4) := (3) a
 Quadruple
op arg1 arg2 arg3
Op arg1 arg2 arg3
(1) * b c t1
(2) * b d t2
(3) + t1 t2 t3
(4) := t3 a
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 33 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 34

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

Monday, March 27, 2023 39


Prepared By Dr. Rekh Ram Janghel Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 40

39 40

10
March 27, 2023

 Quadruple
op arg1 arg2 arg3

Op arg1 arg2 arg3


(1) - c . t1
(2) * b t1 t2
(3) + (t2) b t3
(4) - c . t4
(5) * (t3) (t4) t5
(6) = (5) a

Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 41 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 42

41 42

Write three address code, Triplex and Quadruples for


the following expression

(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

 3-addr code (a+b+c)*(c+d*e)


Write three address code, Triplex and Quadruples for
 Triple
the following expression Op arg1 arg2
op arg1 arg2 (1) + a b
(a+b+c)*(c + d*e) (2) + (1) c
(1) t1 = a+b (3) + c d
(1) t1 = a+b
TAC (2) T2=t1+c (4) * (3) e
(2) T2=t1+c
(3) t2 = b*t1 (3) t2 = b*t1 (5) * (2) (4)
(3) t3 = c+d (3) t3 = c+d
(4) t4 = t3*e (4) t4 = t3*e Op arg1 arg2 arg3
(5) t5 = t2 *t4 t5 = t2 *t4
(5)
(1) + a b t1
(2) + (t1) c t2
Quadruple (3) + c d t3
op arg1 arg2 arg3 (4) * (3) e t4
(5) * (2) (4) t5

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

 3-addr code A= -B*(C+D)


 Triple Example: a
=b*-c+b*-c
Op arg1 arg2
op arg1 arg2 (1) + C D Calculate:
(2) - B .
(3) * (2) (1)
(4) = (3) TAC
Triple
Quadruple Quadruple
Op arg1 arg2 arg3
op arg1 arg2 arg3 Syntax Tree
(1) + C D t1 DAG
(2) - B . t2
(3) * (2) (1) t3
(4) = (3) A (c) Indirect triple

Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 49 Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 50

49 50

Example: Representations of a =b*-c+b*-c


Variants of Syntax Trees
 Syntax tree: represent constructs in the source program; the children of
a node represent the meaningful components of a constructor.
Representations of a = a * (b - c) + (b - c) * d

 b) DAG

 DAG (direct acyclic graph) identifies the common subexpressions


(subexpressions that occur more than once) of the expression. (c) Indirect triple
X[i]=y, (0) op: []=; Arg1: x ;arg2: I
Monday, March 27, 2023 Prepared By Dr. Rekh Ram Janghel 51 (1) op: assign ; arg1: (0);
Monday, arg2:
March 27, 2023 y Prepared By Dr. Rekh Ram Janghel 52

51 52

13
March 27, 2023

The three-address code for


a+b*c-d/(b*c)

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

Three address code is as follows Quadruples Triples


t1 = - b
t2 = t1 * d
t3 = t2 + c
t4 = - b
t5 = t4 * d
t6 = t3 + t5
a = t6

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

You might also like