program wp09zad1;

procedure zbadajPoprawnosc(prg : String);
var otwarte, litery, i : Integer;

    procedure sprawdz(w : Boolean);
    begin
        if not w then begin
            writeln('Blad skladniowy na pozycji ', i);
            halt
        end
    end;

begin
    litery := 0;
    otwarte := 0;
    for i := 1 to length(prg) do
        if prg[i] = '{' then begin
            sprawdz(litery mod 2 = 1);
            litery := 0;
            otwarte := otwarte + 1
        end else if prg[i] = '}' then begin
            sprawdz((otwarte > 0) and (litery mod 2 = 0));
            litery := 0;
            otwarte := otwarte - 1
        end else begin
            sprawdz(('a' <= prg[i]) and (prg[i] <= 'z'));
            litery := litery + 1
        end;
    i := length(prg) + 1;
    sprawdz((otwarte = 0) and (litery mod 2 = 0))
end;

procedure wykonajProgram(prg : String);
var pc : Integer;
    mem : array ['a' .. 'x'] of Integer;
    c : Char;

    function para(i, d : Integer) : Integer;
    var ile : Integer;
    begin
        ile := 1;
        while ile <> 0 do begin
            i := i + d;
            if prg[i] = '{' then
                ile := ile + d
            else if prg[i] = '}' then
                ile := ile - d
        end;
        para := i
    end;

    function wartosc(zmienna : Char) : integer;
    var c : Char;
    begin
        if zmienna = 'y' then
            read(wartosc)
        else if zmienna = 'z' then
            if eof then
                wartosc := -1
            else begin
                read(c);
                wartosc := ord(c)
            end
        else
            wartosc := mem[zmienna]
    end;

    procedure odejmij(zmienna : Char; co : Integer);
    begin
        if zmienna = 'y' then
            writeln(co)
        else if zmienna = 'z' then
            write(chr(co))
        else
            mem[zmienna] := mem[zmienna] - co
    end;

begin
    for c := 'a' to 'w' do
        mem[c] := 0;
    mem['x'] := 1;
    pc := 1;
    while pc <= length(prg) do
        if prg[pc] = '}' then
            pc := para(pc, -1) - 1
        else if prg[pc + 1] = '{' then
            if wartosc(prg[pc]) > 0 then
                pc := pc + 2
            else
                pc := para(pc + 1, 1) + 1
        else begin
            odejmij(prg[pc], wartosc(prg[pc + 1]));
            pc := pc + 2
        end
end;

begin
    if paramCount <> 1 then
        writeln('Interpreter jezyka Malina oczekuje jednego argumentu')
    else begin
        zbadajPoprawnosc(paramStr(1));
        wykonajProgram(paramStr(1))
    end
end.
