| Messages 31-35 from thread "Want a way to strip comments from a" |
Prev 10
Jump to [ Start of thread ]
In article <893@m10ux.UUCP>, mnc@m10ux.UUCP (Michael Condict) writes:
> Oops, the previous lex script I posted for deleting comments from
> C source code is incorrect -- it doesn't recognize: /***...**/
> Here is a better one (simpler, too):
>
> %%
> \"([^\\"]*\\(.|\n))*[^\\"]*\" ECHO;
> "/*"([^*]|"*"+[^/*])*"*"*"*/" ;
> . ECHO;
You indeed fixed the /***/ error, but two errors remain.
First, no handling of single-quoted double quotes:
main() {printf("%c\n", '"');/*gotcha*/printf("%c\n", '"');}
Second, your program crashes when uncommenting a real source file, with
a sizeable change history or whatever inside a comment. You need at
least one state change, so a comment can be matched line-by-line, and
so not overflow a Lex buffer. Both previous Lex postings did it
right. A third state, to handle quoted strings line-by-line, is perhaps
optional, and the previous postings differ here. Apparently you missed
the previous Lex postings, which I will be happy to email you on request.
My argument, that it's difficult to make a logical error in coding this
problem in Lex, has now been demonstrated wrong (sob :-). But at least
Lex is still outscoring straight C (faint praise :-?).
John Rupley
rupley!local@megaron.arizona.edu
Why is everyone obsessed with stripping comments from their C programs.
Is this some new programming trend? :)
Dan Wilson
In article <795@twwells.uucp>, bill@twwells.uucp (T. William Wells) writes:
> In article <9797@megaron.arizona.edu> rupley@arizona.edu (John Rupley) writes:
> : A Lex source for uncommenting is attached (which I hope does not belie
> : the remark above about hard to get the logic wrong :-).
>
> Try it on a very long comment. You might discover an overflowed lex
> buffer. On the other hand, this shouldn't be too hard to fix. Just do
> for the comment what you did for the noncommented text.
Nope.... no problem.... comments are thrown away line-by-line, by design,
so that very long comments indeed do not blow the buffer.
A very long string, however, will overflow the buffer, but clearly this
is understood, and it can be viewed as a feature, although
idiosyncratic, as noted in <9888@megaron.arizona.edu>. If you want to
handle strings differently, add another start condition (state) begun
by '"' and make explicit start condition 0 = <INITIAL>, or change the
size of the match buffer (yytext[]) by including in the definitions:
%{
#define YYLMAX 5000 /* or whatever */
%}
John Rupley
rupley!local@megaron.arizona.edu
In article <4895@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
>
>/*
> * Unfortunately, multi-line comments aren't deleted.
> */
What about the following command in vi: :%s/\/\*[.|\n]*\*\///g
This will work for multi-line comments.
Gertjan Stil
<no signature yet>
In article <9887@megaron.arizona.edu>, rupley@arizona.edu (John Rupley) writes:
>
> > In article <620@gonzo.UUCP>, daveb@gonzo.UUCP (Dave Brower) writes:
> > So, I offer this week's challenge: Smallest program that will take
> > "blank line" style cpp output on stdin and send to stdout a scrunched
> > version with appropriate #line directives. [f]lex, Yacc, [na]awk, sed,
> > perl, c, c++ are all acceptable. This will be an amusing excercise in
> > typical text massaging that can be enlightening for many people.
>
> "Scrunching" is probably a matter of taste, with regard to the format
> of the ouput.
I don't know what is ment by the term scrunching, but here is my entry to
the problem of removing comments in a C program. YACCR (Yet Another C
Comment Remover :-) is a crazy looking lex specification that removes C
comments from a source file. It also does not put out a lot of extra blank
lines that cpp does. I have tested on most styles of C comments that I
have seen and it seems to work, but PLEASE no flames if it doesn't!!!!
In an earlier message, someone address the problem of a yytext overflow.
YACCR redefines the YYLMAX constant as 500, but you can test it with other
values.
To use:
1. Save message in file called yaccr.l and edit this file to
unwanted text.
2. Type: lex yaccr.l
3. Type: cc lex.yy.c -ll -lyaccr
It should then be ready to go.
Good luck.
---mike
EMAIL: ...!gatech!auc!rambro!maw
--------------------------------cut here--------------------------
%{
/*
** Specification: YACCR
** Description : YACCR removes comments from C programs.
*/
#define CR 0x0d
#ifdef YYLMAX
#undef YYLMAX
#define YYLMAX 500
#endif
%}
%%
"/*""*"*("/*"*|[^*/]|[^*]"/"|"*"[^/])*"*"*"*/" putchar(CR);
. printf("%s",yytext);
--------------------------------cut here--------------------------
Prev 10
Jump to [ Start of thread ]
©2004 Google