0% found this document useful (0 votes)
5 views32 pages

Semantic Analysis

The document discusses the semantic analysis phase of compiler design, emphasizing the need for compilers to check various program properties beyond basic syntax, such as variable declaration and type consistency. It introduces concepts like syntax-directed definitions (SDD), synthesized and inherited attributes, and dependency graphs to manage semantic rules. The document also outlines the evaluation of attributes in SDDs, highlighting the differences between S-attributed and L-attributed SDDs.
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)
5 views32 pages

Semantic Analysis

The document discusses the semantic analysis phase of compiler design, emphasizing the need for compilers to check various program properties beyond basic syntax, such as variable declaration and type consistency. It introduces concepts like syntax-directed definitions (SDD), synthesized and inherited attributes, and dependency graphs to manage semantic rules. The document also outlines the evaluation of attributes in SDDs, highlighting the differences between S-attributed and L-attributed SDDs.
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/ 32

Compiler Design

Semantic Analysis
6th Semester B.Tech. (CSE)
Course Code: 18CS1T08

Dr. Jasaswi Prasad Mohanty


School of Computer Engineering
KIIT Deemed to be University
Bhubaneswar, India
Beyond Scanning and Parsing
 A compiler must do more than just recognize whether a sentence belongs to a
programming language grammar.
• An input program can be grammatically correct but may contain other errors
that prevent compilation.
• Lexer and parser cannot catch all program errors.
 Some language features cannot be modeled using context-free grammar (CFG)
• Whether a variable has been declared before use?
• Parameter types and numbers match in the declaration and use of a function.
• Types match on both sides of an assignment.

CD / Module-IV / Jasaswi 2 21 March 2024


Limitations with CFGs
 CFGs deal with syntactic categories rather than specific words.
• Grammar can specify the positions in an expression where a variable name
may occur.
 Enforcing the “declare before use” rule requires knowledge that cannot be
encoded in a CFG.
• CFG cannot match one instance of a variable name with another.

CD / Module-IV / Jasaswi 3 21 March 2024


Questions That Compiler Needs to Answer
 Has a variable been declared?
 What is the type and size of a variable?
 Is the variable scalar or an array?
 Is an array access A[i][j][k] consistent with the declaration?
 Does the name “x” correspond to a variable or a function?
 If x is a function, how many arguments does it take?
 What kind of value, if any, does a function x return?
 Are all invocations of a function consistent with the declaration?
 Track inheritance relationship.
 Ensure that classes and its methods are not multiply defined.

Finding answers to these questions is part of the semantic analysis phase.


CD / Module-IV / Jasaswi 4 21 March 2024
Checking Dynamic Semantics
 Dynamic semantics of languages need to be checked at run time.
• Whether an overflow will occur during an arithmetic operation?
• Whether array bounds will be exceeded during execution?
• Whether recursion will exceed stack limits?
 Compilers can generate code to check dynamic semantics.

CD / Module-IV / Jasaswi 5 21 March 2024


How does a compiler answer these questions?
 Compilers track additional information for semantic analysis.
• For example, types of variables, function parameters, and array dimensions.
Type information is stored in the symbol table or the syntax tree.
• Used not only for semantic validation but also for subsequent phases of
compilation.
• The information required may be non-local in some cases.
 Semantic analysis can be performed during parsing or in another pass that
traverses the IR produced by the parser.

CD / Module-IV / Jasaswi 6 21 March 2024


How does a compiler answer these questions?
 Use formal methods like context-sensitive grammars.
 Use ad-hoc techniques using symbol table.
 Static semantics of PL can be specified using attribute grammars.
 Attribute grammars are extensions of context-free grammars.

CD / Module-IV / Jasaswi 7 21 March 2024


Attribute Grammar Framework
Syntax-Directed Definition
 A syntax-directed definition (SDD) is a context-free grammar with rules and
attributes.
SDD = CFG + Semantic Rules
 A SDD specifies the values of attributes by associating semantic rules with the
grammar productions.

𝑃 → 𝑃1 + 𝑄 𝑃. 𝑣𝑎𝑙 = 𝑃1 . 𝑣𝑎𝑙 + 𝑄. 𝑣𝑎𝑙


