0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Cコード解析】コメントの削除

Posted at

コメントの記述を削除する

gccコンパイラを使ってプリプロセッサをかければいいじゃん。という話は置いといて、それをpythonで実現してみる。

import regex
import glob

filelist = glob.glob("test.c", recursive=True)


REGEX_PATTERN_BLOCK_COMMENT = r"/\*.*?\*/"
REGEX_PATTERN_LINE_COMMENT = r"//.*?(?=\n)"

for file in filelist:
	with open(file, "r", encoding="utf-8") as file_obj:
		filetext = file_obj.read()
		filetext = regex.sub(REGEX_PATTERN_BLOCK_COMMENT, "", filetext, flags=regex.DOTALL)
		filetext = regex.sub(REGEX_PATTERN_LINE_COMMENT, "", filetext, flags=regex.DOTALL)

	with open(file, "w", encoding="utf-8") as file_obj:
		file_obj.write(filetext)

globメソッドの部分を変えれば、複数のファイルを一度に処理できます。

まとめ

コメントを削除することで他の解析作業に活かせたりする。コードツリーの作成とか。。。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?