Thread: preprocessor program

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    14

    preprocessor program

    Code:
    //a program which works as a preprocess removes comments in a given program
    
    #include<stdio.h>
    main()
    {
    int i,start,end;
    char array[500],c;
            for(i=0;(c=getchar())!=EOF;i++)
            array[i]=c;
            for(i=0;array[i]!=EOF;i++)
            {
                    if( (array[i]=='/') && (array[i+1]=='*') )
                            start=i;
                    if( (array[i]=='*') && array[i+1]=='/')
                    {
                    end=i+1;
                    for(i=start;i<=end;i++)
                    array[i]='\b';
                    }
            }
            //for(i=start;i<=end;i++)
            //      array[i]='\0';
            printf("after comments removal the text is as below\n");
            for(i=0;array[i]!=EOF;i++)
                    putchar(array[i]);
            printf("\n");
    }

    here i have created a demo code which works similar to a pre processor which removes the comments in the given program but this is yielding nasty results debug my program please
    INPUT to program:

    Code:
    /*comment1*/
    statement1;
    /*comment2*/
    
    statement2;
    /*comment3*/
    statement3;
    OUTPUT:
    Code:
    after comments removal the text is as below
    
    statement1;
    
    
    statement2;
    
    statement3;�q*Дq�$Дq�@(u�q@
    im supposed to remove the comments BUT THE BLANK SPACES REMAIN HOW TO REMOVE THEM???
    also i get unwanted character at the end ,what changes can i do to my program

    moreover here i want to give a .c file as input to program how could i do that in linux
    can i try code.c | ./program
    Last edited by shyam.sunder91; 03-12-2012 at 11:44 PM.

  2. #2
    Registered User
    Join Date
    Jun 2010
    Location
    In a house
    Posts
    15
    For blank line: also remove the '\n' for the comment line.

    Input to program: check out argument for the main().

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    14

    i did not gent you

    Quote Originally Posted by tripleA View Post
    For blank line: also remove the '\n' for the comment line.

    Input to program: check out argument for the main().
    my question is
    we have successfully removed the comments but the characters remain in their places the spaces are not removed how to remove spaces and make the program spaces free output

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One idea is to recognise two states: within a comment and not in a comment. So, while the state is "within a comment", you simply discard every character until you encounter a "*", upon which you do a lookahead for "/". If the next character is indeed a "/", you change the state (if there is no next character, then the content is invalid). While the state is "not in a comment", you store the character in the array, except that when you encounter a "/", you do a lookahead and only store the character if the next character is not a "*". If it is, you discard both characters and change the state, otherwise you store both characters (or just "/", if that is the last character).

    Now, since you read the entire content into an array first, "discarding" a character is a matter of maintaining two pointers: a read pointer and a store pointer. "Discard" means advancing the read pointer without advancing the store pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by shyam.sunder91 View Post
    also i get unwanted character at the end
    You should always, always initialize your variables and then this problem should go away.

    And while I'm at it, you should indent properly, always use braces with if/for/while/do blocks, you should have an int main and return from it and use a little more whitespace to make your program easier to read.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    14

    i wil go with this idea

    Quote Originally Posted by laserlight View Post
    One idea is to recognise two states: within a comment and not in a comment. So, while the state is "within a comment", you simply discard every character until you encounter a "*", upon which you do a lookahead for "/". If the next character is indeed a "/", you change the state (if there is no next character, then the content is invalid). While the state is "not in a comment", you store the character in the array, except that when you encounter a "/", you do a lookahead and only store the character if the next character is not a "*". If it is, you discard both characters and change the state, otherwise you store both characters (or just "/", if that is the last character).

    Now, since you read the entire content into an array first, "discarding" a character is a matter of maintaining two pointers: a read pointer and a store pointer. "Discard" means advancing the read pointer without advancing the store pointer.
    laser light your idea was good looking to code it soon il post that

  7. #7
    Registered User
    Join Date
    May 2011
    Posts
    14

    The Results were some what weird

    Code:
    #include<stdio.h>
    main()
    {
    
    int i;
    
    static char array[500],c;
    
    for(i=0;(c=getchar())!=EOF;i++)
    {
            if(c=='/')
            {
    
                    if((c=getchar())=='*')
                    {
    
                            while(!(( c=getchar()=='*') && (c=getchar()=='/')));
    
    
                    }
    
            }
    
    array[i]=c;
    }
    
    printf("\nNow the code after removing commnets will be like this\n");
    
    for(i=0;(putchar(array[i]))!=EOF;i++);
    }
    Input and Output Scree Shot

    preprocessor program-screenshot-2012-03-14-21-12-51-png

    buts this not what i expected the code should ignore comments
    output expected
    Code:
    statement;
    statement2;
    statement3;

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    In your while loop you need to also check for '\n' as well as you are not needing those carriage returns.
    Code:
    ...
    while(!(( c=getchar()=='*') && (c=getchar()=='/') && (c=getchar()=='\n')));
    ...
    Also since you have hard set your array to 500, you do not necessarily need to check for EOF for your putchar, in testing, as you saw, I got a Seg fault with that in place. Try this:

    Code:
    ...
    for(i=0;(putchar(array[i]));i++);
    ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. preprocessor
    By pin2 in forum C Programming
    Replies: 5
    Last Post: 12-24-2009, 12:55 AM
  2. Preprocessor help
    By royen in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2009, 10:56 AM
  3. preprocessor help
    By brunogon in forum C Programming
    Replies: 13
    Last Post: 06-16-2008, 11:14 AM
  4. preprocessor!!
    By nishu1988 in forum C++ Programming
    Replies: 6
    Last Post: 07-24-2007, 11:31 PM
  5. What is Preprocessor...?
    By gardenair in forum C Programming
    Replies: 2
    Last Post: 04-09-2003, 07:52 PM