Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!unmvax!pprg.unm.edu!hc!lll-winken!uunet!mcvax!kth!osiris!uplog!lynx!pem From: pem@zyx.SE (Per-Erik Martin) Newsgroups: comp.lang.c Subject: Re: Want a way to strip comments from a Message-ID: <852@lynx.zyx.SE> Date: 21 Mar 89 17:06:39 GMT References: <7150@siemens.UUCP> <9900010@bradley> <4896@cbnews.ATT.COM> <978@philmds.UUCP> <3114@nunki.usc.edu> <983@philmds.UUCP> Reply-To: pem@spunk.zyx.SE (Per-Erik Martin) Organization: ZYX Sweden AB, Stockholm, Sweden Lines: 91 In article <983@philmds.UUCP> leo@philmds.UUCP (Leo de Wit) writes: >In article <3114@nunki.usc.edu> jeenglis@nunki.usc.edu (Joe English) writes: >| >|Does it *have* to be done in sed/awk/other text processor? >|This problem is fairly difficult to solve using regexp/editor >|commands, but it's a piece of cake to do in C: > >Piece of cake? Your program can't even strip its own comments (try it)! Here's another example in C. It *is* a piece of cake (15 minutes work). The problem can be described with a simple automata which is easily coded in in C (with goto's, >yech<). I've tested it on most of the pathological examples given in this group and it seems to work. ---------------------------------------------------------------------------- /* cstrip.c pem@zyx.SE, 1989 */ #include main() { char c, c1; goto into_code; in_code: putchar(c); into_code: switch (c = (char)getchar()) { case EOF: exit(0); case '\'': goto in_char; case '"': goto in_string; case '/': c1 = c; if ((c = (char)getchar()) == '*') goto in_comment; putchar(c1); default: goto in_code; } in_char: putchar(c); switch (c = (char)getchar()) { case EOF: exit(1); case '\\': putchar(c); c = (char)getchar(); default: putchar(c); while ((c = (char)getchar()) != '\'') putchar(c); goto in_code; } in_string: putchar(c); switch (c = (char)getchar()) { case EOF: exit(1); case '"': goto in_code; case '\\': putchar(c); c = (char)getchar(); default: goto in_string; } in_comment: switch (c = (char)getchar()) { case EOF: exit(1); case '*': if ((c = (char)getchar()) == '/') goto into_code; default: goto in_comment; } } ---------------------------------------------------------------------------- -- ------------------------------------------------------------------------------- - Per-Erik Martin, ZYX Sweden AB, Bangardsgatan 13, S-753 20 Uppsala, Sweden - - Email: pem@zyx.SE - -------------------------------------------------------------------------------