request for C comment stripper

Ian Cottam ian at ux.cs.man.ac.uk
Mon Mar 20 03:12:54 AEST 1989


I usually use a lex script for such things.  I didn't have one for
C, but the following might do the trick.  N.B.  Not tested, not proven,
no warranty!
________________________________________________________________________
%{
  /***** Lex script to strip comments from C texts ******/
%}
%s COMMENT STRING CHAR
%%
<INITIAL>\'	        {BEGIN CHAR;   ECHO;}
<INITIAL>\"	        {BEGIN STRING; ECHO;}
<INITIAL>"/*"		BEGIN COMMENT;
<INITIAL>.		ECHO;
<INITIAL>\n		ECHO;
<CHAR>\\'               ECHO;
<CHAR>\'                {ECHO; BEGIN INITIAL;}
<STRING>\\\"            ECHO;
<STRING>\"              {ECHO; BEGIN INITIAL;}
<COMMENT>"*/"		BEGIN INITIAL;
<COMMENT>.		;
<COMMENT>\n		;
%%
-----------------------------------------------------------------
Ian Cottam, Room IT101, Department of Computer Science,
University of Manchester, Oxford Road, Manchester, M13 9PL, U.K.
Tel: (+44) 61-275 6157         FAX: (+44) 61-275-6280
ARPA: ian%ux.cs.man.ac.uk at nss.cs.ucl.ac.uk   
JANET: ian at uk.ac.man.cs.ux    UUCP: ..!mcvax!ukc!mur7!ian
-----------------------------------------------------------------



More information about the Comp.lang.c mailing list