www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - for in D versus C and C++

reply Steve Teale <steve.teale britseyeview.com> writes:
for (; a<b; a++);

is illegal in D.

Doesn't this break a lot of C and C++ code?
Mar 19 2009
next sibling parent reply Sergey Gromov <snake.scaly gmail.com> writes:
Thu, 19 Mar 2009 06:35:37 -0400, Steve Teale wrote:

 for (; a<b; a++);
 
 is illegal in D.
 
 Doesn't this break a lot of C and C++ code?

for (; a<b; a++) {} is legal. I don't think that an empty statement after for is used in "a lot of code."
Mar 19 2009
next sibling parent =?UTF-8?B?QWxleGFuZGVyIFDDoW5law==?= writes:
Sergey Gromov wrote:
 for (; a<b; a++) {}
 
 is legal.  I don't think that an empty statement after for is used in "a
 lot of code."

Only in Enterprise Software!
Mar 19 2009
prev sibling next sibling parent reply BCS <none anon.com> writes:
Hello Sergey,

 Thu, 19 Mar 2009 06:35:37 -0400, Steve Teale wrote:
 
 for (; a<b; a++);
 
 is illegal in D.
 
 Doesn't this break a lot of C and C++ code?
 

is legal. I don't think that an empty statement after for is used in "a lot of code."

it's a trivial fix and easy to find. Heck, you hardly need to think!
Mar 19 2009
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
BCS:
 it's a trivial fix and easy to find. Heck, you hardly need to think!

And it helps to avoid other kinds of bugs quite less easy to find. I have found one of such bugs porting C code to D, the line after the for(); was meant to be looped on :-) Bye, bearophile
Mar 19 2009
prev sibling next sibling parent reply Walter Bright <newshound1 digitalmars.com> writes:
BCS wrote:
 Thu, 19 Mar 2009 06:35:37 -0400, Steve Teale wrote:

 for (; a<b; a++);

 is illegal in D.

 Doesn't this break a lot of C and C++ code?

is legal. I don't think that an empty statement after for is used in "a lot of code."

it's a trivial fix and easy to find. Heck, you hardly need to think!

No, it isn't easy to find. This is in D because a colleague of mine, who was an expert C programmer (the best in the company I was working for), came to me with: for (xxx; i < 10; i++); { ... code ... } and said he could not figure out why his loop executed only and exactly once. He'd fiddled with it for a whole afternoon. He said he must be missing something obvious. I said you've got an extra ; after the ). He smacked his head and about fell over backwards. So it's illegal in D, along with: if (condition); and similar constructs. Have to use a { } to indicate a blank statement.
Mar 19 2009
next sibling parent BCS <ao pathlink.com> writes:
Reply to Walter,

 BCS wrote:
 
 Thu, 19 Mar 2009 06:35:37 -0400, Steve Teale wrote:
 
 for (; a<b; a++);
 
 is illegal in D.
 
 Doesn't this break a lot of C and C++ code?
 

is legal. I don't think that an empty statement after for is used in "a lot of code."


who was an expert C programmer (the best in the company I was working for), came to me with:

Um, that's not what I was referring to. The problem the OP referenced seems trivial enough to me: I try to compile my translated code and get a nice error Line 3: use '{ }' for an empty statement, not a ';' I jump to that line, figure out if it was a bug in the original C code or if it is supposed to be that way and and fix it
Mar 19 2009
prev sibling next sibling parent reply Walter Bright <newshound1 digitalmars.com> writes:
Denis Koroskin wrote:
 That was less than a week ago. I'd say that this type of bugs will live 
 as long as C++ lives.

The thing is, even if the programmer *intended* to put the ; there, it looks like a bug to the maintenance programmer, and he has to spend extra effort determining if it was intended or not. I doubt anyone types { } by accident.
Mar 19 2009
parent reply "Rioshin an'Harthen" <rharth75 hotmail.com> writes:
"Walter Bright" <newshound1 digitalmars.com> kirjoitti viestissä 
news:gpu1sn$sn2$1 digitalmars.com...
 Denis Koroskin wrote:
 That was less than a week ago. I'd say that this type of bugs will live 
 as long as C++ lives.

