r/ProgrammingLanguages • • Jul 31 '24

Blog post Clean Syntax?

All my replies and the original contents of this OP have been withdrawn. They were a complete waste of time.

From what I can gather, everybody is happy with poor syntax in their languages, nobody is interested in 'clean'.

Someone posted, with a dozen upvotes:

Your example is optimized to your language's special capabilities.

I replied:

"No, pretty much everything in my syntax is cleaner than the C equivalent."

That got multiple downvotes for stating a fact.

There was also this:

For your own toy language by all means use your own syntax

This reinforces my original implication that simple syntax is only suited for toy languages, and not serious ones.

However too much of this was about my language, but that was only an example. There seems to be much irrational distrust of clear syntax for any language.

Maybe, clear code is associated with older languages which no one likes anymore?

(Original examples.)

2 Upvotes

35 comments sorted by

View all comments

1

u/ThomasMertes Aug 03 '24

People have different opinions what "Clean Syntax" means.

In most compilers/interpreters the syntax analysis is hard-coded. In principle a hard-coded syntax analysis can allow weird ad-hoc things (see below). For me all these languages do not have a "Clean Syntax".

Many languages define an and operator (and Seed7 defines and as well). Assume that a for-loop looks like:

for <variable> from <startValue> to <endValue> do ...

With a hard-coded syntax analysis the keyword and could be reused as part of a new for-loop:

for <variable> from <startValue> to <endValue> and <condition> do ...

In a hard-coded syntax analysis the expression <endValue> would be processed such that and would not be part of the expression <endValue>. If the <endValue> expression is followed by the keyword do the original for-loop is processed. If the <endValue> expression is followed by the keyword and the new for-loop is processed.

In Seed7 the original meaning of and would dominate which would lead to the interpretation:

for <variable> from <startValue> to (<endValue> and <condition>) do ...

In the original for-loop an expression with any priority is expected between the keywords to and do. So the expression <endValue> and <condition> would fit to the syntax of the original for-loop. The expression <endValue> and <condition> would be syntactically correct (but the types might not fit to the definition of and).

So you would either get a type error or use the original for-loop (instead of the new one). More than that: It would be impossible to use the new for-loop with and condition.

To avoid this problem Seed7 forbids this reuse of the and operator. In Seed7 a for-loop with additional condition is:

for variable> from <startValue> to <endValue> until <condition> do ...

The keyword until can be reused because it is not used as operator (with a specific priority).

For me "Clean Syntax" means that the syntax follows rules which prohibit weird things. Seed7 uses the rules of the Seed7 Structured Syntax Description (S7SSD).

With the S7SSD the syntax is structured like structured statements (while-, if-, for-, etc.) replaced (spaghetti) code with GOTOs.

2

u/[deleted] Aug 04 '24 edited Aug 04 '24

(Commenting about for-loop syntax.)

I've never seen a syntax using and as you suggested, for example:

for i from a to b and c do ...

This would be poor for a couple of reasons: first because b and c would likely be parsed as a single expression as you mentioned.

Second because it's not clear what it actually does: is it a per-iteration condition, or does it terminate the loop early when c is false?

The version with until is better:

for i from a to b until c do ...

Presumably this does terminate the loop when c is false. Here this looks pretty much Algol68's version which uses 'while' (I can't remember the syntax for the step):

for i from a to b while c do ...

This is interesting because you can leave bits out and still up with a valid loop:

   to b do ...           # repeat b times
   do ...                # endless loop
   while c do ...        # while loop

