Thread: Deleting comments C

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    Deleting comments C

    I do not really know how to continue with this, could someone help?
    Comments are treated as whitespace. Each comment is replaced by one space character, or by several newline characters, depending on how the comment was written. Note that the interior of character constants (like 'a') and string literals (like "a") do not contain comments, so this pass is more complex than the first three. You need to do some elements of tokenization here, but only to recognize the beginning and end of character constants, string literals, and the two forms of comments.

    The string literal "/* xyz */" looks like it contains a comment, but it doesn't, so don't change the string. It goes to the output of this pass as-is.

    The two forms of comment are
    /* extending to the first */
    // extending to the end of line

    The /**/ form can extend across source code lines. It should be replaced with one space character if all on one line, or with one or more newline characters if on multiple lines (as a partial attempt to preserve the original number of lines).

    Comments are not recognized within other comments. The first example (a nested comment) is illegal in Standard C and the second is one comment not two, The third example is not terminated (it might be terminated on a later line).
    /* /* something */ */
    // /* something */
    /* // something
    Code:
    # include <stdio.h>
    
    int main(){
    
    int c0, c1;
    bool notString = false;
    c0 = getc(stdin); c1 = getc(stdin), c2 = getc(stdin);
    while (c0 != EOF) {
    int shift = 1;
      if(c0 == '"'){
        input(c0);}
      else if (c0 == '/' && c1 == '*'){
            c0 = '\n';
            shift = 2;
            printf("%c",c0);}
      else if (c0 == '/' && c1 == '/'){
            c0 = ' ';
            shift = 2;
            printf("%c",c0);}
      
      if (shift == 1)
        { c0 = c1; c1 = getc(stdin); }
      else /* shift == 2 */
        { c0 = getc(stdin); c1 = getc(stdin); }
    }
    return 0;}
    
    void input (char c)
    {
      do {
        c = getchar();
        putchar (c);
      } while (c != '"');
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You have at least one problem with your current implementation. Before you start your loop, you read 3 characters into c0, c1 and c2. Then, when you shift, you never shift c2 into c1 or c0 before calling getc(stdin), so you effectively throw away the third character in your input stream.

    You should also be consistent in your usage of getc/getchar, use one or the other but not both. That's just a style issue, but clear, consistent code is easier to write, read and find bugs in.

    Frankly, you shouldn't start coding until you fully understand the problem and you have a solution planned on paper. For a fairly simple tokenizer/parser like this, I would first work out a state machine on paper. Run through some simple examples on paper to make sure your design is good. Then you code in small chunks and test as you go. Consider each type of "token" as it's own mini state machine, so it's modular and easy to test. One for string literals, one for character literals, one for line comments and one for block comments.

    Write separate functions for processing the different kinds of tokens. When you see the start of a string literal, call process_string_literal, which will read and echoes characters until it finds an unescaped ". Do the same for character literals (unescaped '), line comments (read until end of line then echo a space) and block comments (read until */, echoing a new line for each one in the comment block). It will be easy to test each of those functions individually, and then it's simply a matter of reading input and deciding when to call which one.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    I am having a problem printing the next characters, from " to the end of '. In the if statment, how would I print the next chars? I know it is simple but for some reason I can't figure that out.
    int c0, c1;

    bool notString = false;
    c0 = getc(stdin); c1 = getc(stdin), c2 = getc(stdin);
    while (c0 != EOF) {
    int shift = 1;

    if(c0 == '"'){

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The character " has to be escaped properly if you want to treat it like an actual character:
    Code:
    if( c0 == '\"' )
    I'm not sure why you chose to read three characters at a time here, but I wouldn't.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    does this one looks better

    Code:
    # include <stdio.h>
     
    int main(){
     
    int c0, c1;
    bool notString = false;
    c0 = getc(stdin); c1 = getc(stdin);
    while (c0 != EOF) {
    int shift = 1;
      if(c0 == '\"'){
        input(stdin,'"');}
      
      else if (c0 == '/' && c1 == '/'){
            c0 = ' ';
            shift = 2;
            printf("%c",c0);}
       
      if (shift == 1)
        { c0 = c1; c1 = getc(stdin); }
      else /* shift == 2 */
        { c0 = getc(stdin); c1 = getc(stdin); }
    }
    return 0;}
     
    void input (int g,char i)
    {
      int c;
      do {
        c = getc(g);
        putc (c);
      } while (c != i);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deleting comments in another program
    By chrissy2860 in forum C Programming
    Replies: 4
    Last Post: 02-03-2012, 04:15 PM
  2. Creating and deleting directories and deleting files
    By fguy817817 in forum C Programming
    Replies: 1
    Last Post: 04-08-2009, 07:26 AM
  3. Need some comments
    By shaizy in forum C Programming
    Replies: 0
    Last Post: 05-26-2006, 01:59 AM
  4. comments
    By nightingale in forum C Programming
    Replies: 32
    Last Post: 07-18-2003, 03:49 AM
  5. need help deleting a deleting a struct from file
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 05-20-2002, 05:38 AM

Tags for this Thread