Jump to content
  • Advertisement
Sign in to follow this  
Shnoutz

Why do we need semicolons?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 + 2
y = 3 + 3

x = 2 + 2 : y = 3 + 3

x = 2 _
+ _
2
It really just boils down to a question of style, I guess.

Share this post


Link to post
Share on other sites
Quote:
Original post by frob
You 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Shnoutz
Where do we need semicolons in your example?

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

a = b
*c = a

Share this post


Link to post
Share on other sites
Quote:
Original post by Shnoutz
Where 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 this post


Link to post
Share on other sites
Quote:
Original post by frob
Quote:
Original post by Shnoutz
Where 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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!