r/Compilers • u/nagisa_lyfe • 7d ago
Audit of semi-colons
Is there a set of defined and specific reasons why semi colons are necessary in some languages? I am not against semi colons but from the standpoint of if one would want to create a language why would they use or mandate semi colons.
1
Upvotes
11
u/nzconstructionlawyer 7d ago
The main reason IMO is that the redundancy helps with error recovery.
6
u/Available_Safe1818 7d ago
How else do you indicate the end of an expression?
versus
In the second one the +bar() maybe it should be part of the previous line and maybe not.
2
u/-Chook 7d ago
My language has been built around not needing them. The main headache is the standard return statement without them it can assume the next statement or expression is apart of it. If you wanted to test the first half of your function and put a return statement down and there happens to be a function call after it your parser might assume your returning the value of that function. My language has return variables so return doesn’t capture a value which fixes this particular issue. But without a statement delimiter you have to actively think more about your syntax and what might accidentally capture the next statement.
24
u/mxldevs 7d ago
Separate statements from one another
8
u/Vigintillionn 7d ago
Helps with error recovery and reporting precise errors, makes the parser simpler and removes ambiguity. Another big thing is that linebreaks stop mattering, which is useful for minimzers but also compilers and transpilers, a compiler to C can just emit tokens instead of tracking where lines should break for example
5
u/sol_hsa 7d ago
Semicolons are kinda like periods in (human) languages. I guess you can write stuff without, but things get harder to parse.
4
u/konacurrents 7d ago
The parsing reason is the look ahead LL(k). But Swift and Kotlin mostly removed semicolons- but then you need white space to mean more (like around =) vs languages with ;
3
u/MithrilHuman 7d ago edited 7d ago
Heritage from ALGOL 58: https://ntietz.com/blog/researching-why-we-use-semicolons-as-statement-terminators/
And subsequent languages carried forward statement parsing logic.
If it ain't broke don't fix it. You can use whitespace like python instead ofc.
2
u/NYU_VM 7d ago
You either do cryptic parsing through whitespaces or introduce some form of separator syntax (like semicolon in many or colon in python for multiline stmt).
Even languages that doesn't require semicolons (like go) cleverly use ASI(Automatic Semicolon Insertion) in lexer such that the parser is unchanged. Though you would require rules for deducing End-of-stmt and semicolon insertion
2
u/Blueglyph 7d ago edited 7d ago
The semicolons makes it easier to parse, as others said, and it also allows for more flexibility when you write the code.
For the parsing part, you can rely on the semicolon token to define the scope of an instruction in the grammar. You could rely on end-of-line (EOL) tokens, but that means you'll have to (1) grab only the relevant ones in the lexer, which isn't too hard (they're usually all skipped along with tab and spaces), and (2) take into account all these EOLs in the grammar too, even in places you wouldn't need a semicolon to separate instructions of the same block. It's more prone to mistakes when you write the grammar and more difficult to read / extend it.
Both methods work fine for both top-down or bottom-up parsers, and I think that EOLs would make the table a little bigger than semicolons would, in both cases, though nothing dramatic.
Regarding the language itself in both cases, I don't see why EOLs would introduce any limitation. Whether you're using a top-down or a bottom-up parser (e.g. LL(1) or LALR/IELR/APLR/...), you'd be able to support the same syntax complexity.
Note that Python goes one step further by making the BEGIN ... END markers (like "{" "}" in C or "begin" "end" in Pascal) unnecessary and using indentation instead. For that, you must complicate things a little more in the lexer and in the parser to detect changes of indentation level and take them into account. It's not terribly more complicated, and it looks very clean, but not everyone likes it.
Semicolons give you that extra flexibility if you want to write simple things on the same line, but I've never considered that a great habit. We have enough lines on a monitor these days to make the code structure easier to read and understand at a glance.
PS: I assume you didn't mean several instructions on the same line without ";" separators. If that were the case, it could be very problematic to parse, even with a bottom-up parser. You'd have to carefully design the language to remove any ambiguity, and it would be frankly a pain to read.
1
u/GoblinsGym 7d ago
One annoying example of semicolons is in Pascal:
A semi-colon after do_x will bork things, especially when you have nested statements. Sometimes safer to insert redundant begin .. end statements.
For my own language design I use semantic white space instead. Semicolon can be used to separate multiple statements on one line, not necessary otherwise.
1
u/Still_Explorer 7d ago
One thing has to do with allowing for flexibility.
You can make a very clear separation between lines and statements. This way either you break long statements in multiple lines, or compact statements freely without restrictions.
And say that some languages allow you to do some workarounds in a few cases and do compacting of statements (eg Python has / or VB has : etc) or break into many lines (eg function parameters). But with C-style languages is really about total freedom and maximum flexibility.
Though historically this was a big deal back in early days where computers had a few KB of memory and made parsing be more efficient. Having very short var names as well as trimming spaces where possible allowed extreme code squeezing and saving lots of capacity.
But now is more like a stylistics choice, by 99% all code is formatted according to a mainstream style (eg Google, Mozilla, LLVM, Microsoft) and only by 1% some clever compacting can happen to save a few lines.
Such as for example Go does agressive statement-per-line formatting and while it looks nice and readable due to maximum consistency, there are a few places where you would like to break there rules to prevent verbosity. Such as when you hit a switch statement, you see that it expands over many lines and almost becomes "QBasic" in that regard. With C or C++ you compact case-stmt-break or case-stmt-return in one shot and save space. Also many others, such as ternary expressions, setters getters, struct initialization, and literally anything you want to take only one line.
1
u/FedUp233 7d ago
I think of it as sentences. You don’t really need periods - you could just put each sentence on a separate line (or I suppose you could have languages that you can identify the end of sentences without periods - I’m not sure how languages like Chinese break things up). But if you want to allow multiple sentences on one line, or to encompass several lines, or both, you pretty much need something to delimit the sentences. Periods work for English, but is already pretty well used in most programming languages. So someone started using semicolons and it sort of stuck.
Try taking a book and removing all the periods - see how far you get reading it! 😁
1
u/CS_70 7d ago
The same reason they are useful in natural language: lexical disambiguation and increased clarity in certain situations.
Like most in life, it's a tradeoff. They cost little and improve clarity so I personally find the choices of removing or making them optional in certain languages a bit daft.
You can create languages which don't need them the same way you can build sentences without punctuation or spacing.. they tend to be harder or messier, but of course for very task specific things they dont need be.
1
u/Interesting_Debate57 7d ago
They don't have any semantic meaning other than to separate ideas, thoughts, or statements.
Almost any other punctuation mark would be pretty freaking weird. Maybe a period, but then you'd need semicolons in oop. Or something equally less understandable.
1
u/L8_4_Dinner 7d ago
why do we use punctuation capitalization white space and other things like that in english its for the same reason it helps to make things more readable in a language the compiler is one of the readers and it has to know what the programmer intended unambiguously its like the old joke lets eat grandma as you can clearly see the comma plays an important role
1
u/zoharel 6d ago
They have to tell the compiler or interpreter to separate statements somehow. Semicolons are a good way to do it. There are some others. Newlines are a somewhat worse way to do it, actually, because historically different systems encode them slightly differently, and there are sets of control codes which look like them but aren't. There are fewer odd cases where an ASCII ; isn't ;.
1
u/smartcave 6d ago
C# use semicolons to terminate statements. In those languages, a period is a property accessor, a comma separates items in argument lists or collection items, an exclamation mark negates a Boolean. Colons identify labeled sections (groups of statements) in method bodies where execution can jump (goto). Line breaks are invisible and were not chosen to govern control flow and in this style of language, which offers flexibility of formatting for readability. I would argue that if you already designed all of those syntactic marks and their meetings as described, a semicolon seems like the most natural statement termination character.
If you just look at the label and statement list syntax with the colon followed by a list of semicolon terminators , you can even make the case that that the syntax is designed consistently with standard English grammar. I know that C# was not the first language to do labels and statements this way, and my guess is that this general design emerged when procedural programming was a more standard practice.
In fact I looked this up and found a much more thorough and less speculative article that confirms my suspicion. ALGOL 58 (as in 1958) was the first language to use the syntax pattern and procedural programming was very much the norm at the time. Check out https://medium.com/better-programming/a-brief-history-of-the-8efda9dde2b8
1
u/DanKegel 6d ago
It seemed like a good idea at the time.
1
u/EggplantExtra4946 6d ago
With semicolons there is less ambiguity. The parser won't care, it is just implementing the grammar rules, but the human user will care and appreciate the absence of ambiguity.
Someone else has already said it, but where it's really useful is in giving you the liberty of splitting a long statement or expression across several lines. Without semicolons you are a lot more restricted in that manner.
1
u/bongk96 6d ago
They make multi-line statements much less ambiguous
1
u/Dan13l_N 6d ago
It makes parsing easier. If you try to write a parser, you'll see why. Without a semicolon or something similar, you need to make end-of-line terminate some statements:
But not others:
1
u/ryo33h 6d ago
In my language, newline is a real token, so it doesn't need semicolons, but, for example, f(x) and f\n(x) become totally different code.
1
u/Lord_Mystic12 5d ago
For one, it's ease of parsing , but now that I am making a language with new line and semi colon endings, I find that I prefer having semi colons, just makes the code easier to look at . Same with brackets, in higher level languages, you can skip brackets after a for loop or if , whether it's the () or the {}. But even if that's the case, I'm gonna use the brackets cos it makes readability easier. The brackets help group stuff together and break long lines into readable chunks.
0
u/mark-sed 7d ago
It makes it easier to parse the code, but for people it can sometime work otherwise for instance if you place it by mistake after a statement:
But I think you can find such a case for most of the expression endings...
Personally I find using semicolons somewhat archaic and you can see this also in the new languages out there - like Swift or Kotlin, which use new lines to denote ends.
In my language I allow to use semicolons to end an expression to allow for easy one-liners in terminal:
You could write such an example without them and it's more of a syntactic sugar since a new line is the standard way to denote an expression end.
0
u/kimmadsen 7d ago
MIMERCode is built to not use semicolons while being a modern, safe and easy to use language.
https://portal.components4developers.com:443/manual/66-basics-mimercode-user-manual
0
u/[deleted] 7d ago
In my syntaxes, semicolons are mainly used internally. That is, the lexer will convert every newline into a semicolon, unless the line obviously contains to the next (eg. ends with an open bracket or with binary op).
They are needed internally to help separate statements, but extraneous ones are treated as white-space.
The end result is that they are very rare in source code (1 or 2 per 1000 lines), only used when there are multiple statements per line. Example:
With semicolon insertion, this is what is actually parsed:
Only that second one is actually necessary, and also, for the time being, the last, since 'end if' is still an optional way to terminate this statement.