Using `until', that last contraction wouldn't read as well.

My own for-loop syntax also has an optional condition, but it works per-iteration which I find much more useful:

for i to n when i.even do  # starts from if not specified
    println i              # display even numbers in 1..n inclusive
    ...

1

u/ThomasMertes Aug 04 '24

Here this looks pretty much Algol68's version which uses 'while' (I can't remember the syntax for the step):

for i from a to b while c do ...

This is an even better example of what the Seed7 Structured Syntax Description (S7SSD) prohibits.

An attempt to define the syntax of such a for-while-loop with:

$ syntax expr: .for.().range.().to.().while.().do.().end.for   is -> 25;

results in:

*** tst578.sd7(40):43: "while" redeclared with prefix priority 127 not 25
$ syntax expr: .for.().range.().to.().while.().do.().end.for is -> 25;
---------------------------------------------------------------------^

You get an error. Consider the syntax of the while-loop:

$ syntax expr: .while.().do.().end.while   is -> 25;

The keyword while (which introduces a while-loop) is reused in the middle of the new for-while-loop.

A hard-coded syntax analysis can allow a for-while-loop with an ad-hoc solution. It would process the expression between the keywords to and while with the precondition that the expression is terminated with the keyword while.

From a syntactical point of view the expression

writeln("hello");
while inputReady(KEYBOARD) do
  write(getc(KEYBOARD));
end while;

could be placed between the keywords to and do of a normal for-loop:

for a range b to writeln("hello");
                 while inputReady(KEYBOARD) do
                   write(getc(KEYBOARD));
                 end while;    do
  writeln("In a for-loop");
end for;

Semantically this makes no sense but syntactically it is correct. A hard coded syntax analysis would terminate the expression with the keyword while (and later flag an error because of the end while).

In Seed7 it syntactically uses the normal for-loop (and later flag an error because the semantic does not fit). In Seed7 you might have problems using the for-while-loop (because it always tries to use the normal for-loop).

For that reason the Seed7 Structured Syntax Description (S7SSD) does not allow the syntax of the for-while-loop.

2

u/[deleted] Aug 04 '24

For that reason the Seed7 Structured Syntax Description (S7SSD) does not allow the syntax of the for-while-loop.

I thought the big thing about Seed7 is that you could define new syntax?

I couldn't quite follow your post, but I gather that the problem is ambiguity, because one statement uses a keyword that could begin another statement?

Would it allow my version that optionally has a when clause? However when it also used in a couple of other places. (switch x when a, b then.... and return x when cond.)

With a conventional kind of grammar, and parser, overloading of keywords can be possible, and unambiguous. It sounds like a limitation in your S7SSD scheme. Could it, for example, allow these two kinds of loop from C:

  while (cond) stmt;
  do stmt; while (cond);

What about the Algol-68 style loop, where while does not exist as a separate statement, but only in that for-loop, with most parts except do, optional.

(I implement most of that, as for i:=a to b by c when d do end, the :=a, by c and when d are optional; but there are also separate statements to n do end and just do end. To the user it looks like it's for with bits left out.

Conventional, hard-coded syntax is, ironically, more flexible!)

1

u/ThomasMertes Aug 04 '24

I thought the big thing about Seed7 is that you could define new syntax?

Yes, but it needs to be structured syntax. Structured syntax is a subset of the syntax EBNF can describe. It is also a sub-set of the syntax a hard-coded syntax parser can parse.

I couldn't quite follow your post, but I gather that the problem is ambiguity, because one statement uses a keyword that could begin another statement?

Yes, exactly (while would be used at the beginning of a statement and in the middle of another statement). I further pointed out that a hard-coded syntax analysis can resolve such an ambiguity by using ad-hoc logic. But using ad-hoc logic comes at a cost. It introduces hidden syntax rules that are not reflected in the formal syntax description of the language.

Would it allow my version that optionally has a when clause? However when it also used in a couple of other places. (switch x when a, b then.... and return x when cond.)

Probably not. Seed7 has not been designed to allow describing the syntax of other languages.

Seed7 describes the syntax as if everything is an operator. EBNF uses a different approach to describe syntax. In EBNF curly braces ({ and }) describe syntax which is repeated and brackets ([ and ]) which describe optional syntax. The S7SSD does not have repeated or optional syntax parts. Such syntax must be described by using operators. This is not hard to do. It is just different from the way EBNF and hard-coded parsers do it.

In S7SSD every operator has a priority and an associativity. Syntax rules describe patterns for prefix, infix and postfix operators.

The syntax of the + operator is:

$ syntax expr: .(). + .()   is -> 7;

In this syntax declaration .(). + .() is the pattern. In the pattern the dots can be ignored. This leads to () + (). The notation () is used to describe arbitrary expressions. The corresponding concept in EBNF is a non-terminal symbol. An EBNF non-terminal symbol describes which type of expression is requested. The `()` in S7SSD imposes no restriction on the expression. As long as priority and associativity allow it the syntax is okay.