473,883 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to remove // comments

Recently, a heated debate started because of poor mr heathfield
was unable to compile a program with // comments.

Here is a utility for him, so that he can (at last) compile my
programs :-)

More seriously, this code takes 560 bytes. Amazing isn't it? C is very
ompact, you can do great things in a few bytes.

Obviously I have avoided here, in consideration for his pedantic
compiler flags, any C99 issues, so it will compile in obsolete
compilers, and with only ~600 bytes you can run it in the toaster!

--------------------------------------------------------------cut here

/* This program reads a C source file and writes it modified to stdout
All // comments will be replaced by /* ... */ comments, to easy the
porting to old environments or to post it in usenet, where
// comments can be broken in several lines, and messed up.
*/

#include <stdio.h>

/* This function reads a character and writes it to stdout */
static int Fgetc(FILE *f)
{
int c = fgetc(f);
if (c != EOF)
putchar(c);
return c;
}

/* This function skips strings */
static int ParseString(FIL E *f)
{
int c = Fgetc(f);
while (c != EOF && c != '"') {
if (c == '\\')
c = Fgetc(f);
if (c != EOF)
c = Fgetc(f);
}
if (c == '"')
c = Fgetc(f);
return c;
}
/* Skips multi-line comments */
static int ParseComment(FI LE *f)
{
int c = Fgetc(f);

while (1) {
while (c != '*') {
c = Fgetc(f);
if (c == EOF)
return EOF;
}
c = Fgetc(f);
if (c == '/')
break;
}
return Fgetc(f);
}

/* Skips // comments. Note that we use fgetc here and NOT Fgetc */
/* since we want to modify the output before gets echoed */
static int ParseCppComment (FILE *f)
{
int c = fgetc(f);

while (c != EOF && c != '\n') {
putchar(c);
c = fgetc(f);
}
if (c == '\n') {
puts(" */");
c = Fgetc(f);
}
return c;
}

/* Checks if a comment is followed after a '/' char */
static int CheckComment(in t c,FILE *f)
{
if (c == '/') {
c = fgetc(f);
if (c == '*') {
putchar('*');
c = ParseComment(f) ;
}
else if (c == '/') {
putchar('*');
c = ParseCppComment (f);
}
else {
putchar(c);
c = Fgetc(f);
}
}
return c;
}

/* Skips chars between simple quotes */
static int ParseQuotedChar (FILE *f)
{
int c = Fgetc(f);
while (c != EOF && c != '\'') {
if (c == '\\')
c = Fgetc(f);
if (c != EOF)
c = Fgetc(f);
}
if (c == '\'')
c = Fgetc(f);
return c;
}
int main(int argc,char *argv[])
{
FILE *f;
int c;
if (argc == 1) {
fprintf(stderr, "Usage: %s <file.c>\n",arg v[0]);
return EXIT_FAILURE;
}
f = fopen(argv[1],"r");
if (f == NULL) {
fprintf(stderr, "Can't find %s\n",argv[1]);
return EXIT_FAILURE;
}
c = Fgetc(f);
while (c != EOF) {
/* Note that each of the switches must advance the character */
/* read so that we avoid an infinite loop. */
switch (c) {
case '"':
c = ParseString(f);
break;
case '/':
c = CheckComment(c, f);
break;
case '\'':
c = ParseQuotedChar (f);
break;
default:
c = Fgetc(f);
}
}
fclose(f);
return 0;
}

Oct 19 '06
100 5169
Walter Bright wrote:
>
That's what I expected. That pretty much means that trigraphs are a
reasonable solution for such systems, but that since the characters must
be translated anyway, there's not much reason to support trigraphs in
the C language standard itself.
EXACTLY.

Why should the language specs be cluttered with such details?
Why should *I* bother about that?

jacob
Oct 20 '06 #31
Walter Bright wrote:
Jalapeno wrote:
Walter Bright wrote:
Do you need to run non-trigraph C code through a source translater to
get it on to your z/OS system?
Not so much to _get_ the source text to the mainframe but for it to be
usable it'll need to be in EBCDIC.

