Shell / sed Program To Remove All C and C++ Comments From Program File

by on May 6, 2008 · 5 comments

Write the shell / sed program which remove all the comments from a simple C program stored in your current directory. You can assume that the c source code contains only syntactically correct comments:
+ start with // and end with a newline

+ starting with /* and ending */ (can be multiline)

+ nesting of comments is not allowed

Make sure that C source is not changed at all.

How do I use this sed script?

$ ./script.sed < input.c
$ ./script.sed < input.c > output.c
$ for c in *.c; do script.sed < $c > /tmp/zyzcc.c; /bin/cp -f /tmp/zyzcc.c $c; done

Sample sed code to remove all comments from sed

  1. #! /bin/sed -nf
  2.  
  3. # Remove C and C++ comments, by Brian Hiles (brian_hiles@rocketmail.com)
  4.  
  5. # Sped up (and bugfixed to some extent) by Paolo Bonzini (bonzini@gnu.org)
  6. # Works its way through the line, copying to hold space the text up to the
  7. # first special character (/, ", '). The original version went exactly a
  8. # character at a time, hence the greater speed of this one. But the concept
  9. # and especially the trick of building the line in hold space are entirely
  10. # merit of Brian.
  11.  
  12. :loop
  13.  
  14. # This line is sufficient to remove C++ comments!
  15. /^\/\// s,.*,,
  16.  
  17. /^$/{
  18. x
  19. p
  20. n
  21. b loop
  22. }
  23. /^"/{
  24. :double
  25. /^$/{
  26. x
  27. p
  28. n
  29. /^"/b break
  30. b double
  31. }
  32.  
  33. H
  34. x
  35. s,\n\(.[^\"]*\).*,\1,
  36. x
  37. s,.[^\"]*,,
  38.  
  39. /^"/b break
  40. /^\\/{
  41. H
  42. x
  43. s,\n\(.\).*,\1,
  44. x
  45. s/.//
  46. }
  47. b double
  48. }
  49.  
  50. /^'/{
  51. :single
  52. /^$/{
  53. x
  54. p
  55. n
  56. /^'/b break
  57. b single
  58. }
  59. H
  60. x
  61. s,\n\(.[^\']*\).*,\1,
  62. x
  63. s,.[^\']*,,
  64.  
  65. /^'/b break
  66. /^\\/{
  67. H
  68. x
  69. s,\n\(.\).*,\1,
  70. x
  71. s/.//
  72. }
  73. b single
  74. }
  75.  
  76. /^\/\*/{
  77. s/.//
  78. :ccom
  79. s,^.[^*]*,,
  80. /^$/ n
  81. /^\*\//{
  82. s/..//
  83. b loop
  84. }
  85. b ccom
  86. }
  87.  
  88. :break
  89. H
  90. x
  91. s,\n\(.[^"'/]*\).*,\1,
  92. x
  93. s/.[^"'/]*//
  94. b loop

Please note that this program is copied from public domain.



4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

Previous Script:

Next Script: