0% found this document useful (0 votes)
11 views

week13

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

week13

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Programming Language Concepts and Implementation Fall 2011

Week 13

Lecture
This weeks lecture is about memory management and garbage collection in particular. In the example
compilers we have seen until now (the expression language and Micro-C), all memory needed for a
program has been allocated on the stack. There are two reasons why this has worked: (1) for the
languages considered, whenever a function allocates data in memory the data is never needed after
the function invocation ends, and (2) the size of all variables used can be determined at compile time.
In real programming languages like C#, a function (or method) can allocate for example an array and
return a pointer to it. In this case the array may be needed long after the method invocation allocating
it has ended, and so it cannot be allocated on the stack. So such programming languages need a second
area of storage, which is usually called the heap.
While all high-level programming languages provide mechanism for allocating heap space, some
programming languages like C or Pascal leave the responsibility of freeing space when it is no longer
needed to the programmer. This can lead to very efficient programs but, much more commonly it
leads to subtle bugs. Most modern programming languages like Java or C#, or indeed any functional
language manage the heap automatically behind the scenes. In this weeks lecture we shall look at
different techniques for doing this, i.e., allocating space upon request from the program, and freeing
space whenever needed. The latter process is called garbage collection.

This weeks readings


Read Sestoft: PLCSD 10-10.6. An alternative source is Mogensen chapter 12. The research paper Real-
time garbage collection by David Bacon discusses the challenges of using automated memory management
in real-time systems (i.e., systems where response time is crucial) but also describes a recent solution to
these problems.

Exercises
This weeks exercises are examples of possible exam problems. The problems on the actual exam may
still be slightly different, but these problems should give you an idea of what I expect you to be able to
do. As for size, I imagine that the problems below represent a little more than just one exam, but not
much. Also, these problems have not been examined with the same scrutiny as will a real exam set. If
you think some exercises can be misunderstood, please let me know.
When solving the exercises, make sure that you do all the actions marked with bold font.

Delegates and generics


These exercises are about declaring generic methods and using delegates. You are not allowed to use
C#-library functions such as sorting algorithms or the like.

Exercise 1.1 Declare a static C# method FindLeast that takes an array of integers and returns the least
among them. If the array is empty the method should throw an exception. The method should leave
the input array unchanged.

Exercise 1.2 The generic delegate type Func<T,S,bool> is declared as follows


public delegate bool Func<in T, in S, bool>(T x, S y)

Write a static generic method also called FindLeast that takes an array of elements of some type T and
an delegate lt of type Func<T,T,bool>. Assuming that objects of type T are ordered in some way and
that lt(x,y) returns true if x is (strictly) less than y and false otherwise, FindLeast(xs, lt) should
return the smallest element of xs. Write C# code for calling this method to find the smallest element in
an array of integers.

1
Programming Language Concepts and Implementation Fall 2011

Exercise 1.3 The generic interface IComparable<T> contains a method int CompareTo(T that). Write
a static generic method called FindLeastGen that takes an array of elements of some type T such that
T implements IComparable<T> returning the least element in the array. You may assume that int
CompareTo(T that) always returns a negative number if this is smaller than that, and returns 0 or a
positive number otherwise.

Values and references


Consider the following two C# methods
public static void Main() {
int x = Mult(2,3);
...
}

public static int Mult(int x, int y) {


return x*y;
}

Exercise 2.1 In Micro-C all methods return void values, but in the course we have seen how to return
values by passing references as arguments to functions. Write a Micro-C function mult that computes
the product of two integers and returns the result using the technique described in the course, and write
a Micro-C method main that calls mult. In other words represent the above two methods as faithfully
as possible as a Micro-C program.

Exercise 2.2 Also C# programs can take parameters by reference. Rewrite the two C# methods above
so that Mult becomes a method with return type void and the result of Mult is returned to Main using a
by-reference parameter. Draw the call-stack as it looks just before the call to Mult returns.

Exercise 2.3 Consider the following C# program.


public struct CD {
public String title, artist;
public CD(String artist, String title) {
this.title = title; this.artist = artist;
}
public void MakeAbba() {artist = "Abba";}
public override String ToString() {return artist + ": " + title;}
}

public class AbbaRules {


public static void Main() {
CD[] cds = new CD[2];
cds[0] = new CD("ACDC", "Back in black");
cds[1] = new CD("50 Cents", "Get rich or die tryin");
cds[0].MakeAbba(); // program point 1
MakeAbba(cds[1]); // program point 2
Console.WriteLine(cds[0]);
Console.WriteLine(cds[1]);
}
public static void MakeAbba(CD cd) { cd.artist = "Abba";}
}

What is written on screen? Draw the memory (the heap and the stack) as it looks just before each of
the calls in program points 1 and 2 return.

Regular expressions and automata


Exercise 3.1 Construct a deterministic finite automaton recognising a string in the alphabet {a, b, c, d}
if both of the following conditions hold

