0

Is there a way, whilst parsing source of a statically-scoped nested language such as Pascal (which allows nested subroutines), of maintaining binding information (list of name-value pairs as they are encountered in the source) ? The difficulty is that if there is an outer-level routine containing two nested sub-routines, the second-parsed sub-routine should 'inherit' the bindings established globally or within the outer-level routine, but not those of the first-parsed routine. Thus, it seems to me, you can't handle the problem with a monadic parser but that might be because I don't know how to use them well enough.

procedure outer;
    var somevar : integer;
    procedure inner1;
        var in1 : char;
    begin
        something;
    end;
    procedure inner2;
        var in2 : Boolean;
    begin
        something_else;  (* in1 is not visible here of course *)
    end;
begin
    inner1;
    inner2;
end;

Any ideas ? I hope I explained it sufficiently clearly.

  • 1
    Add a state to your parsers and store the bindings in that state, as well as the current scope level. When the scope level changes, go through the list of bindings and flag the ones that are out of scope as such, or delete them completely if you know they can never come back into scope (such as when you are done parsing a function - you can delete bindings created within that function). – user2407038 Jan 03 '14 at 23:01
  • 1
    @user2407038 - even better use the Reader monad (also called the Environment monad) and its `local` method for scoping. This avoids having to manually delete bindings when exiting a nested level. – stephen tetley Jan 04 '14 at 10:57

0 Answers0