That's what I expected. That pretty much means that trigraphs are a
reasonable solution for such systems, but that since the characters
must be translated anyway, there's not much reason to support trigraphs
in the C language standard itself.

Character translation is only necessary if the text originates on an
ASCII system. Since all the "home grown" code here (and that supplied
by IBM) originates on EBCDIC systems absolutly no translations are
necessary and trigraphs are useful. All the world is not a PC. The
standard acknowledges that. I also understand that you don't find much
reason to have trigraphs supported. Some people use them, a lot. IBM's
Mainframes have'nt disappeared, they've just been renamed "Servers" ;o).

Oct 20 '06 #32
Andrey Koptyaev wrote:
try this:

#include <stdio.h>
#define BSIZE 200

int main (int argc,char *argv[]){
char *buf;
FILE *in,*out;
void comm(char *);
char *str1="//";
char *str2="\x22\x2f \x2f\x22";
char *buf1,*substr;
int i;

if (argc<3){
printf("to low parameters\n");
return 1;
}
in=fopen(argv[1],"rb");
if (in==NULL){
printf("file not opening %s\n",argv[1]);
return 1;
}
out=fopen(argv[2],"wb");
buf=malloc(BSIZ E);
while(fgets(buf ,BSIZE,in) != NULL){
if (!(substr=strst r(buf,str2))){
if (substr=strstr( buf,str1)){
buf1=calloc(str len(buf)+3,1);
for (i=0;i<(strlen( buf)-strlen(substr)) ;i++)
buf1[i]=buf[i];
buf1=strcat(buf 1,"/*");
for (i=strlen(buf)-strlen(substr)+ 2;i<(strlen(buf )-2);i++)
buf1[i]=buf[i];
buf1=strcat(buf 1,"*/");
buf1=strcat(buf 1,"\x0d\x0a") ;
fputs(buf1,out) ;
free(buf1);
}
else
fputs(buf,out);
}
else
fputs(buf,out);
}
fclose(in);
fclose(out);
free(buf);
return 0;
}
Excuse me but this will blindly search a // sequence anywhere in the
line you get. Even within character strings:

char *a = "cpp coment is // isn't it?";
and there you go, you destroy the source.

You ignore all the discussion, and you put this program...

C'mon...

You can't do this in such a BRUTE force fashion...
If I write
char *a = "//";
it will replace it with
Oct 20 '06 #33
jacob navia wrote:
Walter Bright wrote:
>>
That's what I expected. That pretty much means that trigraphs are a
reasonable solution for such systems, but that since the characters
must be translated anyway, there's not much reason to support
trigraphs in the C language standard itself.

EXACTLY.

Why should the language specs be cluttered with such details?
Why should *I* bother about that?
You should do whatever you like; but note that you are fooling people
if you are saying you are producing a C compiler, since people do not
expect a C compiler to intentionally ignore some parts of C standard.

You are not saying "a compiler system adding some sugar to C language
and removing some standard parts from it" on your web site, are you?
Web site says "lcc-win32 C compiler system".

Regards,
Yevgen
Oct 20 '06 #34
Yevgen Muntyan wrote:
jacob navia wrote:
>Walter Bright wrote:
>>>
That's what I expected. That pretty much means that trigraphs are a
reasonable solution for such systems, but that since the characters
must be translated anyway, there's not much reason to support
trigraphs in the C language standard itself.

EXACTLY.

Why should the language specs be cluttered with such details?
Why should *I* bother about that?


You should do whatever you like; but note that you are fooling people
if you are saying you are producing a C compiler, since people do not
expect a C compiler to intentionally ignore some parts of C standard.

You are not saying "a compiler system adding some sugar to C language
and removing some standard parts from it" on your web site, are you?
Web site says "lcc-win32 C compiler system".

Regards,
Yevgen
Who is tallking about the C compiler?
We are talking (and is the subject of this thread) about this utility
to eliminate // comments!!!

lcc-win32, by the way, will warn you about any trigraphs it sees by
default. If you want to use trigraphs you have to set the option
-ansic.

