473,902 Members | 4,610 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 5177
jacob navia wrote:
Bart wrote:
You also forgot the case:

#include <ftp://domain.com/myfile.h>

????
Well, URLs in #include directives...
I don't remember seeing anything that forbids it.
And your program output is very misleading when given the input:

#error // comments not allowed

If they are not allowed...
That was just an example to show that your little program may entirely
change the meaning of an #error message. What if you had:

#error This is never supposed to happen (possible cause: // comments).

Regards,
Bart.

Oct 20 '06 #21
Bart wrote:
jacob navia wrote:
Bart wrote:
You also forgot the case:
>
#include <ftp://domain.com/myfile.h>
????
Well, URLs in #include directives...

I don't remember seeing anything that forbids it.
And your program output is very misleading when given the input:
>
#error // comments not allowed
If they are not allowed...

That was just an example to show that your little program may entirely
change the meaning of an #error message. What if you had:

#error This is never supposed to happen (possible cause: // comments).
Or since we're already talking about URLs, the more likely:

#error Please see http://domain.com/xyz for more information about this
error.

Regards,
Bart.

Oct 20 '06 #22

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).

Oct 20 '06 #23
Jalapeno wrote:
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).
Just for kicks I created a terminal emulator macro that put the '[' and
']' into a source file and the resultant int a›8®; is more
difficult to read than int a??(8??); (at least to me).

The code compiles exactly the same.

Oct 20 '06 #24
jacob navia wrote:
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.
I guess you have never seen a system without the following chars in
its char set. From N869:

5.2.1.1 Trigraph sequences

[#1] All occurrences in a source file of the following
sequences of three characters (called trigraph sequences11))
are replaced with the corresponding single character.

??= # ??) ] ??! |
??( [ ??' ^ ?? }
??/ \ ??< { ??- ~

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>
Oct 20 '06 #25
Jalapeno wrote:
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)
No, I haven't. Nor has anyone I've worked with.

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).
I suppose one can get used to anything <g>.

Do you need to run non-trigraph C code through a source translater to
get it on to your z/OS system?
Oct 20 '06 #26
Bart wrote:
jacob navia wrote:
>Bart wrote:
>>And your program output is very misleading when given the input:

#error // comments not allowed
If they are not allowed...

That was just an example to show that your little program may entirely
change the meaning of an #error message. What if you had:

#error This is never supposed to happen (possible cause: // comments).
I don't think that's a reasonable test case, since presumably C code
that uses // comments would not reasonably expect that #error line to work.
Oct 20 '06 #27
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;
}

Oct 20 '06 #28
Walter Bright wrote:
Jalapeno wrote:
Walter Bright wrote:
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)

No, I haven't. Nor has anyone I've worked with.

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).

I suppose one can get used to anything <g>.

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.

A standard ASCII to EBCDIC conversion utility (like one used in a
typical terminal emulator) that uploads source text from a PC to the
mainframe will see the '[' as 0x5B and the ']' as 0x5D and will
translate them to the EBCDIC '[' as 0xAD and EBCDIC ']' as 0xBD.

so the ASCII text statement:

char x[8]; which in ASCII is

0x63 0x68 0x61 0x72 0x20 0x78 0x5B 0x38 0x5D 0x3B

will be translated in a "typical" terminal emulator utility to:

0x83 0x88 0x81 0x99 0x40 0xA7 0xAD 0xF8 0xBD 0x5E

but on the screen that looks like:

char x›8®; and not char x[8];

this compiles but looks horrible on the screen and you can't type those
characters when you edit, you have to copy and paste those characters
(or create a macro). Even though the '[' and ']' exist in EBCDIC the
3270 family of terminals do not have those characters to type in or to
display.

If I manually change the characters › and ® using the terminal
emulator keyboard to '[' and ']', which the Windows keyboard has, the
encoding becomes 0xBA for '[' and 0xBB for ']' and you have

0x83 0x88 0x81 0x99 0x40 0xA7 0xBA 0xF8 0xBB 0x5E

which becomes a syntax error and won't compile.

Our code base apparently contains "vendor" supplied source in the char
x›8®; format and "home grown" (and IBM supplied sample) source in the
char x??(8??); format. We don't normally modify the vendor source so
there isn't any need to replace the ugly "screen" characters with
trigraphs but the "home grown" code is edited much more frequently and
I've become used to dealing with trigraphs.

Oct 20 '06 #29
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.

Our code base apparently contains "vendor" supplied source in the char
x›8®; format and "home grown" (and IBM supplied sample) source in the
char x??(8??); format. We don't normally modify the vendor source so
there isn't any need to replace the ugly "screen" characters with
trigraphs but the "home grown" code is edited much more frequently and
I've become used to dealing with trigraphs.
Oct 20 '06 #30

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
5763
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
14355
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
6296
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
4307
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
9873
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
3313
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
5289
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
9845
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
11279
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10981
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
10499
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...
0
9673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectóplanning, coding, testing, and deploymentówithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8047
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
7205
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4306
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3323
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.