Write a program to remove comments from a c program
Find centralized, trusted content and collaborate around the technologies you use most. Connect and share knowledge within a single location that is structured and easy to search.
I just want the source code with comments stripped, nothing else should be changed. Preference towards an existing tool. I don't want to have to write this myself with regexes, I foresee too many surprises in the code. It depends on how perverse your comments are.
I also have a test file for it, and I tried GCC 4. The current version of SCC is 6. You can also find snapshots of the previous releases 4. The code is still primarily developed under RCS. I'm still working out how I want to use sub-modules or a similar mechanism to handle common library files like stderr.
SCC version 6. It defaults to C11 mode operation. Note that the meaning of the -C flag — mentioned above — flipped between version 4. There is a stripcmt program than can do this:. It requires your source file as a command line argument. I had this problem as well. I found this tool Cpp-Decomment , which worked for me. However it ignores if the comment line extends to next line. In this case, I couldn't find a way in the program so just searched for ignored lines and fixed in manually.
I believe there would be an option for that or maybe you could change the program's source file to do so. Because you use C, you might want to use something that's "natural" to C.
You can use the C preprocessor to just remove comments. The examples given below work with the C preprocessor from GCC. They should work the same or in similar ways with other C perprocessors as well. In case your C preprocessor is not accessible directly, you can try to replace cpp with cc -E , which calls the C compiler telling it to stop after the preprocessor stage. In case your C compiler binary is not cc you can replace cc with the name of your C compiler binary, for example clang.
Note that not all preprocessors support -fpreprocessed. I write a C program using standard C library, around lines, which removes comments of C source code file. I tested with largest cpython source code that contains many comments. In this case it do the job correctly and fast, faster than gcc. Only Problem with this command it cant remove comments that contains more than one line. Here is the code :. It uses following code to preprocess each line in case those comments appear in strings.
If it appears in your code, uh, bad luck. You can replace it with a more complex strings. Then again, gcc 4. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 11 years, 10 months ago.
Active 2 years, 2 months ago. Viewed 70k times. EDIT: Preference towards an existing tool. Improve this question. Mike Mike This is actually a good exercise for using a simple lexer and parser! This is actually a good exercise for using a very complicated lexer and parser.
Pascal: I don't believe Dr. Neil:sorry, but no. A parser deals with the structure of statements. From the viewpoint of the language, a comment is a single token that does not participate in any larger structure. It's no different from a space character in fact, in phase three of translation, each comment is to be replaced by a single space character. As for building the preprocessor into the compiler, the explanation is much simpler: the preprocessor often produces very large output, so communicating it to the compiler efficiently improves compilation speed a lot.
Neil: Perhaps that's best -- you seem to be just repeating the same assertion, with no supporting evidence. You haven't even once pointed to what semantic analysis you think is needed to parse comments correctly, just repeated that it is which the standard not only doesn't require, but doesn't really even allow. You substitute trigraphs, splice lines, then break the source into tokens and sequences of white space including comments. If you try to take more semantics into account than that, you're doing it wrong Improve this question.
Add a comment. Active Oldest Votes. Improve this answer. For the sake of learning, lets pretend it is: Then overall, you could be checking against a look-up table rather than by using a complex series of if-else if. Lundin Lundin 4, 11 11 silver badges 27 27 bronze badges. So, I didn't know any of what you said.
If it returned a char , the EOF value would need to also fit in a char , which would mean it would need to have the value of some valid actual character. That would mean that you'd need to have some other way to figure out if the EOF return value was an actual end-of-file, or the character. That's worse than returning int. Back in the days when all of this was designed at a whim, 7 bit token tables were used, so they could simply have picked any value with MSB set.
Such as 0xFF. But no, surely it must be 0xFFFF! Also god forbid if they designed the function as normally done in professional C, separating data from errors.
But no, getchar must not get a char! This API is just horrible bad, always been, no excuses, no rationales. Like most of stdio. That way it would match most other functions. The caller has to use both anyway, so it's not like it would be in any way easier if the return value was the char.
But yeah, I'm not too familiar with the history there, so consider that a post-facto rationalization if you want. Regardless, as long as it only returns one value, and all char values are valid characters, the retval has to be bigger than a char, regardless of what size they are. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.
Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related 4. Hot Network Questions.
0コメント