



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 5
This page cannot be seen from the preview
Don't miss anything!




What context-sensitive questions might the compiler ask?Context-sensitive analysis
x
a scalar, an array, or a function?
x
declared before it is used?
x
does this reference?
type-consistent?
x
be stored?
(heap, stack,... )
*p
reference the result of a
malloc()
x
defined before it is used?
in bounds?
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?
general problem is P-space complete
augment context-free grammar with rules
calculate attributes for grammar symbols
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
inherited attribute
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
id
: T
char
integer
array [num] of
literal
num
id
mod
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
char
T.type
char
integer
T.type
integer
1
T.type
pointer
1 .type
array [num] of
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
num
E.type
integer
id
E.type
lookup
id
. entry
1
mod
2
E.type
if
E
1 .type = integer
and
2 .type = integer
then
integer
else
typeError
1 [E
2 ]
E.type
if
E
2 .type = integer
and
1 .type = array
s,t
then
t
else
typeError
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
id
S.type
if id.
type = E.type
then
void
else
typeError
if
then
1
S.type
if
E.type = boolean
then
1 .type
else
typeError
while
do
1
S.type
if
E.type = boolean
then
1 .type
else
typeError
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
declaration
application
To capture the argument and return type, we use T ::= T
1
→
2
T.type
1 .type
2 .type
1
( E
2 )
E.type
if
E
1 .type = s
t
and
2 .type = s
then
t
else
typeError
CMSC 430
Lecture 6, Page 17