program Continuations(input, output);
{ proof of concept parser for a non deterministic grammar:
     <sen>    ::= <aORaa> <aORaa>
     <aORaa>  ::= a | aa

  See: L. Allison. Some Applications of Continuations.
       The Computer Journal, Volume 31, Issue 1, pp. 9-16, 1988.
}

var l:array[1..80]of char;  {the input string}
    n:integer;

procedure fin(m:integer); begin if m=n+1 then writeln('solution') end;

procedure aORaa( n:integer; procedure cont(p:integer) );
begin if l[n]='a' then begin cont(n+1);
                             if l[n+1]='a' then cont(n+2)
                       end
end;

procedure sentence( n:integer; procedure Cont(p:integer) );
   procedure aORaaCont( n:integer ); begin aORaa(n, Cont) end;
begin aORaa( n, aORaaCont )
end;

begin
   writeln('type a string for parsing');
   while not eof do
   begin n:=0;
         while not eoln do begin n:=n+1; read(l[n]) end;
         readln; writeln;
         sentence( 1, fin ); writeln('finished;  next one please')
   end
end.

{ Released under GNU General Public Licence (GPL). }
{ NB. Requires a full ISO standard Pascal Implementation. }
