C programming language comments translator

ccomment.c translates C++ programming language comments (//..., which are sometimes used as a non-standard extension in C source code) to pure C comments (/*...*/). Care has been taken of: strings, character literals, constructs like '//.../*...*/' and performance.

ccomment.c uses a simple, compact C/C++ programming language parser, which is implemented in C (not lex or yacc) as a state engine (10 * 7 matrix and accessor function). The focus was to keep it compact.

10 is the number of states (see diagram below; 'literal' meaning character-literal and 'string' meaning string-literal; 'code' is the start state); 7 is the alphabet size (/ * \ " ' newline and rest; untagged transitions meaning the 'else' case (rest and unhandled cases)).

Known limitations: Trigraphs are not handled. [That means ??/ is not recognized as the escape character \, ??' is misinterpreted as quote, etc.]

The simple (C++ comment able) C source code parser used by ccomment.c:

          +-------------------+
          |  cppcommentquote  |
          +-------------------+
                         ^  |
            +----+   '\\'|  |           +----+
            |    v       |  v           |    v
          +-------------------+ '\n'  +-------------------+
          |    cppcomment     |--+    |      comment      |
          +-------------------+  |    +-------------------+
                         ^       |      ^            ^  |
                      '/'|  +---------- +    +----+  |  |'*'
                         |  |'*' |        '*'|    v  |  v
          +-------------------+  |    +-------------------+
          |    precomment     |  |    |    postcomment    |
          +-------------------+  |    +-------------------+
               |  |       ^  |   |        |
           '\''|  |'"' '/'|  |   | +----+ |'/'
               |  |       |  v   v |    v v
               |  |      +-------------------+
               |  |      |       code        |
               |  |      +-------------------+
               |  |       ^  |'\''     ^  |'"'
               |  +------ |  | ------- |  | --+
               v      '\''|  v      '"'|  v   v
          +-------------------+       +-------------------+
          |      literal      |       |      string       |
          +-------------------+       +-------------------+
            ^    |        ^  |         ^  |      ^    |
            +----+        |  |'\\'     |  |'\\'  +----+
                          |  v         |  v
          +-------------------+       +-------------------+
          |   literalquote    |       |    stringquote    |
          +-------------------+       +-------------------+

Keywords: C comments, C++ comments, comment, change, convert, translate, comment conversion, comment translation, source code, sources, source


lr / Mon Jan 27 1997