Want a way to strip comments from a

Per-Erik Martin pem at zyx.SE
Wed Mar 22 03:06:39 AEST 1989


In article <983 at philmds.UUCP> leo at philmds.UUCP (Leo de Wit) writes:
>In article <3114 at nunki.usc.edu> jeenglis at 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 at zyx.SE, 1989 */

#include <stdio.h>

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 at zyx.SE                                                           -
-------------------------------------------------------------------------------



More information about the Comp.lang.c mailing list