5.6. Repeat - Until

Here's one construct that I lifted right from Pascal. The syntax is

REPEAT <block> UNTIL <condition>

and the syntax-directed translation is:

REPEAT          { L = NewLabel;
                  PostLabel(L) }
<block>
UNTIL
<condition>     { Emit(BEQ L) }

As usual, the code falls out pretty easily:

{ Parse and Translate a REPEAT Statement }
procedure DoRepeat;
var L: string;
begin
   Match('r');
   L := NewLabel;
   PostLabel(L);
   Block;
   Match('u');
   Condition;
   EmitLn('BEQ ' + L);
end;

As before, we have to add the call to DoRepeat within Block. This time, there's a difference, though. I decided to use r for REPEAT (naturally), but I also decided to use u for UNTIL. This means that the u must be added to the set of characters in the while-test. These are the characters that signal an exit from the current block … the “follow” characters, in compiler jargon.

{ Recognize and Translate a Statement Block }
procedure Block;
begin
   while not(Look in ['e', 'l', 'u']) do begin
      case Look of
       'i': DoIf;
       'w': DoWhile;
       'p': DoLoop;
       'r': DoRepeat;
       else Other;
      end;
   end;
end;