𝑃→𝑄 𝑃. 𝑣𝑎𝑙 = 𝑄. 𝑣𝑎𝑙
 Attributes are associated with grammar symbols and Semantic rules are
associated with production.
 Attributes may be of any kind: numbers, strings, references, datatypes, tables
etc.
CD / Module-IV / Jasaswi 9 21 March 2024
Syntax-Directed Definition
 Generalization of CFG where each grammar symbol has an associated set of
attributes.
• Let 𝐺 = (𝑇, 𝑁𝑇, 𝑆, 𝑃) be a CFG and let 𝑉 = 𝑇 ∪ 𝑁𝑇
• Every symbol 𝑋 ∈ 𝑉 is associated with a set of attributes (for e.g., denoted by
𝑋. 𝑎 and 𝑋. 𝑏)
• Each attribute takes values from a specified domain (finite or infinite), which is
its type
Typical domains of attributes are, integers, reals, characters, strings,
booleans, and structures,
• New domains can be constructed from given domains by mathematical
operations such as cross product and map
 Values of attributes are computed by semantic rules.

CD / Module-IV / Jasaswi 10 21 March 2024


Types of Attributes
There are two kinds of attributes for nonterminals:
 A synthesized attribute for a nonterminal A at Example: 𝐴 → 𝐵𝐶𝐷 (Here A is the parent
a parse-tree node N is defined by a semantic node and B, C, D are child nodes)
rule associated with the production at N. Note 𝐴. 𝑆 = 𝐵. 𝑆
that the production must have A as its head. A
synthesized attribute at node N is defined only 𝐴. 𝑆 = 𝐶. 𝑆
in terms of attribute values at the children of N 𝐴. 𝑆 = 𝐷. 𝑆
and at N itself. Here S is the synthesized attribute.
 An inherited attribute for a nonterminal B at a Example: 𝐴 → 𝐵𝐶𝐷 (Here A is the parent
parse-tree node N is defined by a semantic rule node and B, C, D are child nodes)
associated with the production at the parent of
𝐶. 𝑖 = 𝐴. 𝑖
N. Note that the production must have B as a
symbol in its body. An inherited attribute at 𝐶. 𝑖 = 𝐵. 𝑖
node N is defined only in terms of attribute 𝐶. 𝑖 = 𝐷. 𝑖
values at N's parent, N itself, and N's siblings.
Here 𝑖 is the inherited attribute.

CD / Module-IV / Jasaswi 11 21 March 2024


SDD of a simple desk calculator
 The following SDD is based on the grammar for arithmetic expressions with operators + and *.
 It evaluates expressions terminated by an endmarker 𝒏.
 In the SDD, each of the nonterminals has a single synthesized attribute, called 𝒗𝒂𝒍.
 The terminal 𝒅𝒊𝒈𝒊𝒕 has a synthesized attribute 𝒍𝒆𝒙𝒗𝒂𝒍, which is an integer value returned by the
lexical analyzer.

CD / Module-IV / Jasaswi 12 21 March 2024


Evaluating an SDD
 The rules of an SDD are applied by first constructing a
parse tree and then using the rules to evaluate all of
the attributes at each of the nodes of the parse tree.
 A parse tree, showing the value(s) of its attribute(s) is
called an annotated parse tree.
 In what order do we evaluate attributes?
• SDDs do not specify any order of evaluation. Annotated parse tree for
• We must evaluate all the attributes upon which the the input string 3 * 5 + 4 n
attribute of a node depends.
• Any topological sort of dependency graph gives a
valid order in which semantic rules must be
evaluated.

CD / Module-IV / Jasaswi 13 21 March 2024


Evaluating an SDD
 If all attributes are synthesized, (as shown in the table),
then we must evaluate the 𝒗𝒂𝒍 attributes at all of the
children of a node before we can evaluate the 𝒗𝒂𝒍 attribute
at the node itself.
• With synthesized attributes, we can evaluate attributes
in any bottom-up order, such as that of a postorder
traversal of the parse tree.
 For SDD’s with both synthesized and inherited attributes,
