Allowing <statement list> in places where TD grammar requires <statement>

This forum is for any questions about the language Tutorial D or the Rel implementation of it.
Post Reply
HughDarwen
Posts: 124
Joined: Sat May 24, 2008 4:49 pm

Allowing <statement list> in places where TD grammar requires <statement>

Post by HughDarwen »

Chris Date is urging me to change the grammar for operator definitions, replacing <statement> (for the operator body) by <statement list>. He doesn't like having always to enclose multiple statements in a BEGIN/END pair.

I'm saying that if we do that for operator definitions we should also do it for DO and WHILE. I notice that Rel supports <statement list> in DO but not in WHILE. Is there a good reason for that?

Hugh
Dave
Site Admin
Posts: 372
Joined: Sun Nov 27, 2005 7:19 pm

Re: Allowing <statement list> in places where TD grammar requires <statement>

Post by Dave »

If it's done for operator definitions, DO, and WHILE, it should probably be done for IF ... ELSE ... END IF and CASE ... WHEN ... END CASE, too, assuming it doesn't cause some grammar ambiguity. I don't think it would, but I haven't tested it.

Regarding usage of <statement list> in Rel DO and WHILE loops, they should currently be the same. Are you seeing a difference?

This is what I get:

Code: Select all

VAR i INIT(0);
DO i := 0 TO 3;
     WRITELN(i);
END DO;

0
1
2
3

Ok.

VAR i INIT(0);
DO i := 0 TO 3;
     WRITELN(i);
     WRITELN(i);
END DO;

ERROR: Encountered "WRITELN" at line 4, column 5.

Was expecting:
"END" ...

VAR i INIT(0);
DO i := 0 TO 3;
     BEGIN;
          WRITELN(i);
          WRITELN(i);
     END;
END DO;

0
0
1
1
2
2
3
3

Ok.

VAR i INIT(0);
WHILE i < 3;
     BEGIN;
          WRITELN(i);
          i := i + 1;
     END;
END WHILE;

0
1
2

Ok.

VAR i INIT(0);
WHILE i < 3;
     WRITELN(i);
     i := i + 1;
END WHILE;

ERROR: Encountered "i" at line 4, column 5.

Was expecting:
"END" ...
HughDarwen
Posts: 124
Joined: Sat May 24, 2008 4:49 pm

Re: Allowing <statement list> in places where TD grammar requires <statement>

Post by HughDarwen »

Sorry, I seem to have misled myself concerning DO.

Regarding THEN and ELSE, I worried about possible ambiguity but on reflection I agree that there probably isn't, thanks to the requirement for END IF and END CASE. Also, a statement beginning with key word ELSE ends a statement list following THEN.

Rexx uses DO instead of BEGIN for compound statements and requires DO ... END around multi-statement lists following THEN and ELSE but does not use END IF etc., nor does it allow for variables that are local to compound statements in which they are defined. Tutorial D does specify name scoping rules we wanted to allow an implementation to support this:

begin;  var i int init(1); output i; begin; var i int init(2); output i; end; output i; end;
ERROR: RS0095: i is already defined in operator Interactive Session()
Line 5, column 16 near '2'

but Rel doesn't, as you can see (I'm not asking for it, just noting).

Hugh
Dave
Site Admin
Posts: 372
Joined: Sun Nov 27, 2005 7:19 pm

Re: Allowing <statement list> in places where TD grammar requires <statement>

Post by Dave »

In early versions of Rel aka Rel2, I supported nested scope with reusable variable names. E.g., the following was allowable:

Code: Select all

VAR i INIT(0);
BEGIN;
	VAR i INIT(2);
END;
Around the time I started working on Rel3, a colleague convinced me that this was undesirable in a pedagogic language, never needed in a production language, and should perhaps be considered questionable style in either. So I removed it from Rel3.

Of course, nested scope can be supported whilst not allowing reusable variable names -- current versions of Java and C# allow nested scope but do not allow re-using variable names from outers scopes in inner scopes -- but I'm not convinced nested scope is needed, either.

I'll experimentally alter the Rel grammar to support <statement list> wherever <statement> currently appears, and see if it introduces any difficulties. A nice thing is that if it works it should not affect any existing code, as use of BEGIN ... END only becomes redundant but not erroneous.
Dave
Site Admin
Posts: 372
Joined: Sun Nov 27, 2005 7:19 pm

Re: Allowing <statement list> in places where TD grammar requires <statement>

Post by Dave »

I've tried replacing all <statement> appearances with <statement list>, and whilst I've not tested it intensively, it works for a reasonable set of test cases. The code remains very readable, too.

I think it's a sufficiently sound change to leave it supported in Rel even if you decide not to adopt it in Tutorial D. It won't be a problem if so, because all Tutorial D code that uses BEGIN ... END will continue to work whether Rel makes it redundant or not.
Dave
Site Admin
Posts: 372
Joined: Sun Nov 27, 2005 7:19 pm

Re: Allowing <statement list> in places where TD grammar requires <statement>

Post by Dave »

HughDarwen wrote:Regarding THEN and ELSE, I worried about possible ambiguity but on reflection I agree that there probably isn't, thanks to the requirement for END IF and END CASE. Also, a statement beginning with key word ELSE ends a statement list following THEN.
This made me curious as to whether or not the "IF" was needed on END IF, and the "CASE" on END CASE, and likewise for DO, WHILE, and OPERATOR definitions. So, I experimentally changed the Rel grammar to make the IF/CASE/DO/WHILE/OPERATOR optional after END, such that you can (for example) either use END or END IF.

I strongly suspected it would work, and it turns out that it does. There are no ambiguities, because nesting is always conserved from inside out. So, for example, the innermost IF will look to match the first END after it, regardless how many ENDs follow it in the stream of tokens.
Post Reply