| Messages 1-10 from thread "Want a way to strip comments from a" |
Next 10
Jump to [ End of thread ]
> /* Written 9:58 am Mar 9, 1989 by jrv@siemens.UUCP */
> /* ---------- "Want a way to strip comments from a" ---------- */
> Does anyone have a sed or awk script which we
> can use to preprocess the C source and get rid of all the comments before
> sending it to the compiler?
The following works in vi: :%s/\/\*.*\*\///g
I don't know if it will work in sed, but it should...
...............................................................................
"Don't drop acid, take it pass-fail!"
Brian Michael Wendt UUCP: {cepu,uiucdcs,noao}!bradley!brian
Bradley University ARPA: cepu!bradley!brian@seas.ucla.edu
(309) 677-2335 ICBM: 40 40' N 89 34' W
In article <9900010@bradley> brian@bradley.UUCP writes:
> The following works in vi: :%s/\/\*.*\*\///g
/*
* Unfortunately, multi-line comments aren't deleted.
*/
---
Steve Kennedy
cbatt!cbosgd!smk
In article <9900010@bradley> brian@bradley.UUCP writes:
> The following works in vi: :%s/\/\*.*\*\///g
/* And this */ important_variable = 42 /* doesn't work either! */
---
Steve Kennedy
cbatt!cbosgd!smk
In article <9900010@bradley>, brian@bradley.UUCP writes:
|
| > /* Written 9:58 am Mar 9, 1989 by jrv@siemens.UUCP */
| > /* ---------- "Want a way to strip comments from a" ---------- */
| > Does anyone have a sed or awk script which we
| > can use to preprocess the C source and get rid of all the comments before
| > sending it to the compiler?
|
| The following works in vi: :%s/\/\*.*\*\///g
|
| I don't know if it will work in sed, but it should...
|
Yes, it will. The only problem is that it won't strip out comments
that span more than one line...'Aye, There's the Rub.
--rkl
In article <4896@cbnews.ATT.COM> smk@cbnews.ATT.COM (Stephen M. Kennedy) writes:
|In article <9900010@bradley> brian@bradley.UUCP writes:
|> The following works in vi: :%s/\/\*.*\*\///g
|
|/* And this */ important_variable = 42 /* doesn't work either! */
And how about:
puts(" A comment /* in here */");
And you can give more examples showing it isn't that trivial; a challenge
for the sed adept, perhaps ...
Leo.
In article <978@philmds.UUCP>, leo@philmds.UUCP (Leo de Wit) writes:
> In article <4896@cbnews.ATT.COM> smk@cbnews.ATT.COM (Stephen M. Kennedy) writes:
> |In article <9900010@bradley> brian@bradley.UUCP writes:
> |> The following works in vi: :%s/\/\*.*\*\///g
> |
> |/* And this */ important_variable = 42 /* doesn't work either! */
>
> And how about:
>
> puts(" A comment /* in here */");
>
> And you can give more examples showing it isn't that trivial; a challenge
> for the sed adept, perhaps ...
>
> Leo.
[And a lot of previous articles on the same topic]
The problem is: sed and vi do not understand C syntax.
Solution: write a lex program to strip comments. The program must
understand C syntax enough to know what is a comment and what is not.
Encouragement: it should not be too difficult.
--------------------------------------------------------------------
Joel Loo Peing Ling composed on Fri Mar 17 10:44:52 CST 1989
---------------------------- Now: ----------------------------------
MCC | Email: loo@sw.mcc.com
3500 West Balcones Centre Dr. | Voice: (512)338-3680 (O)
Austin, TX 78759 | (512)343-1780 (H)
In article <2131@mister-curious.sw.mcc.com>, loo@mister-curious.sw.mcc.com
(Joel Loo) writes:
> In article <978@philmds.UUCP>, leo@philmds.UUCP (Leo de Wit) writes:
> > And how about:
> > puts(" A comment /* in here */");
> > And you can give more examples showing it isn't that trivial; a challenge
> > for the sed adept, perhaps ...
> > Leo.
> [And a lot of previous articles on the same topic]
>
> The problem is: sed and vi do not understand C syntax.
>
> Solution: write a lex program to strip comments. The program must
> understand C syntax enough to know what is a comment and what is not.
>
> Encouragement: it should not be too difficult.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
It isn't. Six lines of Lex source (not counting initialization) are
enough. A Lex source for ``uncomment'' has been posted in comp.sources.unix,
as part of:
Subject: Volume 16 (Ends January 17, 1989)
identlist List identifiers and declarations for C sources
Attached is a minimum test for an uncommenting algorithm, including
tests for quotes inside and outside comments.
John Rupley
uucp: ..{uunet | ucbvax | cmcl2 | hao!ncar!noao}!arizona!rupley!local
internet: rupley!local@megaron.arizona.edu
(O) Dept. Biochemistry, Univ. Arizona, Tucson AZ 85721 - (602) 621-3929
----------------------------------------------------------------------------
/*
* tests for ``uncomment''
* assume C-code conventions:
* strings start and end on one line
* comments can be multi-line
* no tests for varieties of: '"' \'"\' etc
* no tests for strings with newline escaped
*/
string4 "hi /*\"hi there*/there\""
comment1 /*one"*/"*/
comment2 /*\"hi there"*/"*/"
comment3 /*\"hi there*/
comment4 /* hello/*hello/*hello/*hello*/
comment5 /*******/
comment6 /*/*/ a /**/ b /***/ c /****/ d /*////*/
comment7 /*/*// a /**// b /***// c /****// d /*////*//
1. /*****//"hello world */" ok /"hello world */"
2. /* hello /* /* world */ ok
3. /* */ hello /* */ ok hello
4. /**// /* this should produce "/ \n" for output */ ok /
5. /* */ hello */ ok hello */
6. /*/*/ hello ok hello
7. /*////*/ ok
8. /*//*/ ok
9. abc = "/* fake comment"; /* got who ? */ ok abc = "STRING";
10. /* "start quote
"then next line end quote, after more characters than on line 1"
more more more */ " ok "
----------------------------------------------------------------------------
In article <3145@nunki.usc.edu>, jeenglis@nunki.usc.edu (Joe English) writes:
> I made a mistake in the comment-eating program I
> posted yesterday -- it won't handle
> /* something like *//* this. */
> Change the line in the '/' case from:
> if ((ch = getchar()) == '*') { eatcomment(); ch=getchar(); }
> to:
> if ((ch = getchar()) == '*') { eatcomment(); ch=getchar(); continue; }
> and it will work. If anyone's interested.
It still doesn't work. It won't uncomment itself. Or the following line:
'"' /* hi there */ '"'
Or distinguish a correct string, with escaped newlines,
"hi\
/*\*/ /**/\
there"
from an incorrect string without the escapes.
The point is not _whether_ one can write an ``uncomment'' in C, but how,
and in what language, one can do it most simply. It is certainly right
to use C if uncommenting is part of a larger design, as in cpp or ctags.
But if the whole aim is to uncomment, then a pattern-handling language,
such as Lex, is more appropriate. A few lines of Lex source do the job,
and assuming familiarity with regular expression syntax, it is easy to
write and understand, and hard to get the logic wrong. It should be
doable with sed or awk, but probably not as easily, because they see a
file as a stream of lines rather than characters. In C, the proper
setting up of the switch and flags is not trivial, as the previous
posting witnesses.
A Lex source for uncommenting is attached (which I hope does not belie
the remark above about hard to get the logic wrong :-).
John Rupley
uucp: ..{uunet | ucbvax | cmcl2 | hao!ncar!noao}!arizona!rupley!local
internet: rupley!local@megaron.arizona.edu
--------------------------------------------------------------------
%{
/* UNCOMMENT- */
/* regexp for comment recognition based on usenet posting by: */
/* Chris Thewalt; thewalt@ritz.cive.cmu.edu */
%}
STRING \"(\\\n|\\\"|[^"\n])*\"
COMMENTBODY ([^*\n]|"*"+[^*/\n])*
COMMENTEND ([^*\n]|"*"+[^*/\n])*"*"*"*/"
QUOTECHAR \'[^\\]\'|\'\\.\'|\'\\[x0-9][0-9]*\'
ESCAPEDCHAR \\.
%START COMMENT
%%
<COMMENT>{COMMENTBODY} ;
<COMMENT>{COMMENTEND} BEGIN 0;
<COMMENT>.|\n ;
"/*" BEGIN COMMENT;
{STRING} ECHO;
{QUOTECHAR} ECHO;
{ESCAPEDCHAR} ECHO;
.|\n ECHO;
---------------------------------------------------------------------------
I made a mistake in the comment-eating program I
posted yesterday -- it won't handle
/* something like *//* this. */
Change the line in the '/' case from:
if ((ch = getchar()) == '*') { eatcomment(); ch=getchar(); }
to:
if ((ch = getchar()) == '*') { eatcomment(); ch=getchar(); continue; }
and it will work. If anyone's interested.
--Joe English
jeenglis@nunki.usc.edu
I missed the original posting, so I didn't catch the exact question,
but when I have needed to remove comments, I have simply passed the
source through the preprocessor stage of the compiler only. Granted,
this does a lot of other things, which may or may not be undesirable
for your application. Some compilers, however, have options on
the preprocessor which can limit the scope of the damage.
If the original poster still hasn't solved his problem, he can contact
me. I think we also have a quick and dirty C program which someone wrote
which does the job.
-----------------------------------------------------------------------------
| David C. Brown | uunet!mnetor!motto!dave |
| Motorola Canada, Ltd. | 416-499-1441 ext 3708 |
| Communications Division | Disclaimer: Motorola is a very big company |
-----------------------------------------------------------------------------
Next 10
Jump to [ End of thread ]
©2004 Google