Extraiga solo los nombres de las funciones y sus comentarios del código C

I want to extract comments and want to know from which functions they are. I have lots of such C files as below:

Con entrada:

void main()
{
    //sdgs
    call A;
    /*
    sdfgs
    dfhdfh
    */
    call b;
    some code;
}

/* this function adds
 something */
int add()
{
    //sgsd
    some code;
    //more comments
    some code;
}

La salida debe ser:

void main()
{
    //sdgs

    /*
    sdfgs
    dfhdfh
    */

}

/* this function adds
 something */
int add()
{
    //sgsd

    //more comments

}

Input code is neatly formatted and 'function code' starts after { at next line. Basically, I just need to know which 'comment' is from which function. Also it should include any other comments above function name or elsewhere. Note: this is different as function names at top level should be there.

To simplify my requirements:

  1. Print all comments
  2. Detect a block containing ( on the first line, with a single line containing only { at first column after one to three lines and print the lines immediately above.

preguntado el 08 de noviembre de 11 a las 13:11

3 Respuestas

This is impossible with regular expressions, you need to write a little C parser.

¿Por qué?

First there are macro's that need to be substituted first. Second because function defintions are kind of "hard" to put in a regular expression. Some legal function definitions:

int f() {}
const int f() {}
const char* f(int);
void f(double t);
void f(t,a) int t; int (*a)(float, char, char) {}

respondido 08 nov., 11:17

Just out of curiosity, what is: "void f(t,a) int t;" doing? - John Humphreys

@w00te: It declares a function returning nothing (void) that takes two parameters, an int llamado t and a pointer to a function taking a float, char, char as arguments and returning an int llamado a. I did make a (now edited) mistake, I forgot the return type of the function pointer. Also, this is a combination of old-style function declarations and function pointers. - orlp

I though the 3rd and 4th examples would be a valid declaration, but not definition. - Sidyll

@sidyll: Sure, but you can easily replace the semicolon with brackets to make them definitions, it was just an example. - orlp

ok, if line x has { at first column and that's the only thing in that line, then x-l has function name end with ) and goes up till line number (x minus --) till start of ( <==is it still not possible? - Piyush

It is perhaps not doable in a very general sense (e.g. because functions could be defined by what is apparently a macro invocation).

But if you don't care about perfection, you might make a simple lexer & parser which nearly does the job (on input code which is not too contrived).

I'm not sure it is useful. You should explain what you really want to achieve.

respondido 08 nov., 11:17

function name is not in defined thru macro or any other complex way. it's function like unsigned int func_data( arg one, agrg two) { - Piyush

you want to print only comments and function defintions from a well formated file. In my opinion this specific task is doable without a proper parser.

Tratar:

comments.awk:

/^(int|void|{|}|etc.)/{print $0;next}
/\/\*/,/\*\//{print $0;next}
/\s*\/\//{print $0;next}

llamar con:

awk -f comments.awk file1

respondido 08 nov., 11:17

but it print function name containing int or void. that's not what i want. i think awk/sed being line processor--it cannot be done unless one can see 3 or 4 lines ahead. thanks for ur help though - Piyush

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.