This is an edited, corrected, more specific version of a previous question of mine. So I'm doing a homework assignment where we have to use a sliding window to print the comments and strings from an input file coming from the stdin. I am very close but something is missing. Following is my code, an input, my current output, and the correct output. The //
comments and ignoring characters work. I'm not exactly sure the problem with strings and in /**/ comments I can't get rid of the the star in the initial /*
without screwing up it finding the */
. Thank you anyone who can offer assistance.
Code:
#include <stdio.h>
typedef enum
{
Initial,
Comment,
String,
Char,
CPPComment
} extrema;
int main()
{
int c, c1 = 0, c2 = 0;
extrema state = Initial;
extrema next = Initial;
while(1)
{
switch(state)
{
case Initial: next = ((c2 == '*' && c1 == '/') ? Comment : (c2 == '\"') ? String : (c2 == '/' && c1 == '/') ? Char : (c2 == '\'') ? CPPComment : Initial); break;
case Comment: next = ((c2 == '/' && c1 == '*') ? Initial : Comment); break;
case String: next = ((c2 == '\"' && c1 != '\\') ? Initial : String); break;
case Char: next = ((c2 == '\n') ? Initial : Char); break;
case CPPComment: next = ((c2 == '\'' && c1 != '\\') ? Initial : CPPComment); break;
default: next = state;
}
if(state == Comment)
{
if(c1 == '*')
{
if(c2 != '/')
putchar(c1);
else
putchar('\n');
}
else
putchar(c1);
}
else if(state == String)
{
if(c2 != '\"' || (c2 == '\"' && c1 != '\\'))
putchar(c2);
}
else if(state == CPPComment)
{
putchar(c2);
}
c = getchar(); if( c < 0) break;
c1 = c2; c2 = c; // slide window
//printf("%i",state);
state = next;
// c2 is the current input byte and c1 is the previous input byte
}
return 0;
}
Input:
/* recognize '...' otherwise see " as start of string: */
int c='\"', d='\'', e = '\012'; // comment line 3
/* recognize "..." otherwise see comments here: */
char s[] = "abc/*not a comment*/efg\"ZZ\'";
char t[] = "ABC//not a comment//EFG\x012\/\/";
char *p = ""; //
int d = '/*'; // comment line 13
/*/*/
/**/
/*Z*/
/***/
/****/
/**A**/
My Output:
* recognize '...' otherwise see " as start of string:
comment line 3
* recognize "..." otherwise see comments here:
abc/*not a comment*/efg\ZZ\'"ABC//not a comment//EFG\x012\/\/""
comment line 13
*
*Z
**
***
**A*
Correct Output:
recognize '...' otherwise see " as start of string:
comment line 3
recognize "..." otherwise see comments here:
abc/*not a comment*/efg\"ZZ\'
ABC//not a comment//EFG\x012\/\/
comment line 13
/
Z
*
**
*A*