The thing is, even if the programmer *intended* to put the ; there, it looks like a bug to the maintenance programmer, and he has to spend extra effort determining if it was intended or not. I doubt anyone types { } by accident.

I have a habit of indenting the semicolon on the following line, if I wanted a blank statement, like for (...; ...; ...) ; Much clearer in my opinion than having it on the same line as the for construct.
Mar 20 2009
parent reply Walter Bright <newshound1 digitalmars.com> writes:
Rioshin an'Harthen wrote:
 "Walter Bright" <newshound1 digitalmars.com> kirjoitti viestissä 
 I doubt anyone types { } by accident.

I have a habit of indenting the semicolon on the following line, if I wanted a blank statement, like for (...; ...; ...) ; Much clearer in my opinion than having it on the same line as the for construct.

Yes, that's ok, too. The only issue with it as far as working it into the language goes is then the parser and lexer do not have a clean separation - whitespace has meaning in one case.
Mar 20 2009
parent reply Sean Kelly <sean invisibleduck.org> writes:
== Quote from Walter Bright (newshound1 digitalmars.com)'s article
 Rioshin an'Harthen wrote:
 "Walter Bright" <newshound1 digitalmars.com> kirjoitti viestissä
 I doubt anyone types { } by accident.

I have a habit of indenting the semicolon on the following line, if I wanted a blank statement, like for (...; ...; ...) ; Much clearer in my opinion than having it on the same line as the for construct.

the language goes is then the parser and lexer do not have a clean separation - whitespace has meaning in one case.

Sounds like HTTP/HTML. The best I've come up with so far for parsing that stuff is to have the lexer actually return tokens representing whitespace in some instances. It's totally ridiculous.
Mar 20 2009
next sibling parent reply Walter Bright <newshound1 digitalmars.com> writes:
Sean Kelly wrote:
 Sounds like HTTP/HTML.  The best I've come up with so far for parsing
 that stuff is to have the lexer actually return tokens representing whitespace
 in some instances.  It's totally ridiculous.

When you see ad-hoc designs like that, it's obvious the designer has no experience with compilers. It's why every programmer should take a basic course in compiler design <g>.
Mar 20 2009
parent reply Sean Kelly <sean invisibleduck.org> writes:
== Quote from Walter Bright (newshound1 digitalmars.com)'s article
 Sean Kelly wrote:
 Sounds like HTTP/HTML.  The best I've come up with so far for parsing
 that stuff is to have the lexer actually return tokens representing whitespace
 in some instances.  It's totally ridiculous.

experience with compilers. It's why every programmer should take a basic course in compiler design <g>.

I very much agree. In fact, I'd go so far as to say that my compiler design course was the single most valuable CS course I took while in college. It's amazing how many problems I encounter have something to do with parsing or language translation. It's also amazing how many crappy parsers there are out there for these same tasks. Clearly, compiler design doesn't get as much attention as it should in undergrad CS.
Mar 20 2009
next sibling parent Walter Bright <newshound1 digitalmars.com> writes:
Sean Kelly wrote:
 I very much agree.  In fact, I'd go so far as to say that my compiler design
 course was the single most valuable CS course I took while in college.  It's
 amazing how many problems I encounter have something to do with parsing
 or language translation.  It's also amazing how many crappy parsers there
 are out there for these same tasks.  Clearly, compiler design doesn't get
 as much attention as it should in undergrad CS.

Reminds me of a metal shop class I heard about where for the first assignment each student was handed a chunk of metal and a file. His job was to file out an end wrench. The idea was to get a feel for working metal, besides developing an appreciation for what power tools do! It also filtered out the impatient, slap-dash and careless people who should never be around metal milling machines. In my high school wood shop, we were each handed a chunk of wood, a try-square, and a plane. Our job was to use the plane to create a perfectly square block. We weren't allowed to use any power tools until we passed that. I still remember the shrieks of agony from some of the students as the teacher would lay the square on it, point to a crack of light seeping under it, and pronounce "not square yet!" Good times.
Mar 20 2009
prev sibling parent reply Georg Wrede <georg.wrede iki.fi> writes:
Sean Kelly wrote:
 == Quote from Walter Bright (newshound1 digitalmars.com)'s article
 Sean Kelly wrote:
 Sounds like HTTP/HTML.  The best I've come up with so far for parsing
 that stuff is to have the lexer actually return tokens representing whitespace
 in some instances.  It's totally ridiculous.

