CD Unit4 - PPT
CD Unit4 - PPT
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.
Example:-
Productions Semantic Rules
EE+T E.Val = E.val + T.val
ET 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:- ABCD // 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:- ABCD // 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:- ABCD
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.
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
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
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).
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