0% found this document useful (0 votes)
9 views

CD Unit4 - PPT

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

CD Unit4 - PPT

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Syntax Directed Definition (SDD)

Syntax Directed Definition is a kind of abstract specification, a combination of CFG + semantic rules

It is generalization of context free grammar in which each grammar production X –> a is associated
with it a set of production rules of the form s = f(b1, b2, ……bk) where s is the attribute obtained from
function f.

• Attributes are associated with grammar symbols.


• Semantic rules are associated with productions.
• If “X” is a symbol and “a” is one of its attribute then Xa denotes value at node X
s

Example:-
Productions Semantic Rules
EE+T E.Val = E.val + T.val
ET E.Val = T.val
Syntax Directed Definition (SDD)
SDD is used to add High Level information to the grammar by adding semantic rules.
SDD gives information like
• How symbols get values
• Simplifying evaluation
• Deriving attributes
• Which production to be evaluated first.
s
Types of Attributes:-
1. Synthesized Attribute, if a node takes value from its children
Ex:- ABCD // A is parent & BCD are children and s is attribute
A.s = B.s // Here parent A is taking value from its child B
2. Inherited Attribute, if a node takes value from its parent or siblings
Ex:- ABCD // A is parent & BCD are children and s is attribute
C.s = A.s // Here C is taking value from its parent A
C.s = B.s // Here C is taking value from its sibling B
Syntax Directed Definition (SDD)
Types of Syntax Directed Definition :
1. S-Attributed SDD or S-Attributed Definition Or S-Attributed Grammar:-
• A SDD that uses only synthesized attributes
Ex:- ABCD
A.s = B.s
• The semantic actions are placed at right end of production that’s why called POSTFIX SDD
• Attributes are evaluated with Bottom-Up parsing.

2. L-Attributed SDD or L-Attributed Definition Or L-Attributed Grammar:-


• A SDD that uses both synthesized & inherited attributes
s but each inherited attribute is
restricted to inherit from parent or left siblings only
Ex:- ABCD
A.s =B.s
C.s = A.s
C.s = B.s
C.s = D.s // WRONG because its inherited form right sibling.
• The semantic actions are placed at any end of production
• Attributes are evaluated with depth first , left to right order.
Syntax Tree
• A syntax tree is a tree in which each leaf node represents an operand, while each
internal node represents an operator.
• The Parse Tree is abbreviated as the syntax tree.
• The syntax tree is usually used when representing a program in a tree structure.
Example:- a * (b + c) – d /2

Constructing a Syntax Tree


3 functions are used to construct a syntax tree
1. mknode (op, left, right): It creates an operator node
s with the name op
and two fields, containing left and right pointers.

2. mkleaf (id, entry): It creates an identifier node with the label id


and the entry field, which is a reference to the identifier’s symbol table entry.

3. mkleaf (num, val): It creates a number node with the name num and a field containing the number’s
value, val. Make a syntax tree for the expression a 4 + c, for example. p1, p2,…, p5 are pointers to the
symbol table entries for identifiers ‘a’ and ‘c’, respectively, in this sequence.
Syntax Tree-example
Example:- Construct syntax tree for the given expression X*Y-5+2

STEP1:-- convert the expression from infix to postfix XY*5-Z+


STEP2:-- construct symbol-operation table
Symbol Operation
X P1=mkleaf( id , ptr to entry X)
Y P2= =mkleaf( id , ptr to entry Y)
* P3=mknode(*, p1,p2)
s
5 P4=mkleaf (num,5)
- P5=mknode(-,p3,p4)
Z P6=mkleaf( id , ptr to entry Z)
+ P7=mknode(+,p5,p6)
Syntax Tree-example
STEP3:-- write the grammar , for the expression X*Y-5+Z ,here operations performed are *, -, +
E E1 * T
E E1 - T
E E1 + T
E T
T id
T num

STEP3:-- write the SDD for the grammar = CFG + semantic rules (refer operation for a symbol form
symbol-operation table) s
Production Semantic Operation
E E1 * T E.node = mknode(*, E1.node, T.node)
E E1 - T E.node = mknode(-, E1.node, T.node)
E E1 + T E.node = mknode(+, E1.node, T.node)
E T E.node = T.node
T id T.node =mkleaf(id, id.ptr_entry)
T num T.node =mkleaf(num, num.value)
Syntax Tree-example
STEP5:-- construct syntax tree, follow the order of operations as in symbol-operation table

s
Intermediate code Generation
In the analysis-synthesis model of a compiler, the front end of a compiler translates a source program
into an independent intermediate code, then the back end of the compiler uses this intermediate
code to generate the target code (which can be understood by the machine). The benefits of using
machine-independent intermediate code are:

s NOTE:-
Intermediate code can be either I
i ) language-specific (e.g.,
Bytecode for Java) or

ii) language independent (three-


address code).
Intermediate code Representations
1. Postfix Notation: Also known as reverse Polish notation or suffix notation.
Ex:-For expression (a + b) * c postfix is : ab + c *
For expression(a – b) * (c + d) + (a – b) postfix is : ab – cd + *ab -+

