473,804 Members | 3,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

// comments

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(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 == '*') {
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(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) {
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.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jun 27 '08 #1
40 2683
jacob navia <ja***@nospam.c omwrites:
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?

<snip program>
Note that trigraphs are not supported. Homework: Add that feature
I also does not treat multi-line // comments correctly.

--
Ben.
Jun 27 '08 #2
jacob navia said:
Recently we had poor Mr "teapot" that was horrified at the heresy
of lcc-win of accepting // comments.
It is surely not too surprising that at least one user is horrified by the
idea that a supposedly conforming C90 implementation fails to produce a
diagnostic message for a syntax error. We knew lcc-win32 didn't conform to
C99. Now it appears it doesn't appear to conform to C90 either.

Does it at least have a K&R C mode? If not, it would seem that it isn't
actually a C compiler after all, and is therefore off-topic in
comp.lang.c.
C is a nice language, and you can do anything with it, inclusive a
program that transforms // comments into well behaved /* ... */
ones...
....and fails not only on trigraphs (as you pointed out) but also on the
following input:

/\
/ double-slash comment with line-splice

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #3
Ben Bacarisse wrote:
jacob navia <ja***@nospam.c omwrites:
>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?
-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

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jun 27 '08 #4
jacob navia <ja***@nospam.c omwrites:
Ben Bacarisse wrote:
>jacob navia <ja***@nospam.c omwrites:
>>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?

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

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #5
Keith Thompson said:
jacob navia <ja***@nospam.c omwrites:
<snip>
>Note that many compilers at the C89 level accept // comments,
If they are invoked in conforming mode, they *must* diagnose syntax errors.
>for instance Microsoft compilers

Then my comment applies to them as well.
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.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #6
Richard Heathfield wrote:
Keith Thompson said:
>jacob navia <ja***@nospam.c omwrites:

<snip>
>>Note that many compilers at the C89 level accept // comments,

If they are invoked in conforming mode, they *must* diagnose syntax errors.
Maybe. If they do not, please use another compiler.
>>for instance Microsoft compilers
Then my comment applies to them as well.

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\tes t>type tt.c
// aaaaa

D:\lcc\mc71\tes t>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.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jun 27 '08 #7
On Apr 27, 1:27 am, jacob navia <ja...@nospam.c omwrote:
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)
<snip code>
>
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.
Jun 27 '08 #8
jacob navia wrote, On 27/04/08 08:59:
Richard Heathfield wrote:
>Keith Thompson said:
>>jacob navia <ja***@nospam.c omwrites:

<snip>
>>>Note that many compilers at the C89 level accept // comments,

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

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.
>>>for instance Microsoft compilers
Then my comment applies to them as well.

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\tes t>type tt.c
// aaaaa

D:\lcc\mc71\tes t>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.
--
Flash Gordon
Jun 27 '08 #9
Flash Gordon wrote:
jacob navia wrote, On 27/04/08 08:59:
>Richard Heathfield wrote:
>>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\te st>type tt.c
// aaaaa

D:\lcc\mc71\te st>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.
word games, word games
// aaaaaaa
int a=0;

Now, cl doesn't give any warning:
D:\lcc\mc71\tes t>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\tes t>

WORD GAMES as always.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jun 27 '08 #10

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

Similar topics

4
3380
by: Sims | last post by:
Hi, I proud myself in having good comments, (/**/, // etc...), all over my scripts as well as a very descriptive section at the beginning of the script. No correct me if i am wrong but php must still 'read' those comments? So, do comments technically slow the whole process? Or is the loss of CPU/Time/memory so negligible that i do not need to worry about it.
17
2755
by: lkrubner | last post by:
I've got a PHP application that's 2 megs in size. Of that, my guess is 200k-400k is comments. Do they impose a performance hit? I've been postponing any kind of optimization, but at some point I'll have to do it. Is taking out the comments worth it? Of all the optimizations I can do, where should it rank?
4
10940
by: Uwe Ziegenhagen | last post by:
Hello, my fellows and me implement a c++ tool that is able to divide blank/tab separated files into <number>, <text>, <c-singlelinecomment> and <multilinecomment>. So far it's not working bad, we have just one problem if I call the a.exe that gcc compiles with the following textfile (./a.exe < test.txt) he does not match the multiline comments correctly. *test.txt contains:
28
3466
by: Benjamin Niemann | last post by:
Hello, I've been just investigating IE conditional comments - hiding things from non-IE/Win browsers is easy, but I wanted to know, if it's possible to hide code from IE/Win browsers. I found <!> in the original MSDN documentation, but this is (although it is working) unfortunately non-validating gibberish. So I fooled around trying to find a way to make it valid. And voila: <!--><!><!-->
4
4671
by: lorinh | last post by:
Hi Folks, I'm trying to strip C/C++ style comments (/* ... */ or // ) from source code using Python regexps. If I don't have to worry about comments embedded in strings, it seems pretty straightforward (this is what I'm using now): cpp_pat = re.compile(r""" /\* .*? \*/ | # C comments
40
4649
by: Edward Elliott | last post by:
At the risk of flogging a dead horse, I'm wondering why Python doesn't have any multiline comments. One can abuse triple-quotes for that purpose, but that's obviously not what it's for and doesn't nest properly. ML has a very elegant system for nested comments with (* and *). Using an editor to throw #s in front of every line has limitations. Your editor has to support it and you have to know how to use that feature. Not exactly...
7
1888
by: Bob Stearns | last post by:
Several weeks ago I asked what comments I could pass to DB2 in a SELECT statement. I don't remember whether I said via PHP/ODBC. I was assured that both /* */ style comment blocks and -- comment lines were allowed. Unfortunately, neither work. Both -- and /* */ comments cause a "premature end of statement". Any sage advice?
98
4632
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9569
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
10558
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...
0
10318
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...
0
10069
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
7608
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
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2975
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.