This is NONSENSE for all users that are NOT EBCDIC and do NOT work in
mainframes. By the way, the venerable 3270 is DEAD SINCE CONCEPTION
and one of the nice things of the microcomputers that appeared in the
eighties was this wonderful KEYBOARDS where we could type any character
we wish... Nice isn't it?
Oct 20 '06 #35
jacob navia wrote:
Yevgen Muntyan wrote:
>jacob navia wrote:
>>Walter Bright wrote:
That's what I expected. That pretty much means that trigraphs are a
reasonable solution for such systems, but that since the characters
must be translated anyway, there's not much reason to support
trigraphs in the C language standard itself.
EXACTLY.

Why should the language specs be cluttered with such details?
Why should *I* bother about that?

You should do whatever you like; but note that you are fooling people
if you are saying you are producing a C compiler, since people do not
expect a C compiler to intentionally ignore some parts of C standard.

You are not saying "a compiler system adding some sugar to C language
and removing some standard parts from it" on your web site, are you?
Web site says "lcc-win32 C compiler system".

Regards,
Yevgen


Who is tallking about the C compiler?
Below is what made me think your compiler does not support trigraphs
(this your reply elsethread). If you meant "my compiler supports
trigraphs but I do not support them" (not sure what you actually meant
then), then I apologize.
Walter Bright wrote:
Peter Nilsson wrote:
>Some test cases for you to consider...

int c = a //* ... */
b;
int d = '??''; // this is a // comment, is it translated?

A trigraph case:

char* d = "??/""; // "

but of course I've never seen trigraphs outside of a test suite.
Me neither. But I do not support trigraphs anyway. They are an
unnecessary feature. We had several lebgthy discussions about this in
comp.std.c.
Oct 20 '06 #36
"Jalapeno" <ja*******@mac. comwrites:
Walter Bright wrote:
>Peter Nilsson wrote:
Some test cases for you to consider...

int c = a //* ... */
b;
int d = '??''; // this is a // comment, is it translated?

A trigraph case:

char* d = "??/""; // "

but of course I've never seen trigraphs outside of a test suite.

Haven't worked in a z/OS shop before, huh? (or a Sys 370 one either)

It only takes an hour or two of working with int a??(8??); to get used
to them (and they become second nature quickly when you see them all
day long).
Fascinating. There have been raging arguments about trigraphs both
here and in comp.std.c for years. I think you're the first person
I've seen who actually *uses* them. Maybe mainframe users just don't
post to Usenet very often?

In my own experience, and that of most people here, trigraphs have
caused far more problems than they solve; if a trigraph appears in a C
source file, it's far more likely to be accidental than intentional
(unless the code is deliberately obfuscated). For example:

fprintf(stderr, "Unexpected error, what happened??!\n") ;

Since there is currently no active effort to publish a new C standard,
it looks like we're stuck with the current situation for the
forseeable future, but some of us are still trying to come up with a
better solution. For example, I've proposed *disabling* trigraphs by
default, but enabling them if there's some unique marker at the top of
the file.

For any change like this, there's a danger of breaking existing code,
but for those of us outside the IBM mainframe world, it would probably
accidentally *fix* more code than it would break.

