Beyond Syntax, Attributes Grammar - Compiler Construction - Lecture Notes, Study notes of Compiler Construction

Beyond Syntax, Attribute grammars, Context sensitive analysis, Ad hoc Techniques, Set of rules, Signed binary numbers, Derivation, Attribute version, Decimal value are the points from this lecture. You can find series of lecture notes for compiler construction here.

Typology: Study notes

2011/2012

Uploaded on 11/06/2012

asim.amjid
asim.amjid ๐Ÿ‡ต๐Ÿ‡ฐ

4.4

(47)

41 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Sohail Aslam Compiler Construction Notes Set:5-90
L
Le
ec
ct
tu
ur
re
e
3
31
1
Beyond Syntax
These questions are part of context-sensitive analysis. Answers depend on values, not
parts of speech. Answers may involve computation.
These questions can be answered by using formal methods such as context-sensitive
grammars and attribute grammars or by using ad-hoc techniques.
One of the most popular is the use of attribute grammars.
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
Consider the grammar for signed binary numbers (SBN)
Number โ†’ Sign List
Sign โ†’ + ๏ฃด โ€“
List โ†’ List Bit | Bit
Bit โ†’ 0๏ฃด1
The string โ€œโ€“1โ€ can be derived as follows:
Number โ†’ Sign List
โ†’ โ€“ List
โ†’ โ€“ Bit
โ†’ โ€“ 1
Similarly, the derivation for โ€œ-101โ€ is
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
โ†’ โ€“ 1 0 1
pf3
pf4

Partial preview of the text

Download Beyond Syntax, Attributes Grammar - Compiler Construction - Lecture Notes and more Study notes Compiler Construction in PDF only on Docsity!

Le Leccttuurree 3 311

Beyond Syntax

These questions are part of context-sensitive analysis. Answers depend on values, not parts of speech. Answers may involve computation.

These questions can be answered by using formal methods such as context-sensitive grammars and attribute grammars or by using ad-hoc techniques.

One of the most popular is the use of attribute grammars.

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

Consider the grammar for signed binary numbers (SBN)

Number โ†’ Sign List Sign โ†’ + ๏ฃด โ€“ List โ†’ List Bit | Bit Bit โ†’ 0 ๏ฃด 1

The string โ€œโ€“1โ€ can be derived as follows:

Number โ†’ Sign List โ†’ โ€“ List โ†’ โ€“ Bit โ†’ โ€“ 1

Similarly, the derivation for โ€œ-101โ€ is

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 โ†’ โ€“ 1 0 1

For an attributed version of SBN , the following attributes are needed

Symbol Attributes Number val Sign neg List pos, val Bit pos, val

We will add rules to compute decimal value of a signed binary number

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 List0 โ†’ List1 Bit (^) List1.pos โ† List0.pos + 1 Bit.pos โ† List0.pos List0.val โ† List1.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

Attributes are associated with nodes in parse tree. Rules are value assignments associated with productions. Rules and parse tree define an attribute dependence graph which must be acyclic.

When the parse tree is peeled away, we get the attribute dependence graph

val: -

pos: 0

val: 5

pos: 0

val: 1

pos: 1

val: 4

pos: 1

val: 0

pos: 2

val: 4

pos: 2

val: 4

neg: true