![]() |
| | #1 |
| Registered User Join Date: Jul 2009
Posts: 38
| [C] remove comments Don't forget to handle quoted strings and character constants properly. C comments do not nest. So i have to remove all comments from a C program. I dont understand how i'm supposed to handle quoted strings and character constants. Delete them aswell or what do they mean? |
| Tool is offline | |
| | #2 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| I think that it means is that you cannot blindly remove the contents of strings, even if they appear to contain comments.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
| | #3 |
| Registered User Join Date: Jul 2009
Posts: 38
| So basicaly, i have to have 4 states - normal, comment, quoted string, and chracter constant and make sure i only delete comments which i find from normal state. Or is there an easier fix to this problem? |
| Tool is offline | |
| | #4 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Well, that sounds about right to me, actually.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
| | #5 |
| Registered User Join Date: Jul 2009
Posts: 38
| But im interested about the constant state. I dont think a comment start can fit into a constant: If you write '/*' debuger reports an error. So why am i supposed to bother with the constant part? |
| Tool is offline | |
| | #6 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #7 |
| Registered User Join Date: Jul 2009
Posts: 38
| I know about escape char constants, example '\n'. But that only works for escape character \ followed by any character. I dont know if there are any other multi char consts, are there? One which could be filled with a comment? |
| Tool is offline | |
| | #8 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| I would say both constants and defines must use QUOTED string literals, so all you have to do is ignore "comments" that occur inside quotes. AFAIK you cannot extent a quote over multiple lines in C code. Also, a single quote will not escape a comment*, so all you have to worry about is double quoted strings on a single line. I guess the thing about constants and defines is just to draw your attention to the issue. * '\/*' is nonsense anyway, and it WILL open a comment.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
| | #9 | ||||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,352
| Quote:
Quote:
Quote:
Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | ||||
| laserlight is offline | |
| | #10 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
Code: char eg = "this and \
that";
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is offline | |
| | #11 |
| Registered User Join Date: Jul 2009
Posts: 38
| * '\/*' does not open a comment in my compiler. So the only state i have to worry about is quote? And if im allready in the state quote, and i reach a \n newline signal, i have to return into normal state therefore, right? Only if there's \ and then \n, then i dont have to, right? Since i wrote exercise 1-24 today, i didn't include that going into a newline after being in double quote state throws you out of double quote state. Should i add that part aswell? Sorry. But what are string literals? Last edited by Tool; 11-14-2009 at 12:29 PM. |
| Tool is offline | |
| | #12 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Well, it is some kind of error or mistake in any case. Quote:
String literals are strings which appear literally "as is" in the code. In C these are always double quoted (so a string literal could be a define or a variable assignment). AFAIK, the only place you can escape comments is in a string literal. Pretty nearly positive.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 11-14-2009 at 12:33 PM. | |
| MK27 is offline | |
| | #13 | ||
| Registered User Join Date: Jul 2009
Posts: 38
| Quote:
I don't understand a thing what you said here. What would be an error? What do i have to deal with here then. I have to make a parser with states. States are normal and double_quote, you said i should ignore single quote constants. Only from the normal state i delete comments. The only exception for the double quote state is escape sequence \, which i know how it works and i will add it. Other then that, am i supposed to take care of any other forms of exceptions? Quote:
Last edited by Tool; 11-14-2009 at 12:57 PM. | ||
| Tool is offline | |
| | #14 |
| Registered User Join Date: Apr 2006
Posts: 1,193
| What you need to do is come up with a list of test cases that you want to support. The list should include all the complex possibility with single quotes, double quotes, multi line comments and any other possibilities you can think of. Do you need to worry about ??/ used in place of \? What about C++/C99 single line comments? If yes, write a few examples. Only once you have that can you start thinking about how many state you need, and what constitutes a transition between them. Though I suspect that you're original four states are correct and all needed. Five if you support // comments.
__________________ It is too clear and so it is hard to see. A dunce once searched for fire with a lighted lantern. Had he known what fire was, He could have cooked his rice much sooner. Last edited by King Mir; 11-14-2009 at 01:22 PM. |
| King Mir is offline | |
| | #15 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| My point was just '\/*' will not appear in a legal, sensible C program. The only purpose of single quotes in C that I can think of is to indicate character values ('\0', 'x', '\\', '"', etc) which is a finite set that does not include '\/*' '/*' or '//'.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Remove comments | St0rM-MaN | C Programming | 4 | 05-18-2007 11:03 PM |
| program to remove comments from source | Abda92 | C Programming | 12 | 12-25-2006 05:18 PM |
| Request for comments | Prelude | A Brief History of Cprogramming.com | 15 | 01-02-2004 10:33 AM |
| The Art of Writing Comments :: Software Engineering | kuphryn | C++ Programming | 15 | 11-23-2002 05:18 PM |
| remove comments from source code | limbo100 | C Programming | 2 | 09-29-2001 06:25 PM |