Also, why do you use trigraphs rather than digraphs? They were added
in a 1995 update to the standard (I think that's right); you could
write a[8] as a<:8:rather than as a??(8??).

Any thoughts?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 20 '06 #37
In article <45************ **********@news .orange.fr>,
jacob navia <ja***@jacob.re mcomp.frwrote:
>This is NONSENSE for all users that are NOT EBCDIC and do NOT work in
mainframes. By the way, the venerable 3270 is DEAD SINCE CONCEPTION
It was? You only had to wait 3 years for DEC to introduce the VT52,
whose 9600 bps serial interface wasn't up to the task of
connecting 17500 terminals to a single 16 megabyte computer.
>and one of the nice things of the microcomputers that appeared in the
eighties was this wonderful KEYBOARDS where we could type any character
we wish... Nice isn't it?
"In the eighties" was literally a decade after the introduction
of the "dead since conception" 3270. And it took another decade (at least)
before all the codepages were in place.

--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Oct 20 '06 #38
Walter Bright said:
jacob navia wrote:
>Walter Bright wrote:
>>A trigraph case:

char* d = "??/""; // "

but of course I've never seen trigraphs outside of a test suite.
Me neither. But I do not support trigraphs anyway. They are an
unnecessary feature. We had several lebgthy discussions about this in
comp.std.c.

Trigraphs are a worthless feature.
This "worthless feature" is sometimes the only way you can get C code to
compile on a particular implementation, because the native character set of
the implementation doesn't contain such fancy characters as { or [ - so to
dismiss it as worthless is to display mere parochialism. I've worked on a
system that had no end of trouble with [ and ] but was quite at home with
??( and ??)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 20 '06 #39
Walter Bright said:
Jalapeno wrote:
>Walter Bright wrote:
>>Do you need to run non-trigraph C code through a source translater to
get it on to your z/OS system?

Not so much to _get_ the source text to the mainframe but for it to be
usable it'll need to be in EBCDIC.

That's what I expected. That pretty much means that trigraphs are a
reasonable solution for such systems, but that since the characters
must be translated anyway, there's not much reason to support trigraphs
in the C language standard itself.
If trigraphs were *not* supported in the Standard, you'd have a heck of a
job getting the same source base to run on, say, MS-DOS (or, nowadays,
Windows) and MVS. Just because you don't use 'em yourself, that doesn't
mean they're not useful.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 20 '06 #40

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1790
by: qwweeeit | last post by:
For a python code I am writing I need to remove all strings definitions from source and substitute them with a place-holder. To make clearer: line 45 sVar="this is the string assigned to sVar" must be converted in: line 45 sVar=s00001 Such substitution is recorded in a file under: s0001="this is the string assigned to sVar"
3
2661
by: Markus | last post by:
Hi! I wanted to select a subset of nodes (list = selectNodes("parent/child") from a XmlDocument, then remove all (parentNode.removeAll();) child-nodes and insert the previous selected nodes back. Of course the XmlNodes in the nodelist ref. where lost once the removeAll() were executed, this is expected. So the obvious way to achive the result would be to clone the nodes or remove the nodes not qualifying the Xpath.
9
5754
by: Frank Potter | last post by:
I only want to remove the comments which begin with "//". I did like this, but it doesn't work. r=re.compile(ur"//+$", re.UNICODE|re.VERBOSE) f=file.open("mycpp.cpp","r") f=unicode(f,"utf8") r.sub(ur"",f) Will somebody show me the right way? Thanks~~
1
14341
by: Andrus | last post by:
I need to remove all comments ( between <!-- and --tags) from XML string. I tried the following code but comments are still present. Or is it better to open xml string with a StreamReader, read all the text inside and remove all the "<!--" and "-->" substrings? How to remove comments ? string RemoveComments(string sDoc) {
3
6295
by: Laurence | last post by:
Hi folks, How to remove the transaction logs which are out of date in HADR environment? DB2 command PRUNE, it can only run on primary database but cannot run on standby database. Because the database conection is not permitted on standby database. USEREXIT, I don't know whether it can run on standby database or
1
4303
by: Andrus | last post by:
I have SQL strings which contain comments starting with -- and continuing to end of line like string mystring=@"SELECT data, -- ending comment -- line comment FROM mytable "; How to remove comment from those strings ? I need to delete charactes starting from -- and ending with line end in
4
9868
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in this context, in fact some articles from pre-2.0 suggest using any collection class of your choice. So my questions focus more on the syntax of event properties provided by the "event" keyword in C#. (Disclaimer - I am a C++ programmer working...
61
3309
by: arnuld | last post by:
I have created a program which creates and renames files. I have described everything in comments. All I have is the cod-duplication. function like fopen, sprint and fwrite are being called again and again. I know to remove code-duplication I have to make functions and pass arguments to them but I am not able to think of a way doing it. Can you post some example for me, out of this code:
3
5287
by: Allen Chen [MSFT] | last post by:
Hi Richard, Quote from Richard================================================== However I also want to be able to remove the panes. I have tried to include this, but find that when I first add the pane the remove event does not get handled, though thereafter it is handled without problems. ==================================================
0
9777
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10726
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10833
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10405
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7957
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5979
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4602
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3226
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.