Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming > #1009

Re: indent: convert to C-style comments

From jt@toerring.de (Jens Thoms Toerring)
Newsgroups comp.programming, comp.unix.programmer
Subject Re: indent: convert to C-style comments
Date 2011-11-09 14:29 +0000
Organization Freie Universitaet Berlin
Message-ID <9hvh24F8d8U1@mid.uni-berlin.de> (permalink)
References <j8ut1o$psd$1@speranza.aioe.org> <9hhhbcF62fU1@mid.individual.net> <j944ln$ad8$1@speranza.aioe.org> <j97a9a$1lu$1@speranza.aioe.org>

Cross-posted to 2 groups.

Show all headers | View raw


In comp.unix.programmer Thad Smith <ThadSmith@acm.org> 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 <stdio.h>
%}

%x str
%x c_cmt
%x cc_cmt

%%

\"        { putchar( '"' ); yy_push_state( str ); }
<str>{
\\\"      fputs( yytext, stdout );
\"        { putchar( '"' ); yy_pop_state( ); }
.         fputs( yytext, stdout );
}

"/*"      { fputs( yytext, stdout ); yy_push_state( c_cmt ); }
<c_cmt>{
"*/"      { fputs( "*/", stdout ); yy_pop_state( ); }
.         fputs( yytext, stdout );
}

"//"      { fputs( "/*", stdout ); yy_push_state( cc_cmt ); }
<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

Back to comp.programming | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

indent: convert to C-style comments "Mark" <mark_cruzNOTFORSPAM@hotmail.com> - 2011-11-03 16:20 -0400
  Re: indent: convert to C-style comments Ian Collins <ian-news@hotmail.com> - 2011-11-04 20:08 +1300
    Re: indent: convert to C-style comments scott@slp53.sl.home (Scott Lurndal) - 2011-11-04 16:46 +0000
    Re: indent: convert to C-style comments "Mark" <mark_cruzNOTFORSPAM@hotmail.com> - 2011-11-05 16:00 -0500
      Re: indent: convert to C-style comments Ian Collins <ian-news@hotmail.com> - 2011-11-06 09:05 +1300
        Re: indent: convert to C-style comments "Mark" <mark_cruzNOTFORSPAM@hotmail.com> - 2011-11-05 16:12 -0500
      Re: indent: convert to C-style comments Thad Smith <ThadSmith@acm.org> - 2011-11-06 18:54 -0600
        Re: indent: convert to C-style comments jt@toerring.de (Jens Thoms Toerring) - 2011-11-09 14:29 +0000
          Re: indent: convert to C-style comments John Gordon <gordon@panix.com> - 2011-11-09 17:07 +0000
            Re: indent: convert to C-style comments jt@toerring.de (Jens Thoms Toerring) - 2011-11-09 18:36 +0000
              Re: indent: convert to C-style comments John Gordon <gordon@panix.com> - 2011-11-10 20:08 +0000
                Re: indent: convert to C-style comments Doug McIntyre <merlyn@geeks.org> - 2011-11-10 20:21 +0000
                Re: indent: convert to C-style comments jt@toerring.de (Jens Thoms Toerring) - 2011-11-10 21:04 +0000
          Re: indent: convert to C-style comments Thad Smith <ThadSmith@acm.org> - 2011-11-10 20:47 -0700

csiph-web