experience with compilers. It's why every programmer should take a basic course in compiler design <g>.

I very much agree. In fact, I'd go so far as to say that my compiler design course was the single most valuable CS course I took while in college. It's amazing how many problems I encounter have something to do with parsing or language translation. It's also amazing how many crappy parsers there are out there for these same tasks. Clearly, compiler design doesn't get as much attention as it should in undergrad CS.

Sometimes I need to have a command line UI in a program. Such programs usually have 5 to 10 commands, with their parameters. One command per line. So far I have tested and split the command line with regular expressions, because using a parser generator has felt like shooting mosquitos with a shotgun. What would your strategy be?
Mar 21 2009
parent reply Walter Bright <newshound1 digitalmars.com> writes:
Georg Wrede wrote:
 Sometimes I need to have a command line UI in a program. Such programs 
 usually have 5 to 10 commands, with their parameters. One command per line.
 
 So far I have tested and split the command line with regular 
 expressions, because using a parser generator has felt like shooting 
 mosquitos with a shotgun.
 
 What would your strategy be?

If it's 5 or 10, you can get by with ad-hoc. But if you find yourself repeatedly fixing bugs in the parsing, it's time to consider a more principled approach.
Mar 21 2009
parent reply Sean Kelly <sean invisibleduck.org> writes:
Walter Bright wrote:
 Georg Wrede wrote:
 Sometimes I need to have a command line UI in a program. Such programs 
 usually have 5 to 10 commands, with their parameters. One command per 
 line.

 So far I have tested and split the command line with regular 
 expressions, because using a parser generator has felt like shooting 
 mosquitos with a shotgun.

 What would your strategy be?

If it's 5 or 10, you can get by with ad-hoc. But if you find yourself repeatedly fixing bugs in the parsing, it's time to consider a more principled approach.

I've found that once I created one lexer it could be re-used pretty easily for other languages too. And recursive descent parsers are trivial to write. It may be overkill for command-line parameters, but for anything remotely structured it's generally worth using.
Mar 21 2009
parent reply Walter Bright <newshound1 digitalmars.com> writes:
Sean Kelly wrote:
 I've found that once I created one lexer it could be re-used pretty 
 easily for other languages too.  And recursive descent parsers are 
 trivial to write.  It may be overkill for command-line parameters, but 
 for anything remotely structured it's generally worth using.

When I was looking into parsing date strings, I thought it would be much easier if I adopted a lex/parse style approach. The result is in std.dateparse. The payoff is I've had very little trouble with it.
Mar 21 2009
next sibling parent Georg Wrede <georg.wrede iki.fi> writes:
Walter Bright wrote:
 Sean Kelly wrote:
 I've found that once I created one lexer it could be re-used pretty 
 easily for other languages too.  And recursive descent parsers are 
 trivial to write.  It may be overkill for command-line parameters, but 
 for anything remotely structured it's generally worth using.

When I was looking into parsing date strings, I thought it would be much easier if I adopted a lex/parse style approach. The result is in std.dateparse. The payoff is I've had very little trouble with it.

Ah, seems you can't parse dates in Nepal. Read dateparse.d, and found this in line 81: if ( year == year.init || (month < 1 || month > 12) || (day < 1 || day > 31) || (hours < 0 || hours > 23) || (minutes < 0 || minutes > 59) || (seconds < 0 || seconds > 59) || (tzcorrection != int.min && ((tzcorrection < -2300 || tzcorrection > 2300) || (tzcorrection % 10))) ) The last line here causes timezones that are not at full 10 minutes, to fail. Kathmandu, Nepal is UTC+5:45, according to Wikipedia TimeZone.
Mar 23 2009
prev sibling parent reply Georg Wrede <georg.wrede iki.fi> writes:
Walter Bright wrote:
 Sean Kelly wrote:
 I've found that once I created one lexer it could be re-used pretty 
 easily for other languages too.  And recursive descent parsers are 
 trivial to write.  It may be overkill for command-line parameters, but 
 for anything remotely structured it's generally worth using.