there is no guarantee of an order of evaluation existing.
• Consider the nonterminals 𝐴 and B, with synthesized
and inherited attributes A.s and B.i, respectively, along
with the production and rules.
• These rules are circular for which it is impossible to
evaluate either A.s at a node N or B.i at the child of N
without first evaluating the other.

CD / Module-IV / Jasaswi 14 21 March 2024


Annotated Parse Tree: Example 1
 The annotated parse tree for the input string 3 ∗ 5 + 4 𝑛 is shown below using the
following grammar and rules.
 The values of 𝑙𝑒𝑥𝑣𝑎𝑙 are presumed supplied by the lexical analyzer.
 Each of the nodes for the nonterminals has attribute 𝑣𝑎𝑙 computed in a bottom-up order.

Grammar and Rules Annotated Parse Tree


CD / Module-IV / Jasaswi 15 21 March 2024
Annotated Parse Tree: Example 2
 The annotated parse tree for the input string 3 ∗ 5 is shown below using the following
grammar and rules.
 Each of the nonterminals 𝑇 and 𝐹 has a synthesized attribute 𝑣𝑎𝑙.
 The terminal digit has a synthesized attribute 𝑙𝑒𝑥𝑣𝑎𝑙.
 The nonterminal 𝑇′ has two attributes: an inherited attribute 𝑖𝑛ℎ and a synthesized
attribute 𝑠𝑦𝑛.

Grammar and Rules Annotated Parse Tree


CD / Module-IV / Jasaswi 16 21 March 2024
Annotated Parse Tree: Example 3
 Consider the following grammar
for signed binary numbers: Production Attribute Rule

+
 The attributes associated with
each grammar symbols are as
follows:

 The attribute grammar with the


rules of each production is
shown in the side table.
CD / Module-IV / Jasaswi 17 21 March 2024
Annotated Parse Tree: Example 3
 The annotated parse tree for the input string −101 is shown below:

Production Attribute Rule

CD / Module-IV / Jasaswi 18 21 March 2024


Dependency Graph
 If an attribute 𝑏 depends on an attribute 𝑐 then the semantic rule for 𝑏 must be
evaluated after the semantic rule for 𝑐.
 The dependencies among the nodes are depicted by a directed graph called
dependency graph.

CD / Module-IV / Jasaswi 19 21 March 2024


Examples of Dependency Graph

Example 1 Example 2

CD / Module-IV / Jasaswi 20 21 March 2024


Examples of Dependency Graph

Grammar and Rules Dependency Graph for float 𝑥, 𝑦, z


Example 3
CD / Module-IV / Jasaswi 21 21 March 2024
Constructing a Dependency Graph

CD / Module-IV / Jasaswi 22 21 March 2024


Evaluating an SDD
 In what order do we evaluate attributes?
• SDDs do not specify any order of evaluation
• We must evaluate all the attributes upon which the attribute of a node
depends
• Any topological sort of dependency graph gives a valid order in which
semantic rules must be evaluated.
 If there is any cycle in the graph, then there are no topological sorts; that is, there
is no way to evaluate the SDD on this parse tree.
 If there are no cycles, however, then there is always at least one topological sort.
 For SDD’s with both synthesized and inherited attributes, there is no guarantee
of an order of evaluation existing.

CD / Module-IV / Jasaswi 23 21 March 2024


Examples of Order of Evaluation of attributes

 The dependency graph has


no cycles.
 Few of the topological
sorted orders in which we
may evaluate the attributes
are:
 1,2,3,4,5,6,7,8,9.
 1,3,5,2,4,6,7,8,9

CD / Module-IV / Jasaswi 24 21 March 2024


Types of SDD
S-attributed SDD L-attributed SDD
 A SDD that uses only synthesized attributes is  A SDD that uses both synthesized and
called S-attributed SDD. inherited attributes is called L-attributed SDD.
 It is also called postfix SDD.  Each inherited attribute is restricted to inherit
 Semantic actions are always placed at right from parent or left siblings only.
end of production.  Semantic actions are placed anywhere in
 Attributes are evaluated with Bottom-up or RHS.
