Line counting C++

前端 未结 1 888
星月不相逢
星月不相逢 2021-01-29 01:35

I\'m working with this peace of that count the logical lines in a program, omitting comments and black lines. The counting line is working, but I don\'t know how to omit the com

相关标签:
1条回答
  • 2021-01-29 02:40

    I think your methodology is...flawed.

    Consider a line like:

    int x = 1;    // starting from 1 because [some reason]
    

    As your code stands right now, it counts only as a non-comment line. As you've described what you'd like to do, it would count only as a comment line.

    In reality, tThis contains both code and a comment, so you'd normally want to count it as both code and comment, not just one or the other.

    Doing this job well is decidedly non-trivial. Obvious problems you encounter are:

    1. a string that contains something that looks like a comment
    2. line continuation
    3. trigraphs
      • Can hide line continuation
      • Can create a false comment delimiter
    4. Multi-line C-ctyle comments
    5. #ifs, #ifdefs, etc.

    There are probably more issues than that (though are just what occurred to me immediately), but those should be enough to give at least a general flavor.

    Bottom line: I think to get very far with this (at all) you're doing to at least need a reasonably complete/accurate C++ lexer. You probably don't need a full parser, but I think any attempt that doesn't use a full C++ lexer s almost certain to fail, probably quite badly and quite frequently.

    0 讨论(0)
提交回复
热议问题