Docsity

Programming Language Compilers - Homework 3 for Attribute Grammar | CS 5363, Assignments of Computer Science

Toggle sidebar
bg1
Homework 3 : Attribute Grammar (20pts)
Due Oct 20, 2008
Suppose we have the following context-free grammar which accepts nested lists such as
(a(b d) (e(f))).
List ::= single
single ::= VAR | ( multi )
multi ::= single | multi single
Here each V AR can be matched to an arbitrary variable name (e.g., a,b,c).
1. Add translation schemes to evaluate the number of variables contained in each nested
list input. For example, for (a(b d) (e(f))), the number of variables should be 5.
Solution: Define a synthesized attribute, count, for each non-terminal (List,single,
and multi) to keep track of the number of variables.
List ::= single {List.count = single.count; }
single ::= VAR {single.count = 1; }
| ( multi ) { single.count = multi.count; }
multi ::= single { multi.count = single.count; }
| multi1 single { multi.count = multi1.count + single.count; }
2. Add translation schemes to evaluate the subscript of each variable in a nested list;
that is, for each variable xin a given input, the number of other variables contained
before x. For example, for (a(b d) (e(f))), the subscripts for a, b, d, e, f are 0, 1, 2,
3, and 4 respectively.
Solution: Keep the synthesized attribute, count, for each non-terminal (List,single,
and multi) to keep track of the number of variables. In addition, define an inherited
attribute, before, for single,multi and V AR to store the number of variables be-
fore the corresponding parse tree. The value of V AR.before is the subscript of the
corresponding variable.
List ::= {single.before=0; } single {List.count = single.count; }
single ::= {VAR.before=single.before; } VAR {single.count = 1; }
| ( {multi.before=single.before; } multi ) { single.count = multi.count; }
multi ::= {single.before = multi.before; } single { multi.count = single.count; }
| {multi1.before = multi.before; } multi1
{single.before = multi.before + multi1.count; } single
{ multi.count = multi1.count + single.count; }
1

Partial preview of the text

Download Programming Language Compilers - Homework 3 for Attribute Grammar | CS 5363 and more Assignments Computer Science in PDF only on Docsity!

Homework 3 : Attribute Grammar (20pts)

Due Oct 20, 2008

Suppose we have the following context-free grammar which accepts nested lists such as (a (b d) (e (f ))).

List ::= single single ::= VAR | ( multi ) multi ::= single | multi single

Here each V AR can be matched to an arbitrary variable name (e.g., a, b, c).

  1. Add translation schemes to evaluate the number of variables contained in each nested list input. For example, for (a (b d) (e (f ))), the number of variables should be 5. Solution: Define a synthesized attribute, count, for each non-terminal (List, single, and multi) to keep track of the number of variables.

List ::= single {List.count = single.count; } single ::= VAR {single.count = 1; } | ( multi ) { single.count = multi.count; } multi ::= single { multi.count = single.count; } | multi1 single { multi.count = multi1.count + single.count; }

  1. Add translation schemes to evaluate the subscript of each variable in a nested list; that is, for each variable x in a given input, the number of other variables contained before x. For example, for (a (b d) (e (f ))), the subscripts for a, b, d, e, f are 0, 1, 2, 3, and 4 respectively. Solution: Keep the synthesized attribute, count, for each non-terminal (List, single, and multi) to keep track of the number of variables. In addition, define an inherited attribute, bef ore, for single, multi and V AR to store the number of variables be- fore the corresponding parse tree. The value of V AR.bef ore is the subscript of the corresponding variable.

List ::= {single.before=0; } single {List.count = single.count; } single ::= {VAR.before=single.before; } VAR {single.count = 1; } | ( {multi.before=single.before; } multi ) { single.count = multi.count; } multi ::= {single.before = multi.before; } single { multi.count = single.count; } | {multi1.before = multi.before; } multi {single.before = multi.before + multi1.count; } single { multi.count = multi1.count + single.count; }