{
    ACM ICPC 2003-2004 NEERC

    Problem:    Code Formatting
    Author:     Roman Elizarov
    Solution:   Maxim Babenko
}

{$APPTYPE CONSOLE}

const
    inFile = 'code.in';
    outFile = 'code.out';

    whitespaces = [' ', #10, #13, #9];
    alpha = ['0'..'9', 'a'..'z', 'A'..'Z'];

type
    TTokenType = (
        _null,
        _ident,
        _comma,
        _semicolon,
        _lparen,
        _rparen,
        _lcurl,
        _rcurl,
        _eof );

    TToken = record
        what : TTokenType;
        text : string;
    end;

var
    lookaheadChar : char = #0;
    lookaheadToken : TToken = ( what: _null; text: '');

function ReadChar : char;
begin
    if lookaheadChar <> #0 then begin
        ReadChar := lookaheadChar;
        lookaheadChar := #0;
        exit;
    end;

    if EOF then begin
        ReadChar := #26;
        exit;
    end;

    read(result);
end;

procedure UnreadChar(ch : char);
begin
    if lookaheadChar <> #0 then
        RunError(239);
    lookaheadChar := ch;
end;

function ReadToken : TToken;
var
    ch : char;

begin
    if lookaheadToken.what <> _null then begin
        ReadToken := lookaheadToken;
        lookaheadToken.what := _null;
        exit;
    end;
    
    repeat
        ch := ReadChar();
        if ch = #26 then begin
            result.what := _eof;
            exit;
        end;
    until not (ch in whitespaces);

    if ch in [',', ';', '(', ')', '{', '}'] then begin
        result.text := ch;
        case ch of
            ',' : result.what := _comma;
            ';' : result.what := _semicolon;
            '(' : result.what := _lparen;
            ')' : result.what := _rparen;
            '{' : result.what := _lcurl;
            '}' : result.what := _rcurl;
        end;
    end else if ch in alpha then begin
        result.what := _ident;
        result.text := ch;
        repeat
            ch := ReadChar();
            if ch = #26 then
                break;
            if not (ch in alpha) then begin
                UnreadChar(ch);
                break;
            end;
            result.text := result.text + ch;
        until false;
    end else
        RunError(239);
end;

procedure UnreadToken(t : TToken);
begin
    if lookaheadToken.what <> _null then
        RunError(239);
    lookaheadToken := t;
end;

procedure Solve;
var
    t : TToken;
    indent : integer;

    procedure WriteIndent;
    begin
        write(StringOfChar(' ', 4 * indent));
    end;

    function ParseBlock(start : boolean) : boolean; forward;
    function ParseExpression(start : boolean) : boolean; forward;

    procedure ParseArguments;
    var
         t : TToken;
         
    begin
        if not ParseExpression(false) then
            RunError(239);
            
        repeat
            t := ReadToken;
            if t.what <> _comma then begin
                UnreadToken(t);
                break;
            end;
            write(', ');
            if not ParseExpression(false) then
                RunError(239);
        until false;
    end;

    function ParseExpression(start : boolean) : boolean;
    var
        t : TToken;

    begin
        t := ReadToken;
        if t.what <> _ident then begin
            UnreadToken(t);
            ParseExpression := false;
            exit;
        end;

        if start then
            WriteIndent;
        write(t.text);
        
        t := ReadToken;
        if t.what = _lparen then begin
            write('(');
            ParseArguments;
            if ReadToken.what <> _rparen then
                RunError(239);
            write(')');
        end else
            UnreadToken(t);

        ParseBlock(false);

        ParseExpression := true;
    end;

    function ParseStatement : boolean;
    begin
        if not ParseExpression(true) then begin
            ParseStatement := false;
            exit;
        end;
        if ReadToken.what <> _semicolon then
            RunError(239);
        writeln(';');
        ParseStatement := true;
    end;

    function ParseBlock(start : boolean) : boolean;
    var
        t : TToken;
        
    begin
        t := ReadToken;
        if t.what <> _lcurl then begin
            UnreadToken(t);
            ParseBlock := false;
            exit;
        end;

        if start then begin
            WriteIndent;
            writeln('{');
        end else
            writeln(' {');
            
        inc(indent);
        while ParseStatement do ;
        if ReadToken.what <> _rcurl then
            RunError(239);
        dec(indent);

        WriteIndent;
        write('}');

        ParseBlock := true;
    end;

    procedure ParseProgram;
    begin
        if not ParseBlock(true) then
            RunError(239);
        if ReadToken.what <> _eof then
            RunError(239);
    end;

begin
    indent := 0;
    ParseProgram;
    writeln;
end;

begin
    Solve;
end.
