r/ProgrammingLanguages • u/tertsdiepraam • Mar 18 '26
Blog post No Semicolons Needed - How languages get away with not requiring semicolons
https://terts.dev/blog/no-semicolons-needed/Hi! I've written a post about how various languages implement statement termination without requiring semicolons because I couldn't find a good overview. It turned out to be much more complex than I initially thought and differs a lot per language.
I hope this overview will be helpful to other language designers too! Let me know what you think!
123
Upvotes
29
u/KittenPowerLord Mar 18 '26
Haskell kinda does the thing you're describing in "A different idea" (afaik it's more complicated there, but I'm not knowledgeable enough to elaborate). But wow yes, I've been considering this approach for a while and it's very appealing
15
u/SecretlyAPug Mar 18 '26
adjacent, but why do "readable" languages often not include semicolons? there's maybe a small argument to be made that no semicolons can be a little easier to read for very beginners, but i find this ambiguity much more confusing.
5
u/cmontella 🤖 mech-lang Mar 18 '26 edited Mar 18 '26
The way Mech does it is it doesn't strip out whitespace in the lexer, and handles it in the grammar explicitly, so it can handle newlines or semicolons: https://docs.mech-lang.org/design/specification.html#1092025537734171
valid:
Also valid:
Also valid:
7
u/defmacro-jam Mar 18 '26
Porque no Lisp?
5
u/Inconstant_Moo 🧿 Pipefish Mar 18 '26
How I do it: Whitespace is significant a la Python. A newline is treated as a semicolon, separating expressions, unless the line ends with
,or the continuation symbol.., and either way the next line must begin with... Lines starting with..can be aligned how you like for readability.Why? Because explicit is better than implicit. I want to know as soon as I look at a line that it's a continuation of the previous one. This is very simple and non-magical.
16
u/Silphendio Mar 18 '26
I like the Gleam way: No significant whitespace and no operators that can be both infix and prefix.
The only problem is the minus sign. In my (draft-stage) language, I thought about using -- for subtractions to get rid of this last ambiguity, but in the end I decided to just parse this cursed operator as infix whenever possible.
Since commas are optional too, a list of negative numbers can thus be written as [-1, -2, -3] or [{-1} {-2} {-3}], but [-1 -2 -3] is equivalent to [-6].
8
u/Redtitwhore Mar 19 '26
What's the problem with semicolons?
7
u/munificent Mar 19 '26
Excellent post!
I like the simplicity of Python's approach. But the big downside is that it makes it much harder for the language support blocks and statements nested inside expressions. Most languages today support some kind of lambda or anonymous function syntax that contain as much code as it wants. For example, in JavaScript:
Python only supports single-expression lambdas. Part of the reason is that block-bodied lambdas really clash with the language's grammar and the implicit semicolons are part of that.
Python ignores all newlines between pairs of delimiters. That does exactly what you want when you have a big multi-line expression as a function argument or in a collection literal. But if Python wanted to allow statement-bodied lambdas, then they'd need some way to turn newlines back on inside those lambdas even when they are nested inside delimiters.
I also like the simplicity of Go's approach but I think it has one style wart because of that. If you want to have a multi-line method chain, in almost every language, you'd do:
In Go, that doesn't work because the newlines are all treated as significant. Instead, you have to put the
.on the ends of the lines:That looks pretty bad to me. They could handle this while still handling newlines in the lexer. They would just need to lookahead past the newline and see if the first token after the newline is
.. If so, ignore the newline. I don't think.can ever start a statement or expression in Go, so that should work.5
u/Tasty_Replacement_29 Bau Mar 18 '26
An aspect not yet discussed is command line REPL (read–eval–print loop): if the user presses enter, can the engine tell the line is complete? This feature requires either some "continue" character like
\at the end of the line, or the operator, or(to mark "continue". For this reason, in my language I use "end of line operator" or parenthesis:4
u/mot_hmry Mar 18 '26
Someone else mentioned Haskell, but F# also kinda does what you mentioned in another idea.
4
u/TOMZ_EXTRA Mar 19 '26
You should have probably mentioned that Lua doesn't allow expression statements (except function calls).
12
u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Mar 18 '26
InterestingIreallydontwhypeoplearesointerestedinavoidingpunctuationitdoesmakethingsmorereadablebutthatsjustme
3
u/Dry-Light5851 Mar 19 '26
irony is that Basic solved this problem decades ago, use "\n" or in plain text a new line to delimit witespace, and have everything be an expression.
3
u/SwedishFindecanor Mar 19 '26
Javascript seems to me like one of those many "standards" that hadn't been specified unambiguously when it was introduced and therefore got interpreted differently in different implementations, so that future standards and implementations had to more complex to be able to account for all pre-existing varieties.
I've seen that phenomenon many times, also in file formats and protocols.
3
u/kjd3 Mar 19 '26
A very long time ago BCPL got this mostly right, in my opinion. It treated semi-colon as an optional separator. Newlines were similar but not identical as they could occur, and be ignored, anywhere an expression/statement could not be terminated. This is easy to do in practical lexing/parsing and seems sensible to me. Thus: let a, b = 42, ? a := a + 3; b := a / 9
leaves a = 45 and b = 5.
Oddly, the successor of BCPL (via B): C; did not inherit its relaxed approach to semi-colons. As we all know it treated them as terminators. So here we are....
I think BCPL is interesting as both a very early example of relaxed semi-colon use and, historically, as an ancestor of C which differed so much in that respect.
3
u/pjmlp Mar 19 '26
Missed older languages like Fortran, COBOL, BASIC, Smalltalk, xBase/Clipper, among others.
The approach without semicolons is as old as high level programming languages.
3
u/Uncaffeinated 1subml, polysubml, cubiml Mar 21 '26
It seems to me like "just require semicolons" is by far the most attractive approach. No more worrying about whitespace, no more syntactical gotchas or confusing insertion rules.
3
u/SwedishFindecanor Mar 21 '26 edited Mar 21 '26
I agree with the conclusion. I have not designed new lexical rules for a programming language for some time but I had written down the rule I want in one, in case I would get the urge some day.
Continue a line if either is true:
I think that this rule is both simple to communicate to users of the language, and to use in the lexer.
However, to avoid ambiguity, the syntax must not allow a unary operator to be first on a line. Functions must return values using the
returnstatement, and there might be some functional syntax style that is also not possible.BTW, I think the compiler could have an option to warn when the indentation is larger/smaller than what is expected.
2
u/Jhuyt Mar 18 '26
Very nice article!
2
u/Maurycy5 Mar 19 '26
Wonderfully written!
At Duckling, we gave some thought to statement delimiters as well. We realised that semicolons are... let's face it, at least somewhat annoying. But there were few ways to actually get rid of them without some strange consequences or a grammar full of exceptions.
Python's syntax seemed conveniently simple and effective except for one thing... the trailing backslashes. They would look absolutely ugly and if the length of the longest line in the block changed, then all backslashes moved like in a C macro.
Currently, we still require semicolons like C, but we intend to change this to the following. Statements are to be parsed like in Python, but we want to allow backslashes at the beginning of the line as well. So method call chains are a bit more verbose, but at least in my opinion, it is easy to get used to them.
And your examples would look as follows:
The specifics of indentation and alignment will probably see a lot of freedom.
A penny for your thoughts?
2
u/BoppreH Mar 19 '26
I love these comparative deep dives. It's exactly why I've joined this community, and this is a specially high quality one. Keep it up!
1
u/Lorxu Pika Mar 18 '26
I'm doing something very similar - many grammatical constructs involve an indented "block" in which newlines matter, but when an indent is encountered without starting a block, all subsequent indents and newlines are ignored until the matching dedent (or until the start of an indented block). For example:
1
u/BackgroundWasabi Mar 19 '26
This was a really nice read, thanks for putting this together!
I’ve been banging my head recently trying to come up with an elegant solution for optional semicolons in my language, so I’ll definitely be referring back to this.
1
u/mark-sed github.com/mark-sed/moss-lang/ Mar 19 '26
When I was thinking about this in the context of my own language during design, I also ended up going the "modern route" with `;` and new lines, and I came up with these 2 categories of terminators. You have the semicolon as the "hard terminator", which just is so easy to parse and you always know what it is (the same is for end of a file) and then a new line which is the "soft terminator", that requires extra context to be treated as a terminator. As you write in your post, you can escape new lines or have a new line in `()` so there the parser has to keep some state and check if a new line is in this state a terminator or a white space.
1
u/passiveobserver012 Mar 19 '26
I think its good to split the use case into writing and reading. It seems to me that the semicolon, could even make it easier to read. Much like an end delimiter like `.` in Natural Language. Much better than 'whitespace' which you can not even really 'see' and can be multiple characters (space, newline, ... ) . Much harder to debug than an actually visibile character like ';'.
However for writing it can be a real help for beginners. I bet forgetting semicolon is one of the most made user error when writing. So that could be better, though its usually an easy fix.
If we consider only optimizing the 'writing', then idk if ommitting semicolons is the only option.
1
u/Equal_Debate6439 Mar 21 '26
En mi lenguaje de programación de hecho para evitar el punto y coma uso un normalizador de semicolons, lo que hace es si se usa por arriba
;se sigue normal peor si por arriba se usa newline se elimina 6 se reemplaza con semicolons tokens por debajo osea ajn sigo usando semicolons pero solo por debajo lo que me permite no manipular newlines tokens en parser peor aun asi por arriba si permitir usar newline1
u/Imaginary-Deer4185 Mar 21 '26 edited Mar 21 '26
I don't see the problem, to be honest.
I've written my own language, and there never was a need for semicolons. And certainly no significant whitespace like python either.
It probably depends on how your parser works, I think. It's like, if you have an expression, and the next token isn't one of those that extends the expression, then the expression is terminated.
I also eliminated empty parantheses for calling functions without parameters.
list=List(1,2,3)
Is there any doubt about where the assignment, whether you call it a statement or an expression, ends??
1
u/jibbit Mar 22 '26
it's really interesting, but the conclusions about javascript - in my opinion - are pretty misleading. it was at one time very common (and fashionable) to write js without semicolons. for years it was the predominant style, and the reality was it was easy to do. but then a new wave of tooling came along.. airbnb style guide -> eslint -> prettier, etc. and there was a strong movement to adopt the same, most boring, most consistent, most machine verifiable formatting. a lot of this was fashion (and wanting to work at FAANG)
1
u/church-rosser Mar 23 '26
(+ Lisp 4evah!)
1
u/WalkerCodeRanger Azoth Language Mar 27 '26
The convention in math is to put the operator on the next line. I find languages that force the operator to be on the same line before the newline to be unacceptable.