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<br style="padding: 0; margin: 0"></br>$ ./script.sed < input.c > output.c<br style="padding: 0; margin: 0"></br>$ 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
- #! /bin/sed-nf
- # Remove C and C++ comments, by Brian Hiles(brian_hiles@rocketmail.com)
- # Sped up(and bugfixed to some extent)by Paolo Bonzini(bonzini@gnu.org)
- # Works its way through the line, copying to hold space the text up to the
- # first special character(/,", '). The original version went exactly a
- # character at a time, hence the greater speed of this one. But the concept
- # and especially the trick of building the line in hold space are entirely
- # merit of Brian.
- :loop
- # This line is sufficient to remove C++ comments!
- /^/// s,.*,,
- /^$/{
- x
- p
- n
- b loop
- }
- /^"/{
- :double
- /^$/{
- x
- p
- n
- /^"/b break
- b double
- }
- H
- x
- s,n(.[^"]).,1,
- x
- s,.[^"]*,,
- /^"/bbreak
- /^/{
- H
- x
- s,n(.).*,1,
- x
- s/.//
- }
- b double
- }
- /^'/{
- :single
- /^$/{
- x
- p
- n
- /^'/bbreak
- b single
- }
- H
- x
- s,n(.[^']).,1,
- x
- s,.[^']*,,
- /^'/bbreak
- /^/{
- H
- x
- s,n(.).*,1,
- x
- s/.//
- }
- b single
- }
- /^/*/{
- s/.//
- :ccom
- s,^.[^],,
- /^$/ n
- /^*//{
- s/..//
- b loop
- }
- b ccom
- }
- :break
- H
- x
- s,n(.[^"'/]).,1,
- x
- s/.[^"'/]*//
- b loop
原文链接: https://www.cnblogs.com/MagicLetters/archive/2013/04/14/3444258.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/84546
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!