# Why do we need semicolons?

This topic is 3750 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am building a new language. I am evaluating the need to put semicolons everywhere like in c++. I understand that the semicolons are for terminating a expression. But do we really need them? A parser can determine when an expression is complete so why do we need semicolons in the first place? ex: useless semicolons: x = 1 + 2; y = 3 + 4; could be written as: x = 2 + 2 y = 3 + 3 There is no operator after the "2" to continue the expression, so why do we need to put semicolons in there? Is there a case where we definitly need to complete an expression with a semicolon?

##### Share on other sites
Sure, they're easy to come up with.

a = b - c

a = b * c

while(a) while(b) c

More subtly, there are other constructions that are unambiguous but, if allowed, would make parsing much more processing-intensive.

As a historical note, the first versions of C made semicolons optional.

##### Share on other sites
You need something to indicate an "end of statement". While it is possible to detect implied sequence points like you presented (where you have two consecutive identifiers without operators), there are some situations where you cannot do it without ambiguity.

Note that you don't strictly need to use a semicolon in C and C++. You could use the comma, or possibly use other sequence-point generators like &&, or in some cases you could use other control elements like ( ) or { }.

##### Share on other sites
Where do we need semicolons in your example?

a = b - c
a = b * c

while(a)
while(b)
c

note that I am not arguing about the use of brackets to disambiguate flow control statements.

##### Share on other sites
Quote:
 I understand that the semicolons are for terminating a expression. But do we really need them?
No, not really. Usually you're dealing with one statement per line, so using the semicolon to delimit multiple statements on one line or omitting the semicolon to extend a single statement over multiple lines is the exception rather than the rule.

I personally prefer the way BASIC handles this, using the colon to delimit multiple statements on one line and a line continuation character to extend a single statement over multiple lines.
x = 2 + 2y = 3 + 3x = 2 + 2 : y = 3 + 3x = 2 _    + _    2
It really just boils down to a question of style, I guess.

##### Share on other sites
Quote:
 Original post by frobYou need something to indicate an "end of statement". While it is possible to detect implied sequence points like you presented, there are countless situations where you cannot.Note that you don't strictly need to use a semicolon in C and C++. You could use the comma, or possibly use other sequence-point generators like &&, or in some cases you could use other control elements like ( ) or { }.

Can you give me an example of such situation?

There are languages where the "end of statement" symbol is simply a white space, Lua for instance (they allow the use of semicolon but is not required)

##### Share on other sites
Probably for the same reason natural languages use full stops. You could write without them and you should be able to parse the meaning correctly with some effort (except for some cases that may remain ambiguous).

I was under the impression that computer languages generally require end-of-expression to be signalled. Either use semicolons or end-of-line.

One problem with a completely free-style language might be programmer typos. What if I accidentally forget to type an operator. In C-like languages the compiler would complain that it expects a semicolon (if that's what you meant). In your language the compiler might happily interpret the invalid statement as something completely different than what the programmer meant. (I don't know, may-be there are languages of this kind and it's not a problem or can be made into a non-issue through careful language design.)

##### Share on other sites
Quote:
 Original post by ShnoutzWhere do we need semicolons in your example?

Those without operators between them are simple, so consider this one:

a = b
*c = a

##### Share on other sites
Quote:
 Original post by ShnoutzWhere do we need semicolons in your example?a = b - c

Means either a=b; -c; or a=b-c;. Second example is similar.
Quote:
 while(a) while(b) c

Means either while(a) while(b) c; or while(a); while(b) c; or while(a);while(b);c;

##### Share on other sites
Quote:
Original post by frob
Quote:
 Original post by ShnoutzWhere do we need semicolons in your example?

Those without operators between them are simple, so consider this one:

a = b
*c = a

One could of course argue that it is a flaw in the language to "overload" the meaning of symbols like that (* is dereference or multiplication or pointer declaration).

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5
JoeJ
13

• 9
• 14
• 10
• 25
• 9
• ### Forum Statistics

• Total Topics
632645
• Total Posts
3007629
• ### Who's Online (See full list)

There are no registered users currently online

×