// comments

J

jacob navia

Recently we had poor Mr "teapot" that was horrified at the heresy
of lcc-win of accepting // comments.

C is a nice language, and you can do anything with it, inclusive a
program that transforms // comments into well behaved /* ... */
ones...

------------------------------------------------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>
#include <stdlib.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(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;
}

/* Skips multi-line comments */
static int ParseComment(FILE *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 == '*') {
c = fgetc(f);
if (c == '/') {
c = fgetc(f);
putchar(' ');
}
else {
putchar('*');
}
}
}
if (c == '\n') {
puts(" */");
c = Fgetc(f);
}
return c;

}

/* Checks if a comment is followed after a '/' char */
static int CheckComment(int 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) {
puts("Usage: give an input file name. Writes to stdout");
return EXIT_FAILURE;
}
f = fopen(argv[1],"r");
if (f == NULL) {
puts("Can't find the input file");
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;

}

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

Note that trigraphs are not supported. Homework: Add that feature

Compiled with lcc-win, this program makes only 3 616 bytes. C is
an efficient language.

P.S. Note that I have avoided printf... If you add printf the size
will be a HUGE 37K.
 
B

Ben Bacarisse

jacob navia said:
Recently we had poor Mr "teapot" that was horrified at the heresy
of lcc-win of accepting // comments.

Can you clear up the question of what the -ansi89 flag is intended to
do? Your compiler is allowed to accept any extensions it likes and to
reject and conforming C90 programs it wishes to *unless* you claim that
lc -ansi89 is intended to be a conforming C90 implementation. What is
the purpose of -ansi89?
C is a nice language, and you can do anything with it, inclusive a
program that transforms // comments into well behaved /* ... */
ones...

How do you do that with:

