Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,228 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,935 people online right now. Registration is fast and FREE... Join Now!




comment detector

 
Reply to this topicStart new topic

comment detector

cire_1803
18 Mar, 2007 - 04:32 PM
Post #1

New D.I.C Head
*

Joined: 20 Sep, 2006
Posts: 43


My Contributions
Can anyone here revised my fucntion.
The job of the function is to read from a file as well as detect comment such as "/*statement*/" and "//Statement.
If the function finds a comment it will skip the whole statement inside the comment premises. if not, enqueue into the queue which is in the function parameters.
"int xprs" is a counter how many expression inside the file. The terminator of each expression is semi-colon ';'.
The expression inside the comment is not counted.
if the expression inside the file are all inside the comment it will report an error.


CODE
int read_file(char filename[], QUEUE Q1, int xprs) {
    int eflag = 0,ctr=0;
    char temp,cont[2];
    FILE *fp = fopen(filename,"rb");
    if ( !fp ) {
        perror("ERROR: ");
        return 0;
    }
    while ( !feof(fp) ) {
        temp = fgetc(fp);
        if ( !feof(fp) ) {
            /*empty file*/
            if ( (temp == ';') && (ctr == 0) ) {
                eflag = 1;
                break;
            }
            if ( temp == ';' )
                xprs = xprs+1;
            if ( temp == '/' ) {
                cont[0] = temp;
                temp = fgetc(fp);
                cont[1] = temp;
                if ( (cont[0] == '/') && (cont[1] == '/' ) ) {
                    while ( temp != ';' )
                        temp = fgetc(fp);
                }
                else if ( temp == '*' ) {
                    while ( temp == ';' )
                        temp = fgetc(fp);
                }
            }
            else enQueue(temp,Q1);
        }
        ctr = ctr+1;
    }
    if ( eflag == 1) ) {
        printf("\n\nNo expression in the file.");
        getch();
    }
    return 1;
}


Thanks and God Bless
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Comment Detector
20 Mar, 2007 - 08:15 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,859



Thanked: 50 times
Dream Kudos: 550
My Contributions
Sorry that I don't have time to take apart your code at the moment. I can tell you that ussualy a nice Finite state machine is used to do this kind of thing. Your program really is a FSM. Basicly the idea is to define certain "states" in which your program will operate. This can be done using a table such as LEX/YACC might define, or with a state varable and a big switch or if-else-if section of code, or as you have done above just keeping track of what is happening where in your code.

Basicly you will enter a state that I would call "eating comment" where the program just takes a brief look at the next character too see if it is either then end-of-line or an end-of-comment... if either of these are found it changes states...

In the snippet section I posted a FSM which reads in integers. This is an example of an explicitly constructed FSM using the if-else-if method.
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Comment Detector
20 Mar, 2007 - 10:31 PM
Post #3

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
You are searching for the '/*' character combination only.
Although it will work, don't assume that the original programmer has already included the '*/' character as well.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 02:52PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month