2
Programming Language Concepts and Implementation Fall 2011

The course uses the following material


<ul>
<li> Books
<ul>
<li>Torben Mogensen: <a href="http://www.diku.dk/hjemmesider/ansatte/torbenm/Basics/">
Basics of Compiler Design</a></li>
<li>Peter Sestoft: <a href="http://www.itu.dk/courses/BPRD/E2011/plcsd-0_51.pdf">PLCSD</a>,
version 0.51, IT University </li>
<li>Peter Sestoft, Henrik I. Hansen: C# Precisely, second edition.</li>
</ul>
</li>
<li> No papers used </li>
</ul>

Figure 1: An example HTMLLists program

• the length of the string is odd

• the string consists only of the characters a, b and c


For example, the automaton should recognise the strings b, bbb, bcb and acb, but not bb, dcd and ab

Exercise 3.2 Construct a regular expression matching the strings satisfying the conditions of Exer-
cise 3.1.

Eliminating ambiguity in context free grammars


Exercise 4.1 Consider the following context free grammar

E =E&E
| ∗E
|a
|b

Show that the grammar is ambiguous by constructing two different parsing trees for the same expres-
sion. Construct an unambiguous grammar for the same language as the above using the following
rules: & binds tighter than * and & is left associative

Exercise 4.2 Suppose now the grammar of Exercise 4.1 is extended with the production E = E # E.
Construct an unambiguous grammar for the new extended language encoding the same associativity
and precedence rules as above and such that # binds less tightly than the two other operators * and &
and such that # is right associative.

Parsing
Hypertext Markup Language (HTML) is used for describing webpages. In this exercise we consider
a fragment of HTML which we shall call HTMLLists used for describing bullet point lists and links.
Figure 1 contains a program in this language which should be displayed in a browser as shown in
Figure 2. In Figure 2 the underlined text is a link to a web page.
A HTMLLists program consists of some text which may contain links and lists. The beginning of
a list is marked by the tag <ul> and the end is marked by the tag </ul>. Between these tags should
come a list of one or more list items. The beginning of a list item is marked by the tag <li> and ended
by </li>. Between <li> and </li> any string that describes a HTMLLists program is allowed. The
beginning of a link is marked by a tag of the form <a href="address"> where address can be any legal
URL (internet address), and the tag is ended by </a>. Between the start and end tag of a link, only plain
text is allowed.

3
Programming Language Concepts and Implementation Fall 2011

The course uses the following material


• Books
– Torben Mogensen: Basics of Compiler Design
– Peter Sestoft: PLCSD, version 0.51, IT University
– Peter Sestoft, Henrik I. Hansen: C# Precisely, second edition.

• No papers used

Figure 2: Displaying the example HTMLLists program

Exercise 5.1 Write a context free grammar describing the syntax of HTMLLists. The grammar should
be written in BNF or extended BNF as used on the course, and should be free of ambiguity. You may
use the tokens text and url denoting text strings not containing the special HTML symbols <,>,/,"
and URLs in double quotes respectively. Draw the syntax tree for the example in Figure 2.
Exercise 5.2 Sketch a design of an abstract syntax for HTMLLists in C#. Your sketch should answer
the following questions:
• Which classes do you need?
• What should the inheritance hierarchy be among these classes (i.e., which classes should be
subclasses of other classes)
• Which fields should each class have?
Exercise 5.3 Write the productions of a Coco/R parser specification for HTMLLists. Your solution
should be based on your solution to Exercise 5.1 and should extend it with semantics actions, so that the
parser generated by Coco/R generates abstract syntax of the form given by your solution to Exercise 5.2.
Note that you are not asked to produce the entire Coco/R input file, but only the productions, i.e., the
part occuring after the keyword PRODUCTIONS in the .ATG-files used in the course.
You may assume that the two productions
Text<out String s>
URL<out String s>

have been defined for you, so that you can use them. The first of these parses a token text (as used
in Exercise 5.1) and returns the corresponding string and the second parses a URL in double quotes and
returns the URL as a string.

Translating to stack machines


Exercise 6.1 Translate the following arithmetic expression to reverse polish notation
2 ∗ ((3 + 4) ∗ (5 − 2)) + 7
Exercise 6.2 Consider the following two function definitions written in the expression language used
in the course.
int main(int x) = f(x) + f(x+1);
int f(int x ) = if x==0 then 1 else x;

Translate these function definitions to stack machine code for the machine used in the course (as
described in PLCSD Figure 8.1). The stack machine code corresponding to each of these functions
should - assuming the input argument is on top of the stack when execution begins - replace the top of
the stack by the result of the function call, and otherwise leave the stack unchanged. The code of each
function should end with a return statement.
You should use symbolic machine code with symbolic labels, i.e., using instructions such as Label
L1 and GOTO L1.

You might also like