Filter löschen
Filter löschen

How to read a text or a .c file without comments into a cell array ?

15 Ansichten (letzte 30 Tage)
I have a c file name 'function.c'
It have code and comments as well comments are noted with '/*' '*/' Or '//' OR sometimes '%'
I just have to read the content of code not the comments
I have tried : 1) fid=fopen('function.c') code = textscan(fid,'%s','CommentStyle',{'/*','*/'})
but this help me in excluding comments in between '/*' and '*/'
2) fid=fopen('function.c') code = textscan(fid,'%s','CommentStyle','//')
this excludes only comments with '//'
Are there any ways to excludes both types of comments ??
  1 Kommentar
dpb
dpb am 18 Sep. 2013
Bearbeitet: dpb am 18 Sep. 2013
Don't think so in on go...best I can think of is to read it with the first option and write that as a temporary file then process that file with the second.
I think textscan will only handle the case of '/*' '*/' pairs if they're first on the line, however, so perhaps you could get the same effect by reading the whole file as a char() array w/ fread, do a global substitution for strrep('/*','//') and then process that from memory?
Failing that, since one would presume c source files aren't terribly long, read line-by-line w/ fgetl and do the search on each line for either. This will let you get the end of line comments to as a side benefit to make up for the extra complexity...
ADDENDUM:
I reviewed the doc's for textscan; looks like it would catch the /* */ sequences correctly; what I recalled as beginning of line is actually written/documented as beginning of field.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 19 Sep. 2013
Bearbeitet: Jan am 19 Sep. 2013
The problem is not uniquely defined. How can you decide which comment style is used? What will happen for:
this is the contents /* and an intermediate // C++ comment */
Now removing the C++ comment at first will invalidate the file. The same happens, when % comments appear also. Another example:
this is contents // Here a C comment starts /*
this is contents // Here the C comment ends */
Does textscan consider quoted strings?
fprintf("This is not a comment: // %s\n", "hello");
By the way, if you mean Matlab comments, consider {% %} and ... also, because both start comments as % does.
Parsing comments is not trivial. The most reliable technique is to let Matlab's editor display the text with syntax highlighting, capture the screen, delete green pixels, use an OCR tool to parse the rest. ;-)
  2 Kommentare
dpb
dpb am 19 Sep. 2013
Is the Matlab editor reliable for comment parsing in C/C++ code?
Jan
Jan am 19 Sep. 2013
Bearbeitet: Jan am 19 Sep. 2013
Yes. This has been added between 6.5 and R2008a. The editor even creates C-comments for Ctrl-R and removes C and C++ comments for Ctrl-T.
Perhaps even the undocumented SyntaxTextPane can display this, if the code type is set correctly.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Cedric
Cedric am 19 Sep. 2013
Bearbeitet: Cedric am 19 Sep. 2013
What about a simple..
buffer = fileread( 'function.c' ) ;
buffer = regexprep( buffer, '/\*.*?\*/', '' ) ;
buffer = regexprep( buffer, '//.*?(\r)?\n', '$1\n' ) ;
which seems to be working in standard situations. Of course, managing all situations including pathological ones would require more work, but this has the advantage to be simple and you might not need to manage pathological cases, especially if you wrote these C files yourself, consistently and with rigor.
PS: I tested it with the following content
NotComment 1 /* Comment */
// Comment
/* Comment */ NotComment 2
NotComment 3 // Comment
/* Comment // Comment */ NotComment 4
/* Comment
Comment // Comment
Comment */
NotComment 5
  2 Kommentare
Jan
Jan am 20 Sep. 2013
@Cedric: Providing your test data is a rock solid definition of the capabilities of your code. While regexp expressions always look like somebody has rolled an angry armadillo over the keyboard, the test data are clear due to their simplicity. +1
Cedric
Cedric am 20 Sep. 2013
Thank you Jan. Well, providing the test data shows that I am not managing this situation that you listed
this is contents // Here a C comment starts /*
this is contents // Here the C comment ends */
We could actually, but I'll see what the OP answers before making patterns more complex.

Melden Sie sich an, um zu kommentieren.


Simon
Simon am 19 Sep. 2013
Hi!
I usually do it this way:
* read full file with textscan
* define cell array of comment strings
* use regexp to find all lines with a comment string in it (or in front of)
* delete the lines found
  2 Kommentare
Jan
Jan am 19 Sep. 2013
  • ignore comment keys in quotes strings
  • consider multi-line comments
Simon
Simon am 19 Sep. 2013
Yes, my list was not complete. I'm sure it can be enlarged with many more entries! It's easier if you have a fixed-format source code like I usually have with Fortran 77. You just search for "C" in column 1 ...

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by