Advertisement
bitwise_gamgee

Untitled

Jun 6th, 2023
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.06 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define STATE_OUT 0
  4. #define STATE_IN  1
  5.  
  6. void remove_comments();
  7.  
  8. int main()
  9. {
  10.     remove_comments();
  11.     return 0;
  12. }
  13.  
  14. void remove_comments()
  15. {
  16.     // Variable to hold each character read from the input
  17.     int ch;
  18.  
  19.     // Variables to keep track of whether we're currently inside a comment, a quote, or a line comment
  20.     int comment_state = STATE_OUT;
  21.     int quote_state = STATE_OUT;
  22.     int line_comment_state = STATE_OUT;
  23.  
  24.     // Variable to remember the last character we saw
  25.     int last_char = 0;
  26.    
  27.     // Loop over each character in the input until we reach the end of the input
  28.     while ((ch = getchar()) != EOF) {
  29.    
  30.         // If the character is a quote, and we're not inside a comment or line comment, toggle the quote state
  31.         if (ch == '"' && last_char != '\\' && comment_state == STATE_OUT && line_comment_state == STATE_OUT)
  32.         {
  33.             quote_state = (quote_state == STATE_OUT) ? STATE_IN : STATE_OUT;
  34.         }
  35.        
  36.        
  37.         // If the character is a '/', and the last character was also a '/', and we're not inside a comment or quote, enter line comment state
  38.         else if (ch == '/' && last_char == '/' && comment_state == STATE_OUT && quote_state == STATE_OUT)
  39.         {
  40.             line_comment_state = STATE_IN;
  41.         }
  42.        
  43.        
  44.         // If the character is a '*', and the last character was a '/', and we're not inside a line comment or quote, enter comment state
  45.         else if (ch == '*' && last_char == '/' && line_comment_state == STATE_OUT && quote_state == STATE_OUT)
  46.         {
  47.             comment_state = STATE_IN;
  48.         }
  49.        
  50.        
  51.         // If the character is a '/', and the last character was a '*', and we're inside a comment, leave comment state
  52.         else if (ch == '/' && last_char == '*' && comment_state == STATE_IN)
  53.         {
  54.             comment_state = STATE_OUT;
  55.             ch = 0; // Ensures the '/' isn't printed after a '*/'
  56.         }
  57.        
  58.        
  59.         // If the character is a newline, and we're inside a line comment, leave line comment state
  60.         else if (ch == '\n' && line_comment_state == STATE_IN)
  61.         {
  62.             line_comment_state = STATE_OUT;
  63.         }
  64.        
  65.  
  66.         // If we're inside a quote, or we're not inside a comment or line comment, print the character
  67.         if (comment_state == STATE_OUT && line_comment_state == STATE_OUT && quote_state == STATE_IN)
  68.         {
  69.             putchar(ch);
  70.         }
  71.         else if (comment_state == STATE_OUT && line_comment_state == STATE_OUT && quote_state == STATE_OUT)
  72.         {
  73.             // If we're not just starting a comment or line comment, print the last character
  74.             if ((ch != '/' && last_char != '/') || (ch != '*' && last_char != '/'))
  75.             {
  76.                 if (last_char) putchar(last_char);
  77.             }
  78.         }
  79.        
  80.  
  81.         // Update last_char to the current character
  82.         last_char = ch;
  83.     }
  84.  
  85.  
  86.     // After we've processed all the input, if there's a last character that we didn't print yet, print it now
  87.     if (last_char) putchar(last_char);
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement