Exercise 1-23 (nocomment)

Chapter_1     Exercise_1-22 Exercise_1-24







Exercise 1-23     K&R, p. 34


Exercise 1-23. Write a program to remove all comments from a C program. Don't forget to handle quoted strings and character constants properly. C comments do not nest.




nocomment.c         download


#include <stdio.h> // for getchar(), putchar(), printf(), EOF

#define FALSE 0
#define TRUE 1

// testing a \
multiline \
C++ style \
comment

int main()
{
int c1, c2;
int insideComment = FALSE; // for C style comments

while ((c1 = getchar()) != EOF)
{
if (c1 == '\\') // escape character or sequence
{ /* \', \", \\ */ // \ continues a C++ style comment on the next line
c2 = getchar();
if (c2 == EOF)
{ // \ newline is part of a multiline preprocessor command or C++ comment
printf("\nEscape character not ended properly\n"); // Error message
return 1; // return from main(), end program with an error message
}
putchar(c1); putchar(c2);
}
else if (c1 == '\'') // character literal
{ // '/', '*', '"', '\"'
putchar(c1);
while ((c1 = getchar()) != '\'')
{
if (c1 == EOF || c1 == '\n')
{
printf("\nCharacter literal not ended properly\n"); // Error message
return 1; // end program with an error message
}
else if (c1 == '\\') // escape char or sequence
{ // '\\', '"', '\"'
putchar(c1);
c1 = getchar();
if (c1 == EOF)
{ // \ newline is part of a multiline preprocessor command or C++ comment
printf("\nEscape character not ended properly\n"); // Error message
return 1; // end program with an error message
}
else {putchar(c1);} // skip char
}
else {putchar(c1);}
} // here c1 == '\''
}
else if (c1 == '\"') // strings are on a single line
{ // "/*", "*/", "//", "'", "\"", "\'", "'", "\\"
putchar(c1);
while ((c1 = getchar()) != '\"')
{
if (c1 == EOF || c1 == '\n')
{
printf("\nQuoted string not ended properly\n"); // Error message
return 1; // end program with an error message
}
else if (c1 == '\\') // escape char or sequence
{
putchar(c1);
c1 = getchar();
if (c1 == EOF || c1 == '\n')
{
printf("\nEscape character not ended properly\n"); // Error message
return 1; // end program with an error message
}
else {putchar(c1);}
}
else {putchar(c1);}
} // here c1 == '\"'
}
if (c1 == '/')
{
c2 = /* testing */ getchar();
if (c2 == '/') // beginning of a C++ style comment
{
while ((c2 = getchar()) != EOF)
{ // \ continues a C++ style comment on the next line
if (c1 != '\\' && c2 == '\n')
{break;} // out of inner while()
c1 = c2; // c1 holds the penultimate char read
} // here c2 == '\n' or EOF, end of C++ style comment
if (c2 == EOF)
{return 0;} // end program normally, exit main()
putchar(c2); // putchar('\n'); // C++ style comment ends with '\n'
}
else if (c2 == '*') // beginning of a C style comment
{
insideComment = TRUE;
while (insideComment) // insideComment == 1 (insideComment != 0)
{
while ((c1 = getchar()) != '*')
{
if (c1 == EOF)
{
printf("\nC style comment not closed properly\n"); // Error message
return 1; // end program with an error message
}
// else skip chars till '*'
}
c2 = getchar();
if (c2 == EOF)
{
printf("\nC style comment not closed properly\n"); // Error message
return 1; // end program with an error message
}
else if (c2 == '/') // end of C style comment
{
insideComment = FALSE; // reset
break; // out of while(insideComment)
}
// else continue; // C style comment continues
} // end of while (insideComment)
}
else // no comment, false alarm
{
putchar(c1);
if (c2 != EOF) {putchar(c2);}
else {return 0;} // end program normally, exit main()
}
}
else {putchar(c1);} // no comment
} // end of outer while()

return 0;
}
/*
gcc nocomment.c -o nocomment
./nocomment < nocomment.c > copy1.c

gcc copy1.c -o copy1
./copy1 < nocomment.c > copy2.c

diff -s copy1.c copy2.c
// Files copy1.c and copy2.c are identical
meld copy1.c copy2.c
// Files are identical

rm copy1 copy1.c copy2.c // clean
*/









Chapter_1     Exercise_1-22 BACK_TO_TOP Exercise_1-24



Comments

Popular posts from this blog

Contents

Blogger Page Margins in Contempo