When I was looking into parsing date strings, I thought it would be much easier if I adopted a lex/parse style approach. The result is in std.dateparse. The payoff is I've had very little trouble with it.

It's a shame that this kind of lexing/parsing seems to need gotos. I always feel guilty if I put a goto in my code. (Guess I've had too stern teachers, or something...)
Mar 23 2009
parent Georg Wrede <georg.wrede iki.fi> writes:
Georg Wrede wrote:
 Walter Bright wrote:
 (Guess I've had too stern teachers, or something...)

But then in those times, when somebody said "Michael Jackson", everybody thought of structured programming, not pop-icon tragedies.
Mar 23 2009
prev sibling parent Robert Fraser <fraserofthenight gmail.com> writes:
Sean Kelly wrote:
 The best I've come up with so far for parsing
 that stuff is to have the lexer actually return tokens representing whitespace
 in some instances.  It's totally ridiculous.

The Descent port of the DMDFE lexer does this for formatting source code. It's a neat trick for formatting, but shouldn't be necessary for compilation. Also, was there ever a consensus on whether whitespace has meaning in XML?
Mar 20 2009
prev sibling parent reply Steve Teale <steve.teale britseyeview.com> writes:
Walter Bright Wrote:

 BCS wrote:
 Thu, 19 Mar 2009 06:35:37 -0400, Steve Teale wrote:

 for (; a<b; a++);

 is illegal in D.

 Doesn't this break a lot of C and C++ code?

is legal. I don't think that an empty statement after for is used in "a lot of code."

it's a trivial fix and easy to find. Heck, you hardly need to think!

No, it isn't easy to find. This is in D because a colleague of mine, who was an expert C programmer (the best in the company I was working for), came to me with: for (xxx; i < 10; i++); { ... code ... } and said he could not figure out why his loop executed only and exactly once. He'd fiddled with it for a whole afternoon. He said he must be missing something obvious. I said you've got an extra ; after the ). He smacked his head and about fell over backwards. So it's illegal in D, along with: if (condition); and similar constructs. Have to use a { } to indicate a blank statement.

I just find this scary, because although it says at the top of the page "most of this will be familiar to C/C++ programmers" this little fact means I have to go through all of the familiar constructs with a fine tooth comb. Also, nobody is answering the question. I can work out the alternatives, but I'd just prefer it if existing code worked. I used to use the idiom I quote all the time.
Mar 19 2009
next sibling parent Walter Bright <newshound1 digitalmars.com> writes:
Steve Teale wrote:
 I just find this scary, because although it says at the top of the
 page "most of this will be familiar to C/C++ programmers" this little
 fact means I have to go through all of the familiar constructs with a
 fine tooth comb.

It shouldn't be scary, because the idea is if some common C struct is not acceptable in D, one gets a decent compiler error message. A scary thing would be if the code was silently accepted but did something different.
 Also, nobody is answering the question. I can work out the
 alternatives, but I'd just prefer it if existing code worked. I used
 to use the idiom I quote all the time.

I'm not understanding your question, then.
Mar 19 2009
prev sibling parent Gide Nwawudu <gide btinternet.com> writes:
On Thu, 19 Mar 2009 14:16:25 -0400, Steve Teale
<steve.teale britseyeview.com> wrote:

Walter Bright Wrote:

 BCS wrote:
 Thu, 19 Mar 2009 06:35:37 -0400, Steve Teale wrote:

 for (; a<b; a++);

 is illegal in D.

 Doesn't this break a lot of C and C++ code?

is legal. I don't think that an empty statement after for is used in "a lot of code."

it's a trivial fix and easy to find. Heck, you hardly need to think!

No, it isn't easy to find. This is in D because a colleague of mine, who was an expert C programmer (the best in the company I was working for), came to me with: for (xxx; i < 10; i++); { ... code ... } and said he could not figure out why his loop executed only and exactly once. He'd fiddled with it for a whole afternoon. He said he must be missing something obvious. I said you've got an extra ; after the ). He smacked his head and about fell over backwards. So it's illegal in D, along with: if (condition); and similar constructs. Have to use a { } to indicate a blank statement.

I just find this scary, because although it says at the top of the page "most of this will be familiar to C/C++ programmers" this little fact means I have to go through all of the familiar constructs with a fine tooth comb. Also, nobody is answering the question. I can work out the alternatives, but I'd just prefer it if existing code worked. I used to use the idiom I quote all the time.

 ... but I'd just prefer it if existing code worked ...

What D is saying, is that the existing code is likely to be wrong, and should be altered before it compiles. Besides C!=D so I don't see the problem. Gide
Mar 19 2009
prev sibling parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
BCS wrote:
 Hello Sergey,
 
 Thu, 19 Mar 2009 06:35:37 -0400, Steve Teale wrote:

 for (; a<b; a++);

 is illegal in D.

 Doesn't this break a lot of C and C++ code?

is legal. I don't think that an empty statement after for is used in "a lot of code."

it's a trivial fix and easy to find. Heck, you hardly need to think!

It's not easy to find. I didn't understand why that "for" was illegal until I saw Alexander Pnek's second answer and noticed there was a semicolon. I didn't understand why "for (; a<b; a++)" was wrong. :-P
Mar 19 2009
parent BCS <ao pathlink.com> writes:
Reply to Ary,

 It's not easy to find.
 

See my reply to walter
Mar 19 2009
prev sibling next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Thu, 19 Mar 2009 20:32:54 +0300, Walter Bright <newshound1 digitalmars.com>
wrote:

 BCS wrote:
 Thu, 19 Mar 2009 06:35:37 -0400, Steve Teale wrote:

 for (; a<b; a++);

 is illegal in D.

 Doesn't this break a lot of C and C++ code?

is legal. I don't think that an empty statement after for is used in "a lot of code."


No, it isn't easy to find. This is in D because a colleague of mine, who was an expert C programmer (the best in the company I was working for), came to me with: for (xxx; i < 10; i++); { ... code ... } and said he could not figure out why his loop executed only and exactly once. He'd fiddled with it for a whole afternoon. He said he must be missing something obvious. I said you've got an extra ; after the ). He smacked his head and about fell over backwards. So it's illegal in D, along with: if (condition); and similar constructs. Have to use a { } to indicate a blank statement.

Funny enough, or programming department chief posted the following question in our corporate newsgroup: Why the hell this function enters infinite loop? void treeWalkWithoutRecursion( Node* head ) { Stack s; s.push( head ); while ( !s.empty() ); { Node* tmp = s.pop(); if ( !tmp->marked ) { if ( tmp->right ) s.push( tmp->right ); s.push( tmp ); if ( tmp->left ) s.push( tmp->left ); tmp->marked = true; } else doSomeThing( tmp ); } }
Mar 19 2009
prev sibling next sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Thu, 19 Mar 2009 20:43:18 +0300, Denis Koroskin <2korden gmail.com> wrote:

 On Thu, 19 Mar 2009 20:32:54 +0300, Walter Bright  
 <newshound1 digitalmars.com> wrote:

 BCS wrote:
 Thu, 19 Mar 2009 06:35:37 -0400, Steve Teale wrote:

 for (; a<b; a++);

 is illegal in D.

 Doesn't this break a lot of C and C++ code?

is legal. I don't think that an empty statement after for is used in "a lot of code."


No, it isn't easy to find. This is in D because a colleague of mine, who was an expert C programmer (the best in the company I was working for), came to me with: for (xxx; i < 10; i++); { ... code ... } and said he could not figure out why his loop executed only and exactly once. He'd fiddled with it for a whole afternoon. He said he must be missing something obvious. I said you've got an extra ; after the ). He smacked his head and about fell over backwards. So it's illegal in D, along with: if (condition); and similar constructs. Have to use a { } to indicate a blank statement.

Funny enough, or programming department chief posted the following question in our corporate newsgroup: Why the hell this function enters infinite loop? void treeWalkWithoutRecursion( Node* head ) { Stack s; s.push( head ); while ( !s.empty() ); { Node* tmp = s.pop(); if ( !tmp->marked ) { if ( tmp->right ) s.push( tmp->right ); s.push( tmp ); if ( tmp->left ) s.push( tmp->left ); tmp->marked = true; } else doSomeThing( tmp ); } }

That was less than a week ago. I'd say that this type of bugs will live as long as C++ lives.
Mar 19 2009
prev sibling parent "Denis Koroskin" <2korden gmail.com> writes:
On Thu, 19 Mar 2009 22:05:21 +0300, Walter Bright <newshound1 digitalmars.com>
wrote:

 Steve Teale wrote:
 I just find this scary, because although it says at the top of the
 page "most of this will be familiar to C/C++ programmers" this little
 fact means I have to go through all of the familiar constructs with a
 fine tooth comb.

It shouldn't be scary, because the idea is if some common C struct is not acceptable in D, one gets a decent compiler error message. A scary thing would be if the code was silently accepted but did something different.
 Also, nobody is answering the question. I can work out the
 alternatives, but I'd just prefer it if existing code worked. I used
 to use the idiom I quote all the time.

I'm not understanding your question, then.

I believe he is asking for a way to force that code to compile (a compiler switch or have it enabled by default).
Mar 19 2009
prev sibling next sibling parent reply Don <nospam nospam.com> writes:
Steve Teale wrote:
 for (; a<b; a++);
 
 is illegal in D.
 
 Doesn't this break a lot of C and C++ code?

It breaks a lot of broken C and C++ code.
Mar 19 2009
next sibling parent reply Steve Teale <steve.teale britseyeview.com> writes:
Don Wrote:

 Steve Teale wrote:
 for (; a<b; a++);
 
 is illegal in D.
 
 Doesn't this break a lot of C and C++ code?

It breaks a lot of broken C and C++ code.

OK, that's possibly the case, but you know that among C programmers in particular there was an absolute prohibition on typing more characters than necessary.
Mar 19 2009
parent =?UTF-8?B?QWxleGFuZGVyIFDDoW5law==?= writes:
Steve Teale wrote:
 Don Wrote:
 
 Steve Teale wrote:
 for (; a<b; a++);

 is illegal in D.

 Doesn't this break a lot of C and C++ code?


OK, that's possibly the case, but you know that among C programmers in particular there was an absolute prohibition on typing more characters than necessary.

It’s just one character more in D. Really just the closing curly bracket that adds up. for(;;); vs. for(;;){}
Mar 19 2009
prev sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
On Thu, 19 Mar 2009 19:58:48 +0300, Alexander Pánek
<alexander.panek brainsware.org> wrote:

 Steve Teale wrote:
 Don Wrote:

 Steve Teale wrote:
 for (; a<b; a++);

 is illegal in D.

 Doesn't this break a lot of C and C++ code?


particular there was an absolute prohibition on typing more characters than necessary.

It’s just one character more in D. Really just the closing curly bracket that adds up. for(;;); vs. for(;;){}

In D, you can for(;){} :p
Mar 19 2009
parent BCS <ao pathlink.com> writes:
Reply to Denis,

 On Thu, 19 Mar 2009 19:58:48 +0300, Alexander Pnek
 <alexander.panek brainsware.org> wrote:
 
 Steve Teale wrote:
 
 Don Wrote:
 
 Steve Teale wrote:
 
 for (; a<b; a++);
 
 is illegal in D.
 
 Doesn't this break a lot of C and C++ code?
 


in particular there was an absolute prohibition on typing more characters than necessary.

bracket that adds up. for(;;); vs. for(;;){}


or int i; for(if(i%2) i++; i< 10; i++) {}
Mar 19 2009
prev sibling parent Robert Fraser <fraserofthenight gmail.com> writes:
Steve Teale wrote:
 for (; a<b; a++);
 
 is illegal in D.
 
 Doesn't this break a lot of C and C++ code?

Among my top 10 D features! This has caught more bugs in my code than any "warning"s have in any other language (I do that a lot...).
Mar 19 2009