15Cs314J - Compiler Design: Unit Iii
15Cs314J - Compiler Design: Unit Iii
UNIT III
Prepared by:
Dr.R.I.Minu
Associate Professor
1
UNIT III
Unit III
Semantic
Intermediate Code generator
analysis
Intermediate
Synthesized Languages - prefix -
Syntax tree- Intermediate
attributes – postfix - Quadruple Assignment Boolean Back patching –
Evaluation of languages – Case Statements
Inherited - triple - indirect Statements Expressions Procedure calls.
expression - Declarations
attributes triples - three-
address code
SEMANTIC ANALYSIS
• From Syntax analysis ,We have learnt how a parser constructs parse trees in
the syntax analysis phase.
• The plain parse-tree constructed in that phase is generally of no use for a
compiler, as it does not carry any information of how to evaluate the tree.
• Semantics of a language provide meaning to its constructs, like tokens and
syntax structure.
• Semantics help interpret symbols, their types, and their relations with each
other.
• Semantic analysis judges whether the syntax structure constructed in the
source program derives any meaning or not.
SEMANTIC ANALYSIS
• There are two ways to represent the semantic rules associated with
grammar symbols.
• Syntax-Directed Definitions (SDD)
• Syntax-Directed Translation Schemes (SDT)
• Syntax Directed Definitions (SDD)
Context free Grammar + Semantic Rule = SDD
• Syntax-Directed Translation Schemes (SDT) embeds program
fragments called semantic actions within production bodies
• SDTs are more efficient than SDDs as they indicate the order of
evaluation of semantic actions associated with a production rule.
Syntax Directed Definitions
• A syntax-directed definition (SDD) is a context-free grammar together
with attributes and rules.
• Attributes are associated with grammar symbols
• Rules are associated with productions.
SDD- Attributes
• Additional information (attributes) are appended to one or more of
its non-terminals in order to provide context-sensitive information
• Each attribute has well-defined domain of values, such as integer,
float, character, string, and expressions.
• Attribute grammar (when viewed as a parse-tree) can pass values or
information among the nodes of a tree.
E→E+T
{ E.value = E.value + T.value }
SDD- Attributes
• Semantic attributes may be assigned to their values from their
domain at the time of parsing and evaluated at the time of
assignment or conditions.
• Based on the way the attributes get their values, they can be broadly
divided into two categories
• Synthesized attributes and
• Inherited attributes.
Synthesized attributes
• These attributes get values from the attribute values of their child
nodes. To illustrate, assume the following production:
S→ ABC
• A can get values from S, B and C. B can take values from S, A, and C.
Likewise, C can take values from S, A, and B.
S-attributed SDT
• This form of SDT uses both synthesized and inherited attributes with
restriction of not taking values from right siblings.
• In L-attributed SDTs, a non-terminal can get values from its parent,
child, and sibling nodes. As in the following production
S→ ABC
For the string int a,b,c we have to distribute the data type int to all the identifiers a,b and c;
Steps to be followed
• Construct the syntax directed definition using semantic action
• Annotate the parse tree with inherited attributes by processing in top down fashion
Dependency graph
• For the rule X-> YZ the semantic action is given by X.x = f(Y.y,Z.z) then
synthesized attribute is X.x and X.x depends upon attributes Y.y and
Z.z
Dependency graph for L- Attribute
From Sibling
Parent to
child
Evaluation Order
• The RHS should have single operand and the LHS should have two
operand and a operator
• Example :
Question: A=-B * (C/D)
T1 = -B
T2 = C/D
T3 = T1*T2
A = T3
Quadruples
• Each instruction in triples presentation has three fields : op, arg1, and
arg2.
• The results of respective sub-expressions are denoted by the position
of expression.
Op arg1 arg2
(0) * c d
(1) + b (0)
(2) + (1) (0)
(3) = (2)
Indirect Triples
• In these productions,
• Nonterminal B represents a Boolean
expression
• Nonterminal S represents a statement.