Download Beyond Syntax, Attributes Grammar - Compiler Construction - Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!
Compiler
Construction
Lecture 31
2
Beyond Syntax
These questions are part of
context-sensitive analysis
Answers depend on values,
not parts of speech
Answers may involve
computation
4
Beyond Syntax
These questions are part of
context-sensitive analysis
Answers depend on values,
not parts of speech
Answers may involve
computation
5
Beyond Syntax
How can we answer these
questions?
Use formal methods
grammars
7
Beyond Syntax
How can we answer these
questions?
Use formal methods
grammars
8
Beyond Syntax
Use ad-hoc techniques
- Symbol tables
- ad-hoc code
10
Attribute Grammars
A CFG is augmented with a
set of rules
Each symbol in the derivation
has a set of values or
attributes
Rules specify how to compute
a value for each attribute
11
Attribute Grammars
A CFG is augmented with a
set of rules
Each symbol in the derivation
has a set of values or
attributes
Rules specify how to compute
a value for each attribute
13
Attribute Grammars
derivation for “–1”
Number → Sign List
→ – List
→ – Bit
14
Attribute Grammars
For “-101”
Number → Sign List
→ Sign List Bit
→ Sign List 1
→ Sign List Bit 1
→ Sign List 0 1
→ Sign Bit 0 1
→ Sign 1 0 1
16
Attribute Grammars
We will add rules to
compute decimal value of a
signed binary number
17
Attribute Grammars
Productions Attribution Rules
Number → Sign List List.pos ← 0 if Sign.neg then Number.val ← – List.val else Number.val ← List.val
Sign → + (^) Sign.neg ← false
Sign → – (^) Sign.neg ← true
19
Attribute Grammars
Productions Attribution Rules List 0 → List 1 Bit List 1 .pos ← List 0 .pos + 1 Bit.pos ← List 0 .pos List 0 .val ← List 1 .val + Bit.val
List → Bit Bit.pos ← List.pos List.val ← Bit.val
Bit → 0 Bit.val ← 0
Bit → 1 Bit.val ← 2 Bit.pos
20
Attribute Grammars
Attributes are associated with
nodes in parse tree
Rules are value assignments
associated with productions