cs 335: semantic analysis...cs 335: semantic analysis swarnendu biswas semester 2019-2020-ii cse,...
TRANSCRIPT
CS 335: Semantic AnalysisSwarnendu Biswas
Semester 2019-2020-II
CSE, IIT Kanpur
Content influenced by many excellent references, see References slide for acknowledgements.
An Overview of Compilation
CS 335 Swarnendu Biswas
lexical analyzer
semantic analyzer
source program
syntax analyzer code optimizer
code generator
intermediate code generator
target program
error handler
symbol table
Beyond Scanning and Parsing
β’ Example 1std::string x; int y;y = x + 3
β’ Example 2int a, b;a = b + c
int dot_prod(int x[], int y[]) {int d, i; d = 0;for (i=0; i<10; i++)
d += x[i]*y[i];return d;
}
main() {int p, a[10], b[10];p = dot_prod(a,b);
}
CS 335 Swarnendu Biswas
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
CS 335 Swarnendu Biswas
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
CS 335 Swarnendu Biswas
πππππππ’πππ΅πππ¦ β π·πππππππ‘ππππ πΈπ₯πππ’π‘πππππ
Questions That Compiler Needs to Answer
Questions
β’ 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
CS 335 Swarnendu Biswas
Questions That Compiler Needs to Answer
CS 335 Swarnendu Biswas
π₯ β π¦ + π§ π₯ β π + π
Compilers need to understand the structure of the computation to translate the input program
Semantic Analysis
β’ Finding answers to these questions is part of the semantic analysis phase β’ For example, ensure variable are declared before their uses and check that
each expression has a correct type
β’ These are static semantics of languages
CS 335 Swarnendu Biswas
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
int dot_prod(int x[], int y[]) {int d, i; d = 0;for (i=0; i<10; i++)
d += x[i]*y[i];return d;
}
main() {int p; int a[10], b[10];p = dot_prod(a,b);
}
CS 335 Swarnendu Biswas
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
CS 335 Swarnendu Biswas
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
CS 335 Swarnendu Biswas
Syntax-Directed Definition
β’ A syntax-directed definition (SDD) is a context-free grammar with rules and attributesβ’ A SDD specifies the values of attributes by associating semantic rules with the
grammar productions
β’ Attribute grammars are SDDs with no side effects
CS 335 Swarnendu Biswas
Production Semantic Rule
πΈ β πΈ1 + π πΈ. ππππ = πΈ1. ππππ||π. ππππ||" + "
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
CS 335 Swarnendu Biswas
Example
β’ Consider a grammar for signed binary numbers
β’ Build attribute grammar that annotates ππ’ππππ with the value it represents
CS 335 Swarnendu Biswas
ππ’ππππ β π πππ πππ π‘π πππ β +| βπππ π‘ β πππ π‘ πππ‘ | πππ‘πππ‘ β 0 | 1
Example
β’ Consider a grammar for signed binary numbers
β’ Build attribute grammar that annotates ππ’ππππ with the value it represents
β’ Associate attributes with grammar symbols
CS 335 Swarnendu Biswas
ππ’ππππ β π πππ πππ π‘π πππ β +| βπππ π‘ β πππ π‘ πππ‘ | πππ‘πππ‘ β 0 | 1
Symbol Attributes
ππ’ππππ π£ππ
π πππ πππ
πππ π‘ πππ , π£ππ
πππ‘ πππ , π£ππ
Example Attribute GrammarProduction Attribute Rule
ππ’ππππ β π πππ πππ π‘ πππ π‘. πππ = 0if π πππ. πππ:ππ’ππππ. π£ππ = βπππ π‘. π£ππ
else:ππ’ππππ. π£ππ = βπππ π‘. π£ππ
π πππ β + π πππ. πππ = false
π πππ β β π πππ. πππ = true
πππ π‘ β πππ‘ πππ‘. πππ = πππ π‘. πππ πππ π‘. π£ππ = πππ‘. π£ππ
πππ π‘0 β πππ π‘1πππ‘ πππ π‘1. πππ = πππ π‘0. πππ + 1πππ‘. πππ = πππ π‘0. πππ πππ π‘0. π£ππ = πππ π‘1. π£ππ + πππ‘. π£ππ
πππ‘ β 0 πππ‘. π£ππ = 0
πππ‘ β 1 πππ‘. π£ππ = 2πππ‘.πππ
CS 335 Swarnendu Biswas
Parse Tree
CS 335 Swarnendu Biswas
1 0 1
πππ‘
πππ π‘ πππ‘
πππ π‘ πππ‘
π πππ πππ π‘
ππ’ππππ
β
Annotated Parse Tree
β’ A parse tree showing the value(s) of its attribute(s) is called an annotated parse tree
CS 335 Swarnendu Biswas
1 0 1
πππ‘
πππ π‘ πππ‘
πππ π‘ πππ‘
π πππ πππ π‘
ππ’ππππ
β
Annotated Parse Tree
β’ A parse tree showing the value(s) of its attribute(s) is called an annotated parse tree
CS 335 Swarnendu Biswas
1 0 1
πππ‘
πππ π‘ πππ‘
πππ π‘ πππ‘
π πππ πππ π‘
ππ’ππππ
β
πππ = 0
πππ =1 πππ = 0
πππ = 2 πππ =1
πππ = 2
1
2
3
2
3
4
Annotated Parse Tree
β’ A parse tree showing the value(s) of its attribute(s) is called an annotated parse tree
CS 335 Swarnendu Biswas
1 0 1
πππ‘
πππ π‘ πππ‘
πππ π‘ πππ‘
π πππ πππ π‘
ππ’ππππ
β
πππ = 0
πππ =1 πππ = 0
πππ = 2 πππ =1
πππ = 2
πππ = π‘ππ’π
πππ = π‘ππ’π
π£ππ =?
Annotated Parse Tree
β’ A parse tree showing the value(s) of its attribute(s) is called an annotated parse tree
CS 335 Swarnendu Biswas
1 0 1
πππ‘
πππ π‘ πππ‘
πππ π‘ πππ‘
π πππ πππ π‘
ππ’ππππ
β
πππ = 0
πππ =1 πππ = 0
πππ = 2 πππ =1
πππ = 2
πππ = π‘ππ’π
πππ = π‘ππ’π
π£ππ = 4
π£ππ = 1
π£ππ = 0π£ππ = 4
π£ππ = 4
π£ππ = 5
π£ππ = β5
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
CS 335 Swarnendu Biswas
Dependency Graph
β’ Suppose π΄. π = π π. π₯, π. π¦ is a semantic rule for π΄ β ππ
β’ Suppose π. π₯ = π(π΄. π, π. π¦) is a semantic rule for π΄ β ππ
CS 335 Swarnendu Biswas
π΄
π π π. π¦
π΄. π
π. π₯
π΄
π π π. π¦
π΄. π
π. π₯
Parse tree Dependency graph
Construct Dependency Graph
for each node π in the parse tree do
for each attribute π of the grammar symbol do
construct a node in the dependency graph for π
for each node π in the parse tree do
for each semantic rule π = π(π1, π2, β¦ , ππ) do // Associated with production at node π
for π = 1 to π do
construct an edge from ππ to π
CS 335 Swarnendu Biswas
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
β’ For SDDβs with both synthesized and inherited attributes, there is no guarantee of an order of evaluation existing
CS 335 Swarnendu Biswas
Evaluating an SDD
β’ Parse tree methodβ’ Use topological sort of the dependency graph to find the evaluation order
β’ Rule-based methodβ’ Semantic rules are analyzed and order of evaluation is predetermined
β’ Oblivious methodβ’ Evaluation order ignores the semantic rules
CS 335 Swarnendu Biswas
Circular Dependency of Attributes
Production Semantic Rules
π΄ β π΅ π΄. π = π΅. ππ΅. π = π΄. π + 1
CS 335 Swarnendu Biswas
A
B
π΄. π
π΅. π
Types of Nonterminal Attributes
Synthesized
β’ Value of a synthesized attribute for a nonterminal π΄ at a node π is computed from the values of children nodes and π itself
β’ Defined by a semantic rule associated with a production at π such that the production has π΄ as its head
Inherited
β’ Value of an inherited attribute for a nonterminal π΅ at a node π is computed from the values at πβs parent, π itself, and πβs siblings
β’ Defined by a semantic rule associated with the production at the parent of π such that the production has π΅ in its body
CS 335 Swarnendu Biswas
Syntax-Directed Definition
β’ A grammar production π΄ β πΌ has an associated semantic rule π =π(π1, π2, β¦ , ππ)β’ π is a synthesized attribute of π΄ and π1, π2, β¦, ππ are attributes of symbols in
the production
β’ π is an inherited attribute of a symbol in the body, and π1, π2, β¦, ππ are attributes of symbols in the production
β’ Start symbol does not have inherited attributes
CS 335 Swarnendu Biswas
Terminal Attributes
β’ Terminals can have synthesized attributes, but not inherited attributes
β’ Attributes for terminals have lexical values that are supplied by the lexical analyzer
CS 335 Swarnendu Biswas
Postfix Notation
β’ Postfix notation for an expression πΈ is defined inductivelyβ’ If πΈ is a variable or constant, then postfix notation is πΈ
β’ If πΈ = πΈ1opπΈ2 where op is any binary operator, then the postfix notation is πΈ1β²πΈ2
β²op, where πΈ1β² and πΈ2
β² are postfix notations for πΈ1 and πΈ2 respectively
β’ If πΈ = (πΈ1), then postfix notation for πΈ1 is the notation for πΈ
CS 335 Swarnendu Biswas
SDD for Infix to Postfix Translation
Production Semantic Rules
ππ₯ππ β ππ₯ππ1 + π‘πππ ππ₯ππ. ππππ = ππ₯ππ1. ππππ||π‘πππ. ππππ||"+"
ππ₯ππ β ππ₯ππ1 β π‘πππ ππ₯ππ. ππππ = ππ₯ππ1. ππππ||π‘πππ. ππππ||" β "
ππ₯ππ β π‘πππ ππ₯ππ. ππππ = π‘πππ. ππππ
π‘πππ β 0 1 β¦ | 9
π‘πππ. ππππ = "0"π‘πππ. ππππ = "1"β¦π‘πππ. ππππ = "9"
CS 335 Swarnendu Biswas
Annotated Parse Tree
CS 335 Swarnendu Biswas
ππ₯ππ. ππππ = "95 β 2 + "
ππ₯ππ. ππππ = "95 β " π‘πππ. ππππ = "2"
ππ₯ππ. ππππ = "9" π‘πππ. ππππ = "5"
π‘πππ. ππππ = "9"
"9"
" β "
"5"
" + "
"2"
Types of SDDs
β’ Arbitrary SDDs can have cycles
β’ Cycles need to be avoidedβ’ Can no longer meaningfully proceed with evaluation
β’ Expensive to detect
β’ Two types of SDDs guarantee no cyclesβ’ S-attributed and L-attributed
CS 335 Swarnendu Biswas
S-Attributed Definition
β’ An SDD that involves only synthesized attributes is called S-attributed definitionβ’ Each rule computes an attribute for the head nonterminal from attributes
taken from the body of the production
β’ Semantic rules in a S-attributed definition can be evaluated by a bottom-up or postorder traversal of the parse tree
β’ An S-attributed SDD can be implemented naturally in conjunction with an LRparser
CS 335 Swarnendu Biswas
Example SDD
Production Semantic Rules
πΏ β πΈ $ πΏ. π£ππ = πΈ. π£ππ
πΈ β πΈ1 + π πΈ. π£ππ = πΈ1. π£ππ + π. π£ππ
πΈ β π πΈ. π£ππ = π. π£ππ
π β π1 β πΉ π. π£ππ = π1. π£ππ Γ πΉ. π£ππ
π β πΉ π. π£ππ = πΉ. π£ππ
πΉ β (πΈ) πΉ. π£ππ = πΈ. π£ππ
πΉ β digit πΉ. π£ππ = digit. πππ₯π£ππ
CS 335 Swarnendu Biswas
Annotated Parse Tree for 3 β 5 + 4 $
CS 335 Swarnendu Biswas
π. π£ππ = 4
πΉ. π£ππ = 4
digit. πππ₯π£ππ = 4
πΈ. π£ππ = 15
π. π£ππ = 15
π. π£ππ = 3
πΉ. π£ππ = 3
digit. πππ₯π£ππ = 3
πΉ. π£ππ = 5
digit. πππ₯π£ππ = 5
πΏ. π£ππ = 19
πΈ. π£ππ = 19 $
+
β
Abstract Syntax Tree (AST)
β’ Condensed form of a parse tree used for representing language constructsβ’ ASTs do not check for string membership in the language for a grammarβ’ ASTs represent relationships between language constructs, do not bother
with derivations
β’ Parse trees are also called concrete syntax trees
CS 335 Swarnendu Biswas
π β if π then π1else π2
ifβthenβelse
π΅ π1 π2
Parse Tree and Abstract Syntax Tree
Parse Tree Abstract Syntax Tree
CS 335 Swarnendu Biswas
πΈπ₯ππ
name
πΈπ₯ππ
πΈπ₯ππ + ππππ
β ππππ πΉπππ‘ππ
nameπΉπππ‘ππ
name
ππππ
πΉπππ‘ππ
+
nameβ
name name
Inherited Attributes
β’ Useful when the structure of the parse tree does not match the abstract syntax of the source code
CS 335 Swarnendu Biswas
Production Semantic Rules
π β πΉπβ² πβ². ππβ = πΉ. π£πππ. π£ππ = πβ². π π¦π
πβ² ββ πΉπ1β² π1
β². ππβ = πβ². ππβ Γ πΉ. π£πππβ². π π¦π = π1
β². π π¦π
πβ² β π πβ². π π¦π = πβ². ππβ
πΉ β digit πΉ. π£ππ = digit. πππ₯π£ππ
Parse Tree and Annotated Parse Tree for 3 β 5
CS 335 Swarnendu Biswas
π. π£ππ = 15
β πΉ. π£ππ = 5
πβ². ππβ = 3πβ². π π¦π = 15
πΉ. π£ππ = 3
digit. πππ₯π£ππ = 3
digit. πππ₯π£ππ = 5
π1β². ππβ = 15
π1β². π π¦π = 15
π
β πΉ
πβ²πΉ
digit
digit
π1β²
ππ
Parse Tree and Annotated Parse Tree for 3 β 5
CS 335 Swarnendu Biswas
π. π£ππ = 15
β πΉ. π£ππ = 5
πβ². ππβ = 3πβ². π π¦π = 15
πΉ. π£ππ = 3
digit. πππ₯π£ππ = 3
digit. πππ₯π£ππ = 5
π1β². ππβ = 15
π1β². π π¦π = 15
π
β πΉ
πβ²πΉ
digit
digit
π1β²
ππ
Another Example
CS 335 Swarnendu Biswas
Production Semantic Rules
π· β ππΏ πΏ. ππ = π. π‘π¦ππ
π β float π. π‘π¦ππ = float
π β int π. π‘π¦ππ = int
πΏ β πΏ1, id πΏ1. ππ = πΏ. ππ; ππππ‘π¦ππ(id. πππ‘ππ¦, πΏ. ππ)
πΏ β id ππππ‘π¦ππ(id. πππ‘ππ¦, πΏ. ππ)
Parse Tree for βfloat π₯, π¦, π§β
π·
π πΏ
float πΏ id,
πΏ id,
id
ππππ‘π¦ππ() installs πΏ. ππ as the type of the symbol table object pointed to by id. πππ‘ππ¦
Dependency Graph for float π₯, π¦, π§
CS 335 Swarnendu Biswas
π·
π
float
πΏ
πΏ , id
πΏ , id
id
π‘π¦ππ ππβ πππ‘ππ¦
πππ‘ππ¦
πππ‘ππ¦
πππ‘ππ¦
ππβ πππ‘ππ¦
ππβ πππ‘ππ¦
Evaluating S-Attributed Definitions
β’ Attributes can be evaluated with a postorder traversal of the parse tree
postorder(π) {
for (each child πΆ of π, from left to right)
postorder(πΆ)
evaluate the attributes associated with node π
}
CS 335 Swarnendu Biswas
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
CS 335 Swarnendu Biswas
Bottom-up Evaluation of S-AttributedDefinitionsβ’ 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
CS 335 Swarnendu Biswas
β¦ β¦ π€ $
LR Parsing Program
Input
π. π§
π. π¦
π. π₯
$
π
π
π
$
State stack
Value stack
Example S-Attributed Definition
Production Semantic Rules
πΏ β πΈ $ πΏ. π£ππ = πΈ. π£ππ
πΈ β πΈ1 + π πΈ. π£ππ = πΈ1. π£ππ + π. π£ππ
πΈ β π πΈ. π£ππ = π. π£ππ
π β π1 β πΉ π. π£ππ = π1. π£ππ Γ πΉ. π£ππ
π β πΉ π. π£ππ = πΉ. π£ππ
πΉ β (πΈ) πΉ. π£ππ = πΈ. π£ππ
πΉ β digit πΉ. π£ππ = digit. πππ₯π£ππ
CS 335 Swarnendu Biswas
Bottom-up Evaluation of S-Attributed DefinitionsValue State Input Action
$ $ 3 β 5 + 4$ Shift
$3 $digit β 5 + 4$ Reduce by πΉ β digit
$3 $πΉ β 5 + 4$ Reduce by π β πΉ
$3 $π β 5 + 4$ Shift
$3 $π β 5 + 4$ Shift
$3 5 $π β digit +4$ Reduce by πΉ β digit
$3 5 $π β πΉ +4$ Reduce by π β π β πΉ
$15 $π +4$ Reduce by πΈ β π
$15 $πΈ +4$ Shift
$15 $πΈ + 4$ Shift
$15 4 $πΈ + digit $ Reduce by πΉ β digit
$15 4 $πΈ + πΉ $ Reduce by π β πΉ
$15 4 $πΈ + π $ Reduce by πΈ β πΈ + π
$19 $πΈ $ β¦CS 335 Swarnendu Biswas
L-Attributed Definitions
β’ Each attribute must be either I. Synthesized
II. Suppose π΄ β π1π2β¦ππ and ππ . π is an inherited attribute. ππ . π can be computed using
a) Only inherited attributes from π΄, or
b) Either inherited or synthesized attributes associated with π1, β¦ , ππβ1, or
c) Inherited or synthesized attributes associated with ππ.
CS 335 Swarnendu Biswas
Production Semantic Rules
π β πΉπβ² πβ². ππβ = πΉ. π£πππ. π£ππ = πβ². π π¦π
πβ² ββ πΉπ1β² π1
β². ππβ = πβ². ππβ Γ πΉ. π£πππβ². π π¦π = π1
β². π π¦π
πβ² β π πβ². π π¦π = πβ². ππβ
πΉ β digit πΉ. π£ππ = digit. πππ₯π£ππ
Are these SDDs S- or L-attributed?
Production Semantic Rules
π΄ β π΅πΆπ΄. π = π΅. π1π΅. π2 = π(π΄. π, πΆ. π)
CS 335 Swarnendu Biswas
Production Semantic Rules
π΄ β π΅πΆπ΅. π = π1(π΄. π)πΆ. π = π2(π΅. π )π΄. π = π3(πΆ. π )
Production Semantic Rules
π΄ β π΅πΆπΆ. π = π4(π΄. π)π΅. π = π5(πΆ. π )π΄. π = π6(π΅. π )
S-Attributed and L-Attributed Definitions
Every S-attributed grammar is also a L-attributed grammar
All L-attributed grammars are not S-attributed
CS 335 Swarnendu Biswas
Associating Semantic Rules with Productions
β’ Syntax-directed definition (SDD)β’ Defines a set of attributes and translations at every node of the parse tree,
output is available at the root
β’ Declarative style which hides implementation detailsβ’ Evaluation order is not specified among multiple attributes for a production
β’ Only requirement is there should not be any circularity
CS 335 Swarnendu Biswas
Associating Semantic Rules with Productions
β’ Syntax-directed translation (SDT)β’ Program fragments are embedded as semantic actions in production body
β’ Generates code while parsing
β’ Indicates order in which semantic rules are to be evaluated
β’ Executable specification of an SDD, easier to implement and can be more efficient
β’ Yacc uses translation schemes
CS 335 Swarnendu Biswas
πππ π‘ β +π‘πππ { πππππ‘("+") } πππ π‘1
SDT for Infix to Postfix Translation
SDD
Production Semantic Rules
ππ₯ππβ ππ₯ππ1 + π‘πππ
ππ₯ππ. ππππ =ππ₯ππ1. ππππ||π‘πππ. ππππ||" + "
ππ₯ππβ ππ₯ππ1 β π‘πππ
ππ₯ππ. ππππ =ππ₯ππ1. ππππ||π‘πππ. ππππ||" β "
ππ₯ππ β π‘πππ ππ₯ππ. ππππ = π‘πππ. ππππ
π‘πππ β 0 1 β¦ | 9
π‘πππ. ππππ = "0"π‘πππ. ππππ = "1"β¦π‘πππ. ππππ = "9"
SDT
Production Semantic Rules
ππ₯ππ β ππ₯ππ1 + π‘πππ { πππππ‘ " + " }
ππ₯ππ β ππ₯ππ1 β π‘πππ { πππππ‘ " β " }
ππ₯ππ β π‘πππ
π‘πππ β 0 1 β¦ | 9
{ πππππ‘("0") }{ πππππ‘("1") }β¦{ πππππ‘("9") }
CS 335 Swarnendu Biswas
SDT Actions
CS 335 Swarnendu Biswas
ππ₯ππ
ππ₯ππ π‘πππ
ππ₯ππ π‘πππ
π‘πππ
"9"
" β "
"5"
" + "
"2"{ πππππ‘ " β " }
{ πππππ‘ "5" }
{ πππππ‘ "2" }
{ πππππ‘ "9" }
{ πππππ‘ "+" }
SDDs and SDTs
β’ Evaluation of the semantic rules mayβ’ Generate code
β’ Save information in the symbol table
β’ Issue error messages
β’ Perform any other activity
CS 335 Swarnendu Biswas
input string
parse tree
dependency graph
evaluation order for semantic rules
Construction of AST for Expressions
β’ Idea: Construct subtrees for subexpressions by creating an operator and operand nodes
β’ Internal node: ππππ(ππ, π1, π2, β¦ , ππ)
β’ Create a node with label ππ, and π fields for π children
β’ Leaf node: πΏπππ(ππ, π£ππ)β’ Create a node with label ππ, and π£ππ is the lexical value
CS 335 Swarnendu Biswas
Creating an AST
β’ Following sequence of function calls create an AST for π β 4 + π
1. π1 = new πΏπππ(id, πππ‘ππ¦π)
2. π2 = new πΏπππ(num, 4)
3. π3 = newππππ(β β β, π1, π2)
4. π4 = new πΏπππ(id, πππ‘ππ¦π)
5. π5 = newππππ(β + β, π3, π4)
CS 335 Swarnendu Biswas
num 4id
entry for π
+
β id
entry for π
S-Attributed Definition for Constructing Syntax Trees
Production Semantic Rules
πΈ β πΈ1 + π ???
πΈ β πΈ1 β π ???
πΈ β π ???
π β (πΈ) ???
π β id ???
π β num ???
CS 335 Swarnendu Biswas
S-Attributed Definition for Constructing Syntax Trees
Production Semantic Rules
πΈ β πΈ1 + π πΈ. ππππ = π§ππ° ππππ(" + ", πΈ1. ππππ, π. ππππ)
πΈ β πΈ1 β π πΈ. ππππ = π§ππ° ππππ(" β ", πΈ1. ππππ, π. ππππ)
πΈ β π πΈ. ππππ = π. ππππ
π β (πΈ) π. ππππ = πΈ. ππππ
π β id π. ππππ = π§ππ° πΏπππ(id, id. πππ‘ππ¦)
π β num π. ππππ = π§ππ° πΏπππ(num, num. π£ππ)
CS 335 Swarnendu Biswas
Construction of AST for π β 4 + π
CS 335 Swarnendu Biswas
πΈ
πΈ
πΈ
π
id
β
+ π
idπ
ππππ
ππππ ππππ
ππππ
ππππ
id
num
num 4
idβ
+
entry for π
entry for π
id
ππππ
AST edge
Parse Tree edge
L-Attributed Definition for Constructing Syntax Trees
Production Semantic Rules
πΈ β ππΈβ² πΈ. ππππ = πΈβ². π π¦ππΈβ². ππβ = π. ππππ
πΈβ² β +ππΈ1β² πΈ1
β² . ππβ = πππ€ ππππ(" + ", πΈβ². ππβ, π. ππππ)πΈβ². π π¦π = πΈ1
β² . π π¦π
πΈβ² β βππΈ1β² πΈ1
β² . ππβ = πππ€ ππππ(" β ", πΈβ². ππβ, π. ππππ)πΈβ². π π¦π = πΈ1
β² . π π¦π
πΈβ² β π πΈβ². π π¦π = πΈβ². ππβ
π β (πΈ) π. ππππ = πΈ. ππππ
π β id π. ππππ = π§ππ° πΏπππ(id, id. πππ‘ππ¦)
π β num π. ππππ = π§ππ° πΏπππ(num,num. π£ππ)
CS 335 Swarnendu Biswas
Dependency Graph for π β 4 + π
CS 335 Swarnendu Biswas
πΈ
π
id
ππππ
πΈβ²
π
num
πΈβ²
π πΈβ²
id π
β
+
ππππ
πππ‘ππ¦
π π¦πππβ
π£ππ
ππππ
π π¦πππβππππ
π π¦πππβ
πππ‘ππ¦
Implementing SDTs
β’ SDTs can be implemented by 1. building a parse tree
2. performing the actions in a left-to-right depth-first order, i.e., preorder traversal
β’ SDTs are often implemented during parsing, but without a parse treeβ’ Underlying grammar is LR, and the SDD is S-attributed
β’ Underlying grammar is LL, and the SDD is L-attributed
CS 335 Swarnendu Biswas
Postfix SDT for the Desk Calculator
CS 335 Swarnendu Biswas
β’ Consider S-attributed SDD for a bottom-up grammar
β’ We can construct an equivalent SDT with actions at the end of each production
β’ SDT with all actions at the right-end of a production are called postfix SDT
πΏ β πΈ$ { πππππ‘ πΈ. π£ππ }
πΈ β πΈ1 + π { πΈ. π£ππ = πΈ1. π£ππ + π. π£ππ }
πΈ β π { πΈ. π£ππ = π. π£ππ }
π β π1 β πΉ { π. π£ππ = π1. π£ππ Γ πΉ. π£ππ }
π β πΉ π. π£ππ = πΉ. π£ππ
πΉ β πΈ { πΉ. π£ππ = πΈ. π£ππ }
πΉ β πππππ‘ {πΉ. π£ππ = digit. πππ₯π£ππ }
Implementing Postfix SDTs During LR Parsing
CS 335 Swarnendu Biswas
β¦ β¦ π€ $
LR Parsing Program
Input
π. π§
π. π¦
π. π₯
$
π
π
π
$
State stack
Value stack
β’ Execute actions when reductions take placeβ’ Use a value stack to maintain attributes along
with the states (grammar symbols)β’ Manipulating the stack is done by the LR parser
Implementing Postfix SDTs with Bottom-up Parsing
CS 335 Swarnendu Biswas
Production Actions
πΏ β πΈ$ { πππππ‘ π π‘πππ π‘ππ β 1 . π£ππ ; π‘ππ = π‘ππ β 1 }
πΈ β πΈ1 + π { π π‘πππ π‘ππ β 2 . π£ππ = π π‘πππ π‘ππ β 2 . π£ππ +π π‘πππ π‘ππ . π£ππ; π‘ππ = π‘ππ β 2; }
πΈ β π
π β π1 β πΉ { π π‘πππ π‘ππ β 2 . π£ππ = π π‘πππ π‘ππ β 2 . π£ππ Γπ π‘πππ π‘ππ . π£ππ; π‘ππ = π‘ππ β 2; }
π β πΉ
πΉ β πΈ { π π‘πππ π‘ππ β 2 . π£ππ = π π‘πππ π‘ππ β 1 . π£ππ; π‘ππ =π‘ππ β 2; }
πΉ β πππππ‘
SDT with Actions Inside Productions
β’ For bottom-up parsing, execute action π as soon as π occurs on top of the stack
β’ For top-down parsing, execute action π just before expanding nonterminal π or checking for terminal π in the input
CS 335 Swarnendu Biswas
π΅ β π π π
Example of an SDT Problematic for Parsing
πΏ β πΈ$
πΈ β { πππππ‘("+"); } πΈ1 + π
πΈ β π
π β πππππ‘ " β " ; π1 β πΉ
π β πΉ
πΉ β πΈ
πΉ β πππππ‘ { πππππ‘ digit. πππ₯π£ππ ; }
CS 335 Swarnendu Biswas
Implementation of Any SDT
β’ Parse the input and produce a parse treeβ’ Ignore the actions for this step
β’ Examine each interior node π, say one for production π΄ β πΌβ’ Add additional children to π for the actions in πΌ, in left to right order
β’ Perform a preorder traversal of the tree β’ Perform an action as a node labeled by an action is visited
CS 335 Swarnendu Biswas
Parse Tree with Embedded Actions
CS 335 Swarnendu Biswas
$
πΉ
digit
digit { πππππ‘ 5 ; }
{ πππππ‘ 3 ; }
π πΉβ{ πππππ‘ β² ββ² ; }
πΉπ
digit { πππππ‘ 4 ; }
πΈ + π{ πππππ‘ β² +β² ; }
πΏ
πΈ
β’ Parse tree for 3*5+4β’ Traverse the tree in preorder
Design of Translation Schemes
β’ Make all attribute values available when the semantic action is executed
β’ When semantic action involves only synthesized attributes, the action can be put at the end of the production
CS 335 Swarnendu Biswas
Design of Translation Schemes
β’ Rules for L-attributed SDDsβ’ An inherited attribute for a symbol
in the body of a production must be computed in an action before the symbol
β’ A synthesized attribute for the nonterminal on the LHS can only be computed when all the attributes it references have been computed β’ The action is usually put at the end
of the production
π β π΄1π΄2 { π΄1. ππ = 1, π΄2. ππ = 2 }
π΄ β π { πππππ‘ π΄. ππ }
CS 335 Swarnendu Biswas
π
π΄2
π πππππ‘(π΄2. ππ)
π΄1
π πππππ‘(π΄1. ππ)
π΄1. ππ = 1π΄2. ππ = 2
What will happen on a DFS?