{
    ACM ICPC 2003-2004 NEERC

    Problem:    Code Formatting
    Author:     Roman Elizarov
    Solution:   Andrew Stankevich
}

{$APPTYPE CONSOLE}
{$O-,Q+,R+}

program code_formatting;

uses
    sysutils;

type
    ttoken = (_id, _lparen, _rparen, _lbrace, _rbrace, _comma, _semicolon, _end);
    tstate = (_start, _lb, _lc);

const
    idchars = ['A'..'Z', 'a'..'z', '0'..'9'];
    whitespace = [' ', #9, #10, #13];
    other = [';', ',', '(', ')', '{', '}'];

    allowed = idchars + whitespace + other;

var
    ch: char;
    token: ttoken;
    state: tstate;
    ident: longint;
    count: longint;

procedure process(ch: char);
var
    i: longint;
begin
    if (ch in whitespace) then exit;
    if ch = '{' then
        inc(ident);
    case state of
        _start: begin
            writeln('{');
            state := _lb;
        end;
        _lb: begin
            if ch = '}' then
            begin
                dec(ident);
            end;
            for i := 1 to 4 * ident do
                write(' ');
            if ch = '{' then
                writeln(' {')
            else if ch = ',' then
                write(', ')
            else if ch = ';' then
                writeln(';')
            else
                write(ch);
            state := _lc;
        end;
        _lc: begin
            if ch = '{' then
                writeln(' {')
            else if ch = ',' then
                write(', ')
            else if ch = ';' then
                writeln(';')
            else
                write(ch);
            if (ch = '{') or (ch = ';') then
                state := _lb;
        end;
    end;
end;

procedure nextch;
begin
    if eof then
    begin
        ch := '!'
    end else begin
        read(ch);
        inc(count);
        if count > 2000 then runerror(239);
        if not (ch in allowed) then runerror(239);
        process(ch);
    end;
end;

procedure nexttoken;
begin
    while (ch in [' ', #9, #13, #10]) do nextch;
    case ch of
        'A'..'Z', 'a'..'z', '0'..'9': begin
            token := _id;
            while ch in idchars do nextch;
        end;
        '(': begin
            token := _lparen;
            nextch;
        end;
        ')': begin
            token := _rparen;
            nextch;
        end;
        '{': begin
            token := _lbrace;
            nextch;
        end;
        '}': begin
            token := _rbrace;
            nextch;
        end;
        ',': begin
            token := _comma;
            nextch;
        end;
        ';': begin
            token := _semicolon;
            nextch;
        end;
        '!': begin
            token := _end;
            nextch;
        end;
    end;
end;

procedure expression; forward;
procedure block; forward;

procedure arguments;
begin
    expression;
    while token = _comma do
    begin
        nexttoken;
        expression;
    end;
end;

procedure expression;
begin
    if token <> _id then runerror(239);
    nexttoken;
    if token = _lparen then
    begin
        nexttoken;
        arguments;
        if token <> _rparen then runerror(239);
        nexttoken;
    end;
    if token = _lbrace then
        block;
end;

procedure statement;
begin
    expression;
    if token <> _semicolon then runerror(239);
    nexttoken;
end;

procedure statements;
begin
    statement;
    while token = _id do
    begin
        statement;
    end;
end;

procedure block;
begin
    if token <> _lbrace then runerror(239);
    nexttoken;
    statements;
    if token <> _rbrace then runerror(239);
    nexttoken;
end;

procedure prog;
begin
    block;
    if token <> _end then
        runerror(239);
end;

begin
    count := 0;
    state := _start;
    ident := 0;
    nextch;
    nexttoken;
    prog;
end.