post-order traversal of the parse tree.  Attributes are evaluated by traversing parse
 An S attributed SDD can be implemented tree depth first, left to right parsing manner.
naturally in conjunction with an LR parser.  Example:
 Example: 𝐴 → 𝑋𝑌𝑍
𝐴 → 𝐵𝐶𝐷 Here attribute 𝑌 can only obtain its value
Here attribute 𝐴 can only obtain its value either from the parent 𝐴 or its left sibling 𝑋 but
either from 𝐴, 𝐵, 𝐶 or 𝐷. it can’t inherit from its right sibling 𝑍. Similarly,
𝑋 can only get its value from its parent 𝐴 & 𝑍
can get its value from 𝐴, 𝑋, & 𝑌 as well.
CD / Module-IV / Jasaswi 25 21 March 2024
S-attributed SDD
Example Evaluation
 An SDD is S-attributed if every attribute is  When an SDD is S-attributed, we can
synthesized. evaluate its attributes in any bottom-up order
of the nodes of the parse tree.
 In the following SDD each attribute, 𝐿. 𝑣𝑎𝑙,
𝐸. 𝑣𝑎𝑙, 𝑇. 𝑣𝑎𝑙, and 𝐹. 𝑣𝑎𝑙 is synthesized.  It is often especially simple to evaluate the
attributes by performing a post-order traversal
Hence, the following SDD is S-attributed. of the parse tree and evaluating the attributes
at a node N when the traversal leaves N for
the last time.

 S-attributed definitions can be implemented


during bottom-up parsing, since a bottom-up
parse corresponds to a post-order traversal.
CD / Module-IV / Jasaswi 26 21 March 2024
L-attributed SDD
 In a L-attributed SDD, each attribute must be either:  The following SDD is L-attributed
1. Synthesized, or because 𝑇 ′ . 𝑖𝑛ℎ and 𝑇1 ′ . 𝑖𝑛ℎ are
inherited attributes whereas all the
2. Inherited, but with the rules limited as follows. remaining attributes are synthesized
Suppose that there is a production 𝐴 → 𝑋1 𝑋2 … 𝑋𝑛 , attributes.
and that there is an inherited attribute 𝑋𝑖. 𝑎
computed by a rule associated with this production.
Then the rule may use only:
a) Inherited attributes associated with the head 𝐴.
b) Either inherited or synthesized attributes
associated with the occurrences of symbols
𝑋1 𝑋2 … 𝑋𝑖−1 located to the left of 𝑋𝑖.
c) Inherited or synthesized attributes associated  Any SDD containing the following
with this occurrence of 𝑋𝑖 itself, but only in such production and rules cannot be L-
a way that there are no cycles in a dependency attributed:
graph formed by the attributes of this 𝑋𝑖.

CD / Module-IV / Jasaswi 27 21 March 2024


Notes about Inherited Attributes
 Always possible to rewrite a SDD to use only synthesized attributes.
• Inherited attributes can be simulated with synthesized attributes.
 May be more logical to use both synthesized and inherited attributes.
 Inherited attributes usually cannot be evaluated by a simple preorder traversal of
the parse tree.
• Attributes may depend on both left and right siblings!.
• Attributes that do not depend from right children can be evaluated by a
preorder traversal.

CD / Module-IV / Jasaswi 28 21 March 2024


Bottom-up Evaluation of S-Attributed Definitions
 Suppose 𝐴 → 𝑋𝑌𝑍, and semantic rule is 𝐴. 𝑎 = 𝑓(𝑋. 𝑥, 𝑌. 𝑦, . 𝑍. 𝑧).
 Can be computed during bottom-up parsing.
• On reduction, value of new synthesized attribute 𝐴. 𝑎 is computed from the
attributes on the stack.
• Extend stack to hold values.

CD / Module-IV / Jasaswi 29 21 March 2024


Bottom-up Evaluation of S-Attributed Definition

CD / Module-IV / Jasaswi 30 21 March 2024


Are these SDDs S- or L-attributed?

CD / Module-IV / Jasaswi 31 21 March 2024


Relationship between S-Attributed and L-Attributed Definitions

CD / Module-IV / Jasaswi 32 21 March 2024

You might also like