D (419 chars)
enum c=q{string[string] m;import std.stdio;import std.array;void main(){foreach(string s;lines(stdin)){auto a=s.split;if(!a.length)goto e;switch(a[0]){case "*":m.clear;break;case "!":m.remove(a[1]);break;case "?":writeln(m.get(a[1],""));break;default:if(a.length<2){goto e;}m[a[0]]=a[1];}stdout.flush;}e:write("static this(){");foreach(i,v;m)writef("m[`%s`]=`%s`;",i,v);write("}enum c=q{",c,"};mixin(c);");}};mixin(c);
formatted:
enum c=q{
string[string] m;
import std.stdio;
import std.array;
void main(){
foreach(string s;lines(stdin)){
auto a=s.split;
if(!a.length)goto e;
switch(a[0]){
case "*":m.clear;break;
case "!":m.remove(a[1]);break;
case "?":writeln(m.get(a[1],""));break;
default:if(a.length<2){goto e;}m[a[0]]=a[1];
}
stdout.flush;
}
e:write("static this(){");
foreach(i,v;m)writef("m[`%s`]=`%s`;",i,v);
write("}enum c=q{",c,"};mixin(c);");
}
};mixin(c);
the *
command relies on m.clear;
to work correctly which it doesn't in dmd 2.52 (bug in the compiler)
the need for stdout.flush;
depends whether auto flush is enabled (it isn't on my machine)