2. Three-Address Code: A statement involving no more than three references(two for operands and
one for result) is known as a three address statement.
Three address statement is of form x = y op z, where x, y, and z will have address (memory location).
Ex:- The three address code for the expression a + b * c + d :
T1=b*c
T2=a+T1 s
T3=T2+d
T 1 , T 2 , T 3 are temporary variables.

3. Syntax Tree: A syntax tree is a tree in which each leaf node represents an operand, while each
inside node represents an operator.
Example: id + id * id
Intermediate code Representations
Advantages of Intermediate Code Generation:
1. Easier to implement: Intermediate code generation can simplify the code generation process by
reducing the complexity of the input code, making it easier to implement.
2. Facilitates code optimization: Intermediate code generation can enable the use of various code
optimization techniques, leading to improved performance and efficiency of the generated code.
3. Platform independence: Intermediate code is platform-independent, meaning that it can be
s
translated into machine code or bytecode for any platform.
4. Code reuse: Intermediate code can be reused in the future to generate code for other platforms
or languages.
5. Easier debugging: Intermediate code can be easier to debug than machine code or bytecode, as it
is closer to the original source code.
Abstract Syntax Tree(AST)
Abstract Syntax Tree (AST) is a kind of tree representation of the abstract syntactic structure of source
code written in a specific programming language.
• An AST is essentially a simplified version of a parse tree.
• Each node of the tree denotes a construct occurring in the source code.
• AST is highly specific to programming languages
• Abstract Syntax Tree (AST) is used because some constructs cannot be represented in context-free
grammar, such as implicit typing.
s AST :
Ex:- d + id * id would have the following syntax tree &
Syntax Tree Abstract Syntax Tree
Three-Address Code
Three-Address Code: A statement involving no more than three references(two for operands and one
for result) is known as a three address statement.
Three address statement is of form x = y op z, where x, y, and z will have address (memory location).

Example: The three address code for the expression a + b * c + d :


T1=b*c
T2=a+T1
T3=T2+d
T 1 , T 2 , T 3 are temporary variables.
s
NOTE:- Three-address code is an intermediate code. It is used by the optimizing compilers.

There are 3 ways to represent a Three-Address Code in compiler design:


i) Quadruples
ii) Triples
iii) Indirect Triples
Three-Address Code
i) Quadruples
The quadruples have four fields to implement the three address code. The field of quadruples
contains the name of the operator, the first source operand, the second source operand and the
result respectively.
Ex:- For the expression a := -b * c + d
The 3 –address code will be
Quadruple representation is
t1 := -b
t2 := c + d
t3 := t1 * t2
a := t3 s
Three-Address Code
ii) Triples
The triples have three fields to implement the three address code. The field of triples contains the
name of the operator, the first source operand and the second source operand.

Ex:- For the expression a := -b * c + d


The 3 –address code will be
t1 := -b Triple representation
t2 := c + d
t3 := t1 * t2
a := t3 s
Three-Address Code
iii) Indirect Triples
Similar to triple representation , but this representation makes use of pointer to the listing of all
references to computations which is made separately and stored.

Ex:- For the expression a := -b * c + d


The 3 –address code will be
t1 := -b Indirect Triple representation
t2 := c + d
t3 := t1 * t2
a := t3 s
SDD vs SDT

s
Syntax Directed Translation into 3 address Code

s
Syntax Directed Translation into 3 address Code

s
Syntax Directed Translation into 3 address Code

s
Syntax Directed Translation into 3 address Code: Example

s
Translation of Boolean Expression

s
Boolean Expression: Grammar & Action

s
Boolean Expression: Example

s
Flow of Control Statements

s
Translation of Control Statements

s
Flow of Control Statements
Semantic rules for three-address code for if- then statement
Production Semantic Rules Inference
S à if E then S1 We first generate a new
E.true:= newlabelE.false address location E.true. If
:= S.next S1.next := the expression E is false
then control should go to
S.next the statement following the
S.code := E.code || gen (E.true’:’) || S1.code body of the if-then. Hence,
we assign E.false as S.next.
If the expression E is true
the body of S, the
s statements following if-
then block need to be
evaluated and this is
ensured by setting S1.next
and S.next as same.
Finally, the code
corresponding to S is
evaluated as E.code
followed by the generation
of E.true label followed by
S1.code.
Flow of Control Statements
Semantic rules for three-address code for while loop

Production Semantic Rules Inference


S à while E do S1 S.begin := newlabelE.true Two new labels S.begin which
:= newlabel E.false := points to the expression’s first line
S.next S1.next := S.begin and E.true which points to the
S.code := gen (S.begin ‘:’) || E.code || beginning of the statement S1 is
generated. Expression’s false should
gen (E.true’:’) || S1.code || gen skip the body of the while and
(‘goto’ S.begin) should go to the statement
s following the statement S and
hence E.false is assigned S.next. The
next of the statement S1 is assigned
as S.begin as the exit
from the while block if from
Flow of Control Statements: Example

You might also like