r/cpp • • Sep 17 '22

Cppfront: Herb Sutter's personal experimental C++ Syntax 2 -> Syntax 1 compiler

https://github.com/hsutter/cppfront
335 Upvotes

360 comments sorted by

View all comments

Show parent comments

21

u/ooglesworth Sep 18 '22

The main thing is that it’s a context free syntax. With int main() { the compiler has to read all the way to the open paren before deciding whether this is a declaration of a function or declaration of an int. The return value of the function is the first thing lexed, but it isn’t clear that it’s a subexpression of a function declaration until much later. Conversely, with main: () -> int = { it’s much more straightforward to parse. At the colon the compiler knows it is inside of a variable declaration and is expecting a type next, by the open parens it knows it is parsing a function type, etc.

You might argue “this is just making it easier for a computer to parse and more difficult for a human to parse!” Well, for one thing, making it easier for a computer to parse avoids weird edge cases like the “most vexing parse” (you can Google that if you’re not familiar) which in turn contributes to readability by humans. “Making usage look like the declaration” is exactly the problem in a lot of parsing situations.

I think you might be surprised by how much overlap between there is between parseability and readability. Ambiguities aren’t good for parsers and they aren’t good for humans either. It might look foreign to you, but I don’t think there is anything fundamentally less readable about it, you’re just not used to it. I’d be willing to bet it wouldn’t practically present any real readability barrier after working in the language for even a brief amount of time, and it might even be easier to read once becoming acclimated to it.

Also, brevity isn’t really important IMO, it doesn’t matter that one takes four more characters than another. Just my 2c.

-1

u/arthurno1 Sep 18 '22 edited Sep 18 '22

the compiler has to read all the way to the open paren before deciding whether this is a declaration of a function or declaration of an int.

Yes, but is that a problem for a compiler? Someone above-mentioned IDE completion. I can buy the argument it is easier to implement the feature, but I don't see it compelling enough to bake compiler implementation features into the language. As I said, the old syntax is going nowhere, unless C++ wants to become a completely new language incompatible with the old code, which isn't happening either. Thus, we are just adding more syntax and more concepts to implement and for people to learn.

“Making usage look like the declaration” is exactly the problem in a lot of parsing situations.

Another problem I see is with the approach that we are suggesting, is that feature targeting compiler implementation creeps into the language design; or at least its syntax. A compiler is implemented once, and considering all other heavy stuff it does for us nowadays, optimization passes, design patterns creeping in, and what not, it feels wrong to have to type extra syntax every time we write new code just because it is "hard" to implement the compiler. Of course, it is hard; but we already have a working syntax, adding new one does not make things better.

it wouldn’t practically present any real readability barrier after working in the language for even a brief amount of time, and it might even be easier to read once becoming acclimated to it.

I can read music scores quite fluently too; playing both guitar and piano, so I am sure I can learn another syntax to type a function definition. But "getting used to" is not the point. Of course, we can get used to it. People get used to Haskell notation, and Lisp and what not. My point is that keeping things simple has a value in itself. Fewer concepts to learn and understand, introduce also fewer possibilities to make mistakes and introduce bugs that need to be dealt with later on.

brevity isn’t really important IMO

While I agree that brevity itself is not a goal, and can be contra productive, when lots of stuff is condensed into few characters, Perl and Bash have quite brief syntax with all the operators which I don't think is very readable either. However, I think the clarity and simplicity is. Those two often comes together with brevity, but not necessarily.

1

u/dustyhome Sep 20 '22

Some people seem to not grasp that if a syntax is more difficult for the compiler to parse, it is also more difficult for a human to parse. You're doing just as much work as the compiler to read it. You've just gotten used to it. The new syntax is new so you haven't learned it yet, so it "feels" like more effort.

The new syntax is pretty symple, you introduce first a name, then describe what the name is. So when you see the name used somewhere, you can just search for "name :" and find out what it is.

I wonder how new types will be introduced with the new syntax, though.

I am also a bit disappointed that he had to compromise on syntax by allowing mixed old and new syntax in a single file, meaning the syntax has to avoid looking like anything existing cpp could look like. That might come back to bite him in the future. Should have only allowed new syntax in the file, to have a true clean slate. That's how it is most likely to be used.

Alternatively, using the mixed switch and something like "extern cpp2{}" could have been used to make it backwards compatible. You clearly delineate the space that follows the new rules and then you don't need to worry about the old syntax at all.

2

u/arthurno1 Sep 20 '22

Some people seem to not grasp that if a syntax is more difficult for the compiler to parse, it is also more difficult for a human to parse.

Do you have any scientific observation, study, proof, paper to support this claim?

→ More replies (4)