Context-Sensitive Analysis and Attribute Grammars in Compiler Design, Study notes of Computer Science

Context-sensitive analysis in compiler design and the use of attribute grammars to answer context-sensitive questions. It covers the difficulties of context-sensitive analysis, methods for answering these questions, and an example of an attribute grammar for evaluating signed binary numbers.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-0ro
koofers-user-0ro 🇺🇸

9 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Context-sensitive analysis
What context-sensitive questions might the compiler ask?
1. Is xa scalar, an array, or a function?
2. Is xdeclared before it is used?
3. Are any names declared but not used?
4. Which declaration of xdoes this reference?
5. Is an expression type-consistent?
6. Does the dimension of a reference match the declaration?
7. Where can xbe stored? (heap, stack, ...)
8. Does *p reference the result of a malloc()?
9. Is xdefined before it is used?
10. Is an array reference in bounds?
11. Does function foo produce a constant value?
These cannot be answered with a context-free grammar
CMSC 430 Lecture 6, Page 1
Context-sensitive analysis
Why is context-sensitive analysis hard?
need non-local information
answers depend on values, not on syntax
answers may involve computation
How can we answer these questions?
1. use context-sensitive grammars
general problem is P-space complete
2. use attribute grammars
augment context-free grammar with rules
calculate attributes for grammar symbols
3. use ad hoc techniques
augment grammar with arbitrary code
execute code at corresponding reduction
store information in attributes, symbol tables
CMSC 430 Lecture 6, Page 2
Attribute grammars
Attribute grammar
generalization of context-free grammar
each grammar symbol has an associated set of attributes
augment grammar with rules that define values
high-level specification, independent of evaluation scheme
Dependences between attributes
values are computed from constants & other attributes
synthesized attribute value computed from children
inherited attribute value computed from siblings & parent
CMSC 430 Lecture 6, Page 3
Example attribute grammar
A grammar to evaluate signed binary numbers due to Scott K. Warren, Rice Ph.D.
Production Evaluation Rules
1NUM ::= SIGN LIST LIST.pos 0
NUM.val if SIGN.neg
then -LIST.val
else LIST.val
2SIGN ::= +SIGN.neg false
3SIGN ::= -SIGN.neg true
4LIST ::= BIT BIT.pos LIST.pos
LIST.val BIT.val
5LIST0::= LIST1BIT LIST1.pos LIST0.pos + 1
BIT.pos LIST0.pos
LIST0.val LIST1.val + BIT.val
6BIT ::= 0BIT.val 0
7BIT ::= 1BIT.val 2BIT.pos
CMSC 430 Lecture 6, Page 4
pf3
pf4
pf5

Partial preview of the text

Download Context-Sensitive Analysis and Attribute Grammars in Compiler Design and more Study notes Computer Science in PDF only on Docsity!

What context-sensitive questions might the compiler ask?Context-sensitive analysis

  1. Is

x

a scalar, an array, or a function?

  1. Is

x

declared before it is used?

  1. Which declaration of3. Are any names declared but not used?

x

does this reference?

  1. Is an expression

type-consistent?

  1. Where can 6. Does the dimension of a reference match the declaration?

x

be stored?

(heap, stack,... )

  1. Does

*p

reference the result of a

malloc()

  1. Is

x

defined before it is used?

  1. Is an array reference

in bounds?

  1. Does function

foo

produce a constant value?

These cannot be answered with a context-free grammar CMSC 430

Lecture 6, Page 1

Why is context-sensitive analysis hard? Context-sensitive analysis

need non-local information

answers depend on values, not on syntax

answers may involve computation

How can we answer these questions?

  1. use context-sensitive grammars

general problem is P-space complete

  1. use attribute grammars

augment context-free grammar with rules

calculate attributes for grammar symbols

  1. use

ad hoc

techniques

augment grammar with arbitrary code

execute code at corresponding reduction

store information in attributes, symbol tables

CMSC 430

Lecture 6, Page 2

Attribute grammar Attribute grammars

generalization of context-free grammar

each grammar symbol has an associated set of attributes

augment grammar with rules that define values

high-level specification, independent of evaluation scheme

Dependences between attributes

values are computed from constants & other attributes

synthesized attribute

  • value computed from children

inherited attribute

  • value computed from siblings & parent

CMSC 430

Lecture 6, Page 3

A grammar to evaluate signed binary numbers Example attribute grammar

due to Scott K. Warren, Rice Ph.D.

Production

Evaluation Rules

NUM

SIGN LIST

LIST

.pos

NUM

.val

if

SIGN

.neg

then -

LIST

.val

else

LIST

.val

SIGN

SIGN

.neg

false

SIGN

SIGN

.neg

true

LIST

BIT

BIT

.pos

LIST

.pos

LIST

.val

BIT

.val

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

BIT

BIT

.val

BIT

BIT

.val

BIT

.pos

CMSC 430

Lecture 6, Page 4

Example attribute grammar

val: -

NUM

neg: T

SIGN

val: 5^ pos: 0

LIST

val: 4^ pos: 1

LIST

val: 4^ pos: 2

LIST

val: 4^ pos: 2

BIT

val: 0^ pos: 1

BIT

val: 1^ pos: 0

BIT

val

and

neg

are

synthesized

attributes

pos

is an

inherited

attribute

CMSC 430

Lecture 6, Page 5

Disadvantages of attribute grammars Syntax-directed translation

handling non-local information, locating answers

storage management, avoiding circular evaluation

Syntax-directed translation

allow arbitrary actions

provide central repository

can place actions amid production

Examples

YACC — A ::= B C

$$ = concat($1,$2);

CUP — A:n ::= B:m C:p

: n = concat(m,p); :

Typical uses

build abstract syntax tree & symbol table

perform error/type checking

CMSC 430

Lecture 6, Page 6

most non-terminal symbols removed. An abstract syntax tree (AST) is the procedure’s parse tree with the nodes for Abstract syntax tree

id,

x

num,

id,

y

This represents “

x - 2 * y

For ease of manipulation, can use a linearized (operator) form of the tree.

x 2 y * -

in postfix form.

A popular

intermediate representation

CMSC 430

Lecture 6, Page 7

A Symbol tables

symbol table

associates values or attributes

e.g., types and values

) with names.

What should be in a symbol table?

variable and procedure names

literal constants and strings

What information might compiler need?

textual name

data type

declaring procedure

lexical level of declaration

if array, number and size of dimensions

if procedure, number and type of parameters

CMSC 430

Lecture 6, Page 8

Grammar for source language:arrays, pointers, statements, and functions. Using a synthesized attribute grammar, we will describe a type checker forA simple type checker P

::= D ; E

D ::= D ; E

id

: T

T

char

integer

array [num] of

T

T

E

literal

num

id

E

mod

E

E[E]

E

Basic types

char

integer

typeError

assume all arrays start at 1,

e.g.,

array [256] of char

results in the type expression

array

char

builds a pointer type, so

integer

results in the type expression

pointer(integer)

CMSC 430

Lecture 6, Page 13

Partial attribute grammar for the type system Type checking example D ::=

id

: T

addtype

id

.entry, T.type

T

char

T.type

char

T

integer

T.type

integer

T

T

1

T.type

pointer

T

1 .type

T

array [num] of

T

1

T.type

array

num

.val, T

1 .type

CMSC 430

Lecture 6, Page 14

Each expression is assigned a type using rules associated with the grammar. Type checking expressions E ::=

literal

E.type

char

E ::=

num

E.type

integer

E ::=

id

E.type

lookup

id

. entry

E ::= E

1

mod

E

2

E.type

if

E

1 .type = integer

and

E

2 .type = integer

then

integer

else

typeError

E ::= E

1 [E

2 ]

E.type

if

E

2 .type = integer

and

E

1 .type = array

s,t

then

t

else

typeError

E ::= E

1

E.type

if

E

1 .type = pointer

then

t else

typeError

CMSC 430

Lecture 6, Page 15

void Statements do not typically have values, therefore we assign them the type Type checking statements

. If an error is detected within the statement, it gets type

typeError

S ::=

id

E

S.type

if id.

type = E.type

then

void

else

typeError

S ::=

if

E

then

S

1

S.type

if

E.type = boolean

then

S

1 .type

else

typeError

S ::=

while

E

do

S

1

S.type

if

E.type = boolean

then

S

1 .type

else

typeError

S ::= S

1 ; S

2

S.type

if

S 1 .type = void

then

void

else

typeError

CMSC 430

Lecture 6, Page 16

and applications We add two new productions to the grammar to represent function declarationsType checking functions T ::= T

T

declaration

E

::= E ( E )

application

To capture the argument and return type, we use T ::= T

1

T

2

T.type

T

1 .type

T

2 .type

E

::= E

1

( E

2 )

E.type

if

E

1 .type = s

t

and

E

2 .type = s

then

t

else

typeError

CMSC 430

Lecture 6, Page 17