{ NEERC'98 Problem "Spell checker"
  Checking program by Roman Elizarov
  22.11.98
}
{$A+,B-,D+,E+,F-,G+,I+,L+,N+,O-,P-,Q+,R+,S+,T-,V+,X+,Y+}
program SPELL_CHECK;
uses
  testlib;

var
  wa, ans_end, ans_list: boolean;
  wmsg, w_ouf, s_ouf, w_ans, s_ans: string;

begin
  wa:= false;
  while true do begin
    ans_end:= ans.seekeof;
   { check answer if no 'wrong answer' flag }
    ans_list:= false;
    if not wa then begin
      if not ans_end then begin
        w_ans:= ans.readWord ( [], [#0..#255] - ['a'..'z'] );
        ans_list:= ans.curchar = ':';
        if ans_list then
          ans.nextchar;
      end;
    end;

  {--- checking output ---}

    if ouf.curchar in ['a'..'z'] then begin
     { word in the output }
      w_ouf:= ouf.readWord ( [], [#0..#255] - ['a'..'z'] );

      if not wa and ans_end then begin
        wa:= true;
        wmsg:= 'Too many words in the output';
      end;

      if not wa and (w_ouf <> w_ans) then begin {wrong word}
        wa:= true;
        wmsg:= 'Wrong word: "' + w_ouf + '"<>"' + w_ans + '"';
      end;

      if ouf.curchar = ':' then begin { list follows }
        ouf.nextchar;
        if not wa and not ans_list then begin { no list in answer }
          wa:= true;
          wmsg:= w_ouf + ': "is correct" expected instead of list';
        end;

        while not wa and not ouf.seekeoln do begin
          s_ouf:= ouf.readWord ( [], NumberAfter );
          s_ans:= ans.readWord ( [' '], NumberAfter );
          if s_ouf <> s_ans then begin
            wa:= true;
            wmsg:= w_ouf + ': wrong replacement "' + s_ouf + '"<>"' + s_ans + '"';
          end;
        end;

        if not wa and not ans.seekeoln then begin
          wa:= true;
          wmsg:= w_ouf + ': not enough replacements';
        end;

        ouf.nextline;
      end else begin { ' is correct' should follow }
        s_ouf:= ouf.readString;
        if s_ouf <> 'is correct' then
          QUIT ( _PE, w_ouf + ': Wrong format - "is correct" expected' );
        if not wa and ans_list then begin { there's a list in the anser }
          wa:= true;
          wmsg:= w_ouf + ': List expected instead of "is correct"';
        end;
      end;

    end else begin { output finished }
      if not ouf.seekeof then
        QUIT ( _PE, 'Wrong character encountered in output' )
      else
        if not ans_end and not wa then begin
          wa:= true;
          wmsg:= 'Not enough lines in output';
        end;
      break;
    end;

   { if all ok the next line in the 'ans' }
    if not wa then
      ans.nextline;
  end;

  if wa then
    QUIT ( _WA, wmsg );

  QUIT ( _OK, 'Ok' );
end.