Attribute Grammar: A Compiler Design Example, Study notes of Programming Languages

An example of attribute grammars, focusing on their use in prototyping compilers. The example illustrates the translation of assignment statements and the allocation of temporary storage for expression evaluation using an attribute grammar. Bnf production rules and semantic rules.

Typology: Study notes

Pre 2010

Uploaded on 03/19/2009

koofers-user-efw
koofers-user-efw 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
22C:185
page 1
Attribute Grammar Example
This example illustrates the most common use of attribute grammars —
prototyping compilers. It is adapted from B. Courcelle “Attribute grammars: theory
and applications”,
Formalization of Programming Concepts
, Lect. Notes in Comp.
Sci., V.107.
This example concerns itself with only a small part of a compiler, namely
translating assignment statements. In particular, it focuses on the process of
allocating temporary storage for the evaluation of expressions. For instance, in
the expression a+b*c, the result of b*c must first be computed and stored in
some memory location d, and then the addition a+d performed. In complicated
expressions, numerous intermediate results must be generated and the locations
where they are stored retained for later use as appropriate. This methodology is
described here by means of an attribute grammar.
Some pool of available temporary memory locations must be assumed. In order
to focus on the desired issues, this is taken to be the symbolic locations L0, L1,
L2, … Courcelle expresses the code for an assignment statement in terms of
“three address instructions”. That is, the usual precedence is used to resolve the
order of operations, and suitable temporary locations are determined, but the
code is symbolically indicated to avoid computer architecture details. Also,
temporary locations are reused when their prior use is completed. For example,
the assignment
X := 3.14 * (X+Y)
will be translated into
L0 := 3.14;
L1 := X;
L2 := Y;
L1 := L1+L2;
L0 := L0*L1;
X:= L0.
This attribute grammar uses the attribute ‘free’ to denote the first unused
temporary memory location available, the attribute ‘res’ to denote the memory
location used to hold the result of an expression, term, etc., and ‘code’ to denote
the sequence of instructions for an assignment, expression, etc. The semantic
rules also use the function ‘next’ applied to temporary memory locations with the
obvious result (e.g., next(L0) = L1, etc.).
pf3
pf4

Partial preview of the text

Download Attribute Grammar: A Compiler Design Example and more Study notes Programming Languages in PDF only on Docsity!

Attribute Grammar Example This example illustrates the most common use of attribute grammars — prototyping compilers. It is adapted from B. Courcelle “Attribute grammars: theory and applications”, Formalization of Programming Concepts, Lect. Notes in Comp. Sci., V.107. This example concerns itself with only a small part of a compiler, namely translating assignment statements. In particular, it focuses on the process of allocating temporary storage for the evaluation of expressions. For instance, in the expression a+bc, the result of bc must first be computed and stored in some memory location d, and then the addition a+d performed. In complicated expressions, numerous intermediate results must be generated and the locations where they are stored retained for later use as appropriate. This methodology is described here by means of an attribute grammar. Some pool of available temporary memory locations must be assumed. In order to focus on the desired issues, this is taken to be the symbolic locations L 0 , L 1 , L 2 , … Courcelle expresses the code for an assignment statement in terms of “three address instructions”. That is, the usual precedence is used to resolve the order of operations, and suitable temporary locations are determined, but the code is symbolically indicated to avoid computer architecture details. Also, temporary locations are reused when their prior use is completed. For example, the assignment X := 3.14 * (X+Y) will be translated into L 0 := 3.14; L 1 := X; L 2 := Y; L 1 := L 1 +L 2 ; L 0 := L 0 *L 1 ; X:= L 0. This attribute grammar uses the attribute ‘free’ to denote the first unused temporary memory location available, the attribute ‘res’ to denote the memory location used to hold the result of an expression, term, etc., and ‘code’ to denote the sequence of instructions for an assignment, expression, etc. The semantic rules also use the function ‘next’ applied to temporary memory locations with the obvious result (e.g., next(L 0 ) = L 1 , etc.).

BNF Semantic Rules

  1. Æ := asn.code = [exp.code ; id:= exp.res] asn.free = L 0 exp.free = asn.free
  2. Æ (^) exp.code = trm.code exp.res = trm.res trm.free = exp.free
  3. 0 Æ 1 + (^) exp 0 .code = [exp 1 .code ; trm.code ; exp 1 .res:= exp 1 .res+trm.res] exp 0 .res = exp 1 .res exp 1 .free = exp 0 .free trm.free = next(exp 1 .res)
  4. Æ (^) trm.code = fct.code trm.res = fct.res fct.free = trm.free
  5. 0 Æ 1 * trm 0 .code = [trm 1 .code ; fct.code ; trm 0 .res:= trm 1 .res*fct.res] trm 0 .res = trm 1 .res trm 1 .free = trm 0 .free fct.free = next(trm 1 .res)
  6. Æ ( ) (^) fct.code = exp.code fct.res = exp.res exp.free = fct.free
  7. Æ fct.code = fct.free:= id fct.res = fct.free
  8. Æ (^) fct.code = fct.free:= const fct.res = fct.free The attribute grammar above omits subtraction and division operations for brevity, but they would be treated entirely similarly to table entries 3 and 5, respectively. The attribute dependencies in this example are rather intricate. These are perhaps best depicted in terms of the associated derivation tree nodes. So for production #1 there are the following dependencies, where the arrow shows the direction of information flow. code free := code res free

id asn := exp trm trm *^ fct fct const

( (^) exp ) exp +^ trm trm fct id X fct id Y

X

f=L 0 f=L 0 f=L 0 f=L 0 f=L 0 f=L 1 f=L 1 f=L 1 f=L 1 f=L 1 f=L^2 f=L 2 r=L 0 r=L 0 r=L 0 r=L 0 r=L 1 r=L 1 r=L 1 r=L 1 r=L 1 r=L^2 r=L 2 c=[L 2 :=Y] c=[L 1 :=X] c=[L 1 :=X] c=[L 1 :=X] c=[L 2 :=Y] c=[L 1 :=X; L 2 :=Y; L 1 :=L 1 +L 2 ] c=[L 1 :=X; L 2 :=Y; L 1 :=L 1 +L 2 ] c=[L 0 :=3.14] c=[L 0 :=3.14] A partially completed attribute evaluation tree.