unit-iii syllabus introduction...fields ptrs to operands mknode(op, left, right ) •identifier: one...
TRANSCRIPT
![Page 1: UNIT-III SYLLABUS Introduction...fields ptrs to operands mknode(op, left, right ) •identifier: one field with label id and another ptr to symbol table mkleaf (id, entry). •number:](https://reader033.vdocuments.net/reader033/viewer/2022041919/5e6b5eebb882304c61605910/html5/thumbnails/1.jpg)
Blog: anilkumarprathipati.wordpress.com 1
UNIT-III SYLLABUS
Syntax Directed Translation: Definitions, construction of Syntax Trees, S-attributed and L-attributed grammars. Intermediate code generation: abstract syntax tree, Intermediate code forms, translation of simple statements and control flow statements
1
Introduction • We can associate information with a language construct
by attaching attributes to the grammar symbols.
• A syntax directed definition (SDD) specifies the values of attributes by associating semantic rules with the grammar productions.
Semantic Rule Production
E->E1+T E.code=E1.code||T.code||’+’
• SDT scheme: embeds program fragments (also called semantic actions) within production bodies. The position of the action defines the order in which the action is executed (in the middle of production or end). • We may alternatively insert the semantic actions inside the grammar. E -> E1+T {print ‘+’}
2
Syntax Directed Definitions
Attributes are associated with grammar symbols and rules are associated with productions
Attributes may be of many kinds: numbers, types, table references, strings, etc.
Attributes are in two types: Synthesized attributes
A synthesized attribute at node N is defined only in terms of attribute values of children of N and at N itself.
Inherited attributes An inherited attribute at node N is defined only in terms of attribute values
at N’s parent, N itself and N’s siblings
3
Grammar symbols: L, E, T, F, n , + , * ,( , ) , digit
Non-terminals L,E, T, F have an attribute called val
Terminal digit has an attribute called lexval
The value for lexval is provided by the lexical analyzer.
PRODUCTION SEMANTIC RULE
L E n 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
Example with Synthesized Attributes
4
![Page 2: UNIT-III SYLLABUS Introduction...fields ptrs to operands mknode(op, left, right ) •identifier: one field with label id and another ptr to symbol table mkleaf (id, entry). •number:](https://reader033.vdocuments.net/reader033/viewer/2022041919/5e6b5eebb882304c61605910/html5/thumbnails/2.jpg)
Blog: anilkumarprathipati.wordpress.com 2
Example: 3*5+4n
digit lexval =3
digit lexval =5
digit lexval =4
+ * n
F val=3 F val=5 F val=4
T val=3
T val=15
T val=4
E val=19
L
Print(19)
5
Fig: Annotated pars tree for 3*5+4n
Example with Inherited Attributes Even though inherited can be simulated by
synthesized it is more natural to write Syntax-Directed Definitions using inherited.
below in is an inherited attribute of L
PRODUCTION SEMANTIC RULE
D T L L.in = T.type
T int T.type = integer
T real T.type = real
L L1 , id L1.in = L.in
addtype(id.entry, L.in)
L id addtype(id.entry, L.in) 6
Example: real id1, id2 , id3
id entry=id2
id entry=id3
,
L in=real
T type=real
D
real id entry=id1
,
L in=real
L in=real
addtype(id1,real)
addtype(id2,real)
addtype(id3,real)
7 Fig: Annotated pars tree for real id1, id2 , id3
Evaluation orders for SDD’s
A dependency graph is used to determine the order of computation of attributes.
Dependency graph
For each parse tree node, the parse tree has a node for each attribute associated with that node
If a semantic rule defines the value of synthesized attribute A.b in terms of the value of X.c then the dependency graph has an edge from X.c to A.b
If a semantic rule defines the value of inherited attribute B.c in terms of the value of X.a then the dependency graph has an edge from X.a to B.c
8
![Page 3: UNIT-III SYLLABUS Introduction...fields ptrs to operands mknode(op, left, right ) •identifier: one field with label id and another ptr to symbol table mkleaf (id, entry). •number:](https://reader033.vdocuments.net/reader033/viewer/2022041919/5e6b5eebb882304c61605910/html5/thumbnails/3.jpg)
Blog: anilkumarprathipati.wordpress.com 3
Ordering the evaluation of attributes
If dependency graph has an edge from M to N then M must be evaluated before the attribute of N
Thus the only allowable orders of evaluation are those sequence of nodes N1,N2,…,Nk such that if there is an edge from Ni to Nj then i<j
Such an ordering is called a topological sort of a graph
9
S-Attributed definitions An SDT is S-attributed if every attribute is synthesized
We can have a post-order traversal of parse-tree to evaluate attributes in S-attributed definitions
S-Attributed definitions can be implemented during bottom-up
parsing without the need to explicitly create parse trees.
10
L-Attributed definitions A SDD is L-Attributed if the edges in dependency graph goes from Left
to Right but not from Right to Left.
More precisely, each attribute must be either
Synthesized
Inherited, but if there us a production A->X1X2…Xn and there is an inherited attribute Xi.a computed by a rule associated with this production, then the rule may only use: Inherited attributes associated with the head A
Either inherited or synthesized attributes associated with the occurrences of symbols X1,X2,…,Xi-1 located to the left of Xi
Inherited or synthesized attributes associated with this occurrence of Xi itself, but in such a way that there is no cycle in the graph
11
Example of mixed attributes
1) T -> FT’ 1) T’ -> *FT’1
3) T’ -> ε 1) F -> digit
Production Semantic Rules
T’.inh = F.val T.val = T’.syn T’1.inh = T’.inh*F.val T’.syn = T’1.syn T’.syn = T’.inh F.val = digit.lexval
12
![Page 4: UNIT-III SYLLABUS Introduction...fields ptrs to operands mknode(op, left, right ) •identifier: one field with label id and another ptr to symbol table mkleaf (id, entry). •number:](https://reader033.vdocuments.net/reader033/viewer/2022041919/5e6b5eebb882304c61605910/html5/thumbnails/4.jpg)
Blog: anilkumarprathipati.wordpress.com 4
13 14
Difference b/w S-attr and L-attr
S-Attributed L-Attributed
1) Uses only Synthesized attributes
1) Uses both Synthesized and Inherited attributes with left sibling.
2) Semantic actions are placed at right end of the production.
2) Semantic actions are placed any where in the right side of the production.
3) Attributes are evaluated during the Bottom-Up Parsing.
3) Attributes are evaluated by traversing parse tree in depth first, left to right.
ICG Introduction • Intermediate code is the interface between front
end and back end in a compiler • Ideally the details of source language are
confined to the front end and the details of target machines to the back end.
Parser Static
Checker Intermediate Code
Generator Code
Generator
Front end Back end
Intermediate code forms are: 1) Syntax Tree. 2) Postfix Notation. 3) Three Address Code
15
Syntax Trees • An AST, or just syntax tree, is a tree representation
of the abstract syntactic structure of source code written in a programming language.
• It is sometimes beneficial to crate a DAG instead of tree for Expressions.
• This way we can easily show the common sub-expressions and then use that knowledge during code generation.
• Example: a+a*(b-c)+(b-c)*d
+
+ *
*
-
b c
a d
a -
b c 16
![Page 5: UNIT-III SYLLABUS Introduction...fields ptrs to operands mknode(op, left, right ) •identifier: one field with label id and another ptr to symbol table mkleaf (id, entry). •number:](https://reader033.vdocuments.net/reader033/viewer/2022041919/5e6b5eebb882304c61605910/html5/thumbnails/5.jpg)
Blog: anilkumarprathipati.wordpress.com 5
Syntax Tree vs Parse Tree
17
Syntax Tree Parse Tree
1) Interior nodes are operators and leaves are operands.
1) Interior nodes are Non-Terminals and leaves are Terminals.
2) When representing a program in a tree structure usually use a syntax tree.
2) Rarely constructed as a data structure.
3) Represents the abstract syntax of a program(semantics)
3) Represents the concrete structure of a program
4)Contains only meaningful information. 4) Contains un-usable information.
5)
AST
• Each node can be represented as a record.
• operators: one field for operator, remaining fields ptrs to operands mknode(op, left, right )
• identifier: one field with label id and another ptr to symbol table mkleaf (id, entry).
• number: one field with label num and another to keep the value of the number mkleaf (num, val) .
18
SDT for creating DAG’s
1) E -> E1+T 2) E -> E1-T 3) E -> T 4) T -> (E) 5) T -> id 6) T -> num
Production Semantic Rules
E.node= make Node(‘+’, E1.node,T.node) E.node= makeNode(‘-’, E1.node,T.node) E.node = T.node T.node = E.node T.node = make Leaf(id, id.entry) T.node = make Leaf(num, num.val)
19
1) p1=Leaf(id, entry-a) 2) P2=Leaf(id, entry-a)=p1 3) p3=Leaf(id, entry-b) 4) p4=Leaf(id, entry-c) 5) p5=Node(‘-’,p3,p4) 6) p6=Node(‘*’,p1,p5) 7) p7=Node(‘+’,p1,p6)
8) p8=Leaf(id,entry-b)=p3 9) p9=Leaf(id,entry-c)=p4 10)p10=Node(‘-’,p3,p4)=p5 11)p11=Leaf(id,entry-d) 12)p12=Node(‘*’,p5,p11) 13)p13=Node(‘+’,p7,p12)
Example: Given expression is a+a*(b-c)+(b-c)*d
20
![Page 6: UNIT-III SYLLABUS Introduction...fields ptrs to operands mknode(op, left, right ) •identifier: one field with label id and another ptr to symbol table mkleaf (id, entry). •number:](https://reader033.vdocuments.net/reader033/viewer/2022041919/5e6b5eebb882304c61605910/html5/thumbnails/6.jpg)
Blog: anilkumarprathipati.wordpress.com 6
21
Abstract Syntax Tree for a:=b*(-c)+b*(-c)
Three address code
• In a three address code there is at most one operator at the right side of an instruction
• Example: a+a*(b-c)+(b-c)*d
+
+ *
*
-
b c
a d
t1 = b – c t2 = a * t1 t3 = a + t2 t4 = t1 * d t5 = t3 + t4
22
Three address code formats
• Quadruples
– Has four fields: op, arg1, arg2 and result
• Triples
– Temporaries are not used and instead references to instructions are made
• Indirect triples
– In addition to triples we use a list of pointers to triples
23
Example
b * minus c + b * minus c
t1 = minus c t2 = b * t1 t3 = minus c t4 = b * t3 t5 = t2 + t4 a = t5
Three address code
minus *
minus c t3 * +
=
c t1 b t2 t1
b t4 t3
t2 t5 t4
t5 a
arg1 result arg2 op
Quadruples
minus *
minus c
* +
=
c b (0)
b (2)
(1) (3)
a
arg1 arg2 op
Triples
(4)
0 1 2
3 4
5
minus *
minus c
* +
=
c b (0)
b (2)
(1) (3)
a
arg1 arg2 op
Indirect Triples
(4)
0 1 2
3 4
5
(0) (1)
(2)
(3) (4)
(5)
op 35 36 37
38 39
40
24
![Page 7: UNIT-III SYLLABUS Introduction...fields ptrs to operands mknode(op, left, right ) •identifier: one field with label id and another ptr to symbol table mkleaf (id, entry). •number:](https://reader033.vdocuments.net/reader033/viewer/2022041919/5e6b5eebb882304c61605910/html5/thumbnails/7.jpg)
Blog: anilkumarprathipati.wordpress.com 7
Example Write Three Address Code for the expression given
below-
If A < B then 1 else 0
(1) If (A < B) goto (4)
(2) T1 = 0
(3) goto (5)
(4) T1 = 1
(5)
25
Write Three Address Code for the expression given below-
If A < B and C < D then t = 1 else t = 0 (1) If (A < B) goto (3) (2) goto (4) (3) If (C < D) goto (6) (4) t = 0 (5) goto (7) (6) t = 1 (7)
26
Example
x[i] := y x := y[i]
27
Triples
[ ] =
assign
x
(0) y
arg1 arg2 op
(0)
(1)
i
= [ ]
assign y x (0)
arg1 arg2 op
(0)
(1) i
Comparison
28
![Page 8: UNIT-III SYLLABUS Introduction...fields ptrs to operands mknode(op, left, right ) •identifier: one field with label id and another ptr to symbol table mkleaf (id, entry). •number:](https://reader033.vdocuments.net/reader033/viewer/2022041919/5e6b5eebb882304c61605910/html5/thumbnails/8.jpg)
Blog: anilkumarprathipati.wordpress.com 8
29
Compiler Interpreter
Compiler Takes Entire program as
input
Interpreter Takes Single instruction
as input .
Intermediate Object Code is
Generated
No Intermediate Object Code is
Generated
Conditional Control Statements are
Executes faster
Conditional Control Statements are
Executes slower
Memory Requirement : More(Since
Object Code is Generated) Memory Requirement is Less
Program need not be compiled every
time
Every time higher level program is
converted into lower level program
Errors are displayed after entire
program is checked
Errors are displayed for every
instruction interpreted (if any)
Example : C Compiler Example : BASIC