The problem I am facing is with an ANSI compiler that requires C style comments.
So I am trying to convert my existing comments to comply with the C standard ISO C89.
I am looking for a SED expression to replace // comments with /* comments EXCEPT when // comments appear within /* */ comments (which would break the comment).
I have tried this (a range expression) to no avail:
sed -e '/\/*/,/*\//! s_//\(.*\)_/*\1 */_' > filename
Will something work to ignore the 1 line comments inside a comment like this but change everything else?
/**********************************
* Some comment
* an example bit of code within the comment followed by a //comment
* some more comment
***********************************/
y = x+7; //this comment must be changed
Thanks!
Convert code to colored HTML with any convertor that can output different markup for
/*
and//
comments, process the output with perl/awk/sed/whatever, then strip the markup.If you can't use @ephemient's suggestion, then you'll need to apply your regex across multiple lines, which is not sed's default behaviour. sed has a hold buffer, which allows you to append multiple strings together and apply the regex to the concatenated string.
The sed expression would look like this:
1h
- if it is the first line, put the line into the hold buffer (emptying it first)1!H
- If not the first line, append to the hold buffer$ {...}
- if the last line, execute this sed commandNow your matcher expression will work even if the /* and */ are on different lines.
This might work for you (GNU sed):
Explanation:
:a;$!{N;ba}
slurp the file into the pattern spaces/^/\x00/
set a marker N.B. this can be any character not found in the filetb;:b
reset the substitution switch by jumping to the place holderb
s/\x00$//;t
marker has reached the end of the file. All done.s/\x00\(\/\*[^*]*\*\+\([^/*][^*]*\*\+\)*\/\)/\1\x00/;tb
this regexp matches c style comments and bumps the marker passed them if true.s/\x00\/\/\([^\n]*\)/\/*\1\*\/\x00/;tb
this regexp matches the single line comment, replaces with c style comments and bumps the marker passed them if true.s/\x00\(.\)/\1\x00/;tb
this regexp matches any single character and bumps the marker passed it if true.Here's a lightly tested filter written in C that should perform the conversion you want. Some comments about what this filter does that are difficult if not impossible to handle with a regex:
/*
or*/
gets changed to/+
or/|
). I wasn't sure if you needed this or not (if you don't, it should be easy to remove)??/
).Of course, you'll need to perform your own testing to determine if it's suitable for your purposes.
Some indulgent commentary: many years ago, a shop I worked at wanted to impose a coding standard that forbade C99-style comments on the grounds that even though the compiler we used at the time had no problem with them, the code might have to be ported to a compiler that didn't support them. I (and others) successfully argued that that possibility was so remote as to be essentially non-existant, and that even if it did happen, a conversion routine to make the comments compatible could be easily written. We were permitted to use C99/C++ style comments.
I now consider my oath fulfilled, and whatever curse that may have been laid on me to be lifted.
You can do this (almost) entirely in sed, you just need one call to
tr
:translate_comments_prepare.sed
Then we pipe the result of the "preprocessing" step through
tr -d '\n'
to join all lines (I haven't figured out a good way to do this from withinsed
).And then we do the real work:
translate_comments.sed
Then we basically put everything together