|
cソースファイルから関数を見つけるってのはかなり難しいですね。
まともに作るとパーサもどきができちゃう気がします。
とおりさんの言うとおり、
>同じように関数もどこからどこまでが1つの「関数」なのかを認識しなくてはなりません。
というのを決定するところからはじめる必要があります。
なので、この課題がどこまで妥協していい問題かが重要です(笑)
適当に、'単語+()'を関数として表示するプログラムを作ってみました。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char *getfunc(char *line){
char *st,*end,*buf,c;
//'('と')'を探す
st = strchr(line,'(');
if(st == NULL) return NULL;
buf = getfunc(st+1);
if(buf == NULL) end = strchr(st+1,')');
else end = strchr(buf,')');
if(end == NULL) return NULL;
//'()'が見つかったならその直前の単語を関数名とみなす
for(st--;st > line && isspace(*st);st--)//空白飛ばし
;
for(st--;st >= line && isgraph(*st);st--)//単語探し
;
//出力
st++;
end++;
c = *end;
*end = '\0';
printf("%s\n",st);
*end = c;
return end;
}
int main(){
char line[256];
while(fgets(line,256,stdin) != NULL){
getfunc(line);
}
return 0;
}
実行結果:
このソースファイルをプログラムにかけると
*getfunc(char *line)
//'('と')
line,'(')
if(st == NULL)
getfunc(st+1)
strchr(st+1,')
if(buf == NULL) end = strchr(st+1,')')
strchr(buf,')
if(end == NULL)
//'()
isspace(*st)
for(st--;st > line && isspace(*st);st--)
isgraph(*st)
for(st--;st >= line && isgraph(*st);st--)
printf("%s\n",st)
main()
fgets(line,256,stdin)
while(fgets(line,256,stdin) != NULL)
getfunc(line)
と出力されます。
明らかに間違ってるやつ+ifやforなども出力されてますが、先に書いた妥協の結果ですw
とか書いてるうちに親記事消された。。。
問題自体はすごくいいものなんだけどな。
|