cs 335: semantic analysis...cs 335: semantic analysis swarnendu biswas semester 2019-2020-ii cse,...

77
CS 335: Semantic Analysis Swarnendu Biswas Semester 2019-2020-II CSE, IIT Kanpur Content influenced by many excellent references, see References slide for acknowledgements.

Upload: others

Post on 10-Mar-2020

15 views

Category:

Documents


4 download

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

Attribute Grammar Framework

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

Syntax-Directed Translation

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?

References

β€’ A. Aho et al. Compilers: Principles, Techniques, and Tools, 2nd edition, Chapter 2.3, Chapter 5.

β€’ K. Cooper and L. Torczon. Engineering a Compiler, 2nd edition, Chapter 4.

β€’ M. Scott. Programming Language Pragmatics, 4th edition, Chapter 4.

CS 335 Swarnendu Biswas