int main(void) { return 1//* divide? */2; }

Is this a valid C90 program or an incorrect C90 one that has a //
comment int it?

Note that trigraphs are not supported. Homework: Add that feature

I also does not treat multi-line // comments correctly.
 
J

jacob navia

Ben said:
Can you clear up the question of what the -ansi89 flag is intended to
do? Your compiler is allowed to accept any extensions it likes and to
reject and conforming C90 programs it wishes to *unless* you claim that
lc -ansi89 is intended to be a conforming C90 implementation. What is
the purpose of -ansi89?

-ansi89 was implemented to the wishes of a paying customer. They wanted
a compiler to that standard, and I implemented it for them. The usage of
that flag is to disable most C99 stuff.

Note that many compilers at the C89 level accept // comments, for instance
Microsoft compilers
 
K

Keith Thompson

jacob navia said:
-ansi89 was implemented to the wishes of a paying customer. They wanted
a compiler to that standard, and I implemented it for them. The usage of
that flag is to disable most C99 stuff.

If your paying customer is satisfied with that, then that's fine.
However, the ANSI C89 standard (or equivalently the ISO C90 standard)
requires a diagnostic for a "//" comment (except in the rare cases
where it can be a legal C89 construct, such as a division operator
immediately followed by a comment). If your compiler doesn't issue a
diagnostic for a "//" comment, then it's not a 100% conforming C89
compiler. I hope that your documentation makes that clear.

Since it's easy enough to implement full C89 conformance in this area
by issuing a warning, perhaps just on the first occurrence in a
translation unit, I fail to understand why you wouldn't go ahead and
do so, but that's up to you.
Note that many compilers at the C89 level accept // comments, for instance
Microsoft compilers

Then my comment applies to them as well.

(If you perceive a personal attack in the above, or an attack on
lcc-win, or on C99, or on // comments, then you've misunderstood me.
Again.)
 
J

jacob navia

Richard said:
Keith Thompson said:


If they are invoked in conforming mode, they *must* diagnose syntax errors.

Maybe. If they do not, please use another compiler.
When invoked in conforming mode, Microsoft C (or at least my copy of it)
issues the necessary diagnostic message if you use // in an erroneous
syntactical context.


D:\lcc\mc71\test>type tt.c
// aaaaa

D:\lcc\mc71\test>cl -W4 tt.c
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

tt.c
tt.c(2) : warning C4206: nonstandard extension used : translation unit
is empty

Of course your version dates from 1991... Always the same word games,
half truths, etc. Pure regulars BS.
 
V

vippstar

Recently we had poor Mr "teapot" that was horrified at the heresy
of lcc-win of accepting // comments.
You seem horrified too. Or horrifying.
C is a nice language, and you can do anything with it, inclusive a
program that transforms // comments into well behaved /* ... */
ones...
Actually, you cannot, at least not correctly. It's impossible to guess
the intend of the programmer in:
int main(void) { return 1//* divide? */2; }
As noted by Mr Bacarisse (but I have seen that example in other places
too)
Note that trigraphs are not supported. Homework: Add that feature Nor digraphs.
Compiled with lcc-win, this program makes only 3 616 bytes. C is
an efficient language.
I think what you're trying to say here is that lcc-win is efficient,
and not C. A language cannot be efficient, not in that sense.
P.S. Note that I have avoided printf... If you add printf the size
will be a HUGE 37K.
Maybe your compiler is not that efficient then (or maybe you did not
mention how you linked the lib).
Here with gcc I get 8K with no optimizations (using or not using
printf).
I wouldn't expect from someone who complains about "the regulars"
mentioning old systems to prove him wrong, to actually mention 37K as
a "HUGE" size. 37K is _nothing_ in a modern computer.
 
F

Flash Gordon

jacob navia wrote, On 27/04/08 08:59:
Maybe. If they do not, please use another compiler.

If you state that -ansi89 does *not* mean conformance to C89 (or C90 or
C95) then as has already been stated it is allowed to accept whatever
you want. If I was one of your paying customers I would not be happy,
but I'm not.
D:\lcc\mc71\test>type tt.c
// aaaaa

D:\lcc\mc71\test>cl -W4 tt.c
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

tt.c
tt.c(2) : warning C4206: nonstandard extension used : translation unit
is empty

Of course your version dates from 1991... Always the same word games,
half truths, etc. Pure regulars BS.

Not at all. You have just demonstrated that what Richard said is true
for the version you have as well. The compiler emitted a diagnostic.
There is no requirement for it to produce an error or abort the
translation. I have already posted saying that all you have to do is
detect it and issue a warning, as has Keith.
 
J

jacob navia

Flash said:
jacob navia wrote, On 27/04/08 08:59:

Not at all. You have just demonstrated that what Richard said is true
for the version you have as well. The compiler emitted a diagnostic.
There is no requirement for it to produce an error or abort the
translation. I have already posted saying that all you have to do is
detect it and issue a warning, as has Keith.

word games, word games
// aaaaaaa
int a=0;

Now, cl doesn't give any warning:
D:\lcc\mc71\test>cl -W4 -c tt.c
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x6
Copyright (C) Microsoft Corporation. All rights reserved.

tt.c

D:\lcc\mc71\test>

WORD GAMES as always.
 
S

santosh

jacob said:
word games, word games
// aaaaaaa
int a=0;

Now, cl doesn't give any warning:
D:\lcc\mc71\test>cl -W4 -c tt.c
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x6
Copyright (C) Microsoft Corporation. All rights reserved.

tt.c

D:\lcc\mc71\test>

WORD GAMES as always.

Well, what do you expect if you do not tell CL to disable language
extensions? Add the /Za switch and also the /Wall switch and try again.
 
K

Keith Thompson

jacob navia said:
word games, word games
// aaaaaaa
int a=0;

Now, cl doesn't give any warning:
D:\lcc\mc71\test>cl -W4 -c tt.c
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x6
Copyright (C) Microsoft Corporation. All rights reserved.

tt.c

D:\lcc\mc71\test>

WORD GAMES as always.

I don't think the phrase "word games" means what you think it means.

You have demonstrated that Microsoft's C compiler, invoked with the
options you specified, is not a conforming C89/C90 compiler. If it
were, it would have issued a diagnostic for the "//".

I don't know whether "cl -W4 -c tt.c" invokes it in what is intended
to be C90 conforming mode. If not, you haven't demonstrated anything
of any significance.

*At most*, you've simply demonstrated that, *if* lcc-win's "-ansi89"
option is intended to invoke it in C89 conforming mode, then it
exhibits a bug (a failure to conform to the C89 standard) that
Microsoft's compiler also exhibits.

None of this matters much to me personally. Since it appears to
matter a great deal to you, perhaps you can answer these questions and
clear this up:

1. Is lcc-win's "-ansi89" option intended to invoke the compiler in a
mode that conforms to the ANSI C89 standard?

2. In that mode, does it issue a diagnostic for a "//" comment?

3. If it doesn't, do you acknowledge that that is a conformance
failure?

If this is a bug, I'm not saying it's terrifying, and I'm not pushing
to you fix it. I'm only asking you to clear up this issue.
 
Y

ymuntyan

jacob navia wrote, On 27/04/08 08:59:



If you state that -ansi89 does *not* mean conformance to C89 (or C90 or
C95) then as has already been stated it is allowed to accept whatever
you want. If I was one of your paying customers I would not be happy,
but I'm not.

Perhaps that customer wanted warnings for stuff which
reduces portability, like variadic macros not understood
by MS compiler?
(Yes I do realize that where were, and hence are, C compilers
which do not understand // comments)

Yevgen
 
Y

ymuntyan

Perhaps that customer wanted warnings for stuff which
reduces portability, like variadic macros not understood
by MS compiler?
(Yes I do realize that where were, and hence are, C compilers
which do not understand // comments)

Actually I was wrong, I have no idea if there was a C
compiler which didn't understand // comments. By the time
when a compiler got to C90 conformance (N years after 1990),
// comments were probably already there. What conforming C90
compiler doesn't understand // comments? Just curious.

Yevgen
 
F

Flash Gordon

jacob navia wrote, On 27/04/08 11:05:
word games, word games

No, statements of fact.
// aaaaaaa
int a=0;

Now, cl doesn't give any warning:
D:\lcc\mc71\test>cl -W4 -c tt.c
Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x6
Copyright (C) Microsoft Corporation. All rights reserved.

tt.c

D:\lcc\mc71\test>

WORD GAMES as always.

You are the one playing word games. You know very well that if MS VC++
is invoked in conforming more it *does* produce a diagnostic for //
style comments. That is does not when *not* in conforming mode is *not*
an excuse for your compiler to fail to produce required diagnostics in a
claimed C89 conforming mode.

Also MS VCC++ not producing a diagnostic in non-conforming mode does not
contradict what Richard says above.

Either accept that on your compiler -ansi89 does not make it conforming
(which would seem strange to me but it's your choice) or accept that you
have a bug. Do not try and use the non-conforming mode of other
compilers as an excuse if you are attempting to provide a conforming mode.
 
B

Ben Bacarisse

Perhaps that customer wanted warnings for stuff which
reduces portability, like variadic macros not understood
by MS compiler?

Then they would be disappointed. I am not sure to what extent it is
either wise or topical to go into details but -ansi89 does not
diagnose variadic macros (as it should). Fortunately, the
implementation of them seems to be broken so the user would likely
find out if they tried to use them. It seems that -ansi89 also
permits long long int, treats restrict as a keyword, does not diagnose
the use of compound literals, permits [static N] in function
parameters... I stopped looking after a while[1].

Its use for checking portability would be rather limited, I think.

[1] To be balanced, it does diagnose some non ANSI C constructs.
 
S

santosh

Ben said:
Perhaps that customer wanted warnings for stuff which
reduces portability, like variadic macros not understood
by MS compiler?

Then they would be disappointed. I am not sure to what extent it is
either wise or topical to go into details but -ansi89 does not
diagnose variadic macros (as it should). Fortunately, the
implementation of them seems to be broken so the user would likely
find out if they tried to use them. It seems that -ansi89 also
permits long long int, treats restrict as a keyword, does not diagnose
the use of compound literals, permits [static N] in function
parameters... I stopped looking after a while[1].

Its use for checking portability would be rather limited, I think.

[1] To be balanced, it does diagnose some non ANSI C constructs.

Maybe jacob implemented -ansi89 switch as a synonym for -ansi?
 
H

Harald van Dijk

Sorry, I don't follow.

With GCC, -ansi means that valid C90 programs will be accepted, but
invalid C90 programs, complete with syntax errors and/or constraint
violations, will not necessarily be rejected. It's possible that jacob
navia made -ansi89 behave the same way in lcc-win32.
 
B

Bartc

Ben Bacarisse said:
Can you clear up the question of what the -ansi89 flag is intended to
do? Your compiler is allowed to accept any extensions it likes and to
reject and conforming C90 programs it wishes to *unless* you claim that
lc -ansi89 is intended to be a conforming C90 implementation. What is
the purpose of -ansi89?


How do you do that with:

int main(void) { return 1//* divide? */2; }

Is this a valid C90 program or an incorrect C90 one that has a //
comment int it?

I think it's more of a problem of the design of the // comment convention,
in that this ambiguity can occur.

I would guess that in the case of Jacob's code, it's assuming input is of a
file where // can legally occur. It will translate that into a form that
will give a compilation error (in any mode). Then the user can trace back
and see immediately where the problem is.

That's no different from feeding this code to practically any compiler that
by default accepts // comments; it will report an error and the user can
either invoke conforming mode or (more sensibly so that it gives no further
problems) turn // into / /).

For Jacob's code to work sensibly, it would need a switch to tell it the
input has pure C90-conforming comments, but if someone knew that, there
would be no need to run the program!
 
B

Ben Bacarisse

Harald van Dijk said:
With GCC, -ansi means that valid C90 programs will be accepted, but
invalid C90 programs, complete with syntax errors and/or constraint
violations, will not necessarily be rejected. It's possible that jacob
navia made -ansi89 behave the same way in lcc-win32.

I considered that meaning, but since santosh was replying to a message
that stated that -ansi89 treats restrict as a keyword, that did not
seem like a reasonable interpretation.
 
F

Flash Gordon

Harald van Dijk wrote, On 27/04/08 13:42:
With GCC, -ansi means that valid C90 programs will be accepted, but
invalid C90 programs, complete with syntax errors and/or constraint
violations, will not necessarily be rejected. It's possible that jacob
navia made -ansi89 behave the same way in lcc-win32.

I believe that on lcc-win32 -ansic selects C99 conformance (modulo bugs
and bits not implemented). So if -ansi99 was a synonym for -ansic it
would also select C99 conformance and thus allow // comments, treat
restrict as a keyword etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

How to remove // comments 100
Command Line Arguments 0
A simple parser 121
Text processing 29
Working with files 1
Serial port 5
Taking a stab at getline 40
hexump.c 79

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top