Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!not-for-mail From: jt@toerring.de (Jens Thoms Toerring) Newsgroups: comp.programming,comp.unix.programmer Subject: Re: indent: convert to C-style comments Date: 9 Nov 2011 14:29:24 GMT Organization: Freie Universitaet Berlin Lines: 81 Message-ID: <9hvh24F8d8U1@mid.uni-berlin.de> References: <9hhhbcF62fU1@mid.individual.net> X-Trace: news.uni-berlin.de KtAjvNFQXFe/sVhwVx/6SQmP+rM/+ZRxRxTLq5rhpAMIzs X-Orig-Path: not-for-mail User-Agent: tin/1.9.3-20080506 ("Dalintober") (UNIX) (Linux/2.6.30-1-amd64 (x86_64)) Xref: x330-a1.tempe.blueboxinc.net comp.programming:1009 comp.unix.programmer:1570 In comp.unix.programmer Thad Smith wrote: > On 11/5/2011 3:00 PM, Mark wrote: > >>> I've read through 'man indent' but have not found if it allows to enforce > >>> C-style comments. Am I missing any option using which would convert C++ > >>> comments in to C-style? > It's a nice programming exercise to convert the comments. Remember to > disable // searches in string constants and character constants. > For solid code, you > need to also process trigraphs and line continuations. How could "//" appear in a character constant? > That said, there must be such a program out there somewhere. I must have > written one, but can't find it. :-( Here's a small filter program, using flex: ------8<-------------------------------------------- %option noyywrap noinput stack nounput noyy_top_state %{ #include %} %x str %x c_cmt %x cc_cmt %% \" { putchar( '"' ); yy_push_state( str ); } { \\\" fputs( yytext, stdout ); \" { putchar( '"' ); yy_pop_state( ); } . fputs( yytext, stdout ); } "/*" { fputs( yytext, stdout ); yy_push_state( c_cmt ); } { "*/" { fputs( "*/", stdout ); yy_pop_state( ); } . fputs( yytext, stdout ); } "//" { fputs( "/*", stdout ); yy_push_state( cc_cmt ); } { \n { puts( " */" ); yy_pop_state( ); } . fputs( yytext, stdout ); } . fputs( yytext, stdout ); %% int main( void ) { yyin = stdin; yylex( ); return 0; } ------8<-------------------------------------------- Store it as ccmt.l, then use flex to convert it to a C program with e.g. flex -o ccmt.c ccmt.l then compile it. Pipe the file you want to convert to it and the converted data go to stdout. Dealing with trigraphs is left as an exercise to the reader;-) Since I'm not sure how contenuation lines work with C++-comments I left that out but that shouldn't be too difficult to add support for that. And, of course, there are no guarantees that it will work as ad- vertised;-) Regards, Jens -- \ Jens Thoms Toerring ___ jt@toerring.de \__________________________ http://toerring.de