我正在爲我的C文件論文工作。我需要拆分一個C頭來操作構成它的單個單詞。我有所有這些字符分割線:C文件在Python中分裂?
- 空間和標籤
- 新線
;
或\n
- 單行註釋和多行註釋
// /* */
- 指針
*
- 陣列支架
[]
或[dimension]
因此,舉例來說,如果我有這幾行:
/* This is a
multiline comment.
*/
typedef int rocket;
// This is a single line comment.
typedef double* pi[10];
分割函數返回像這樣的列表(或一切):
["/*","This","is","a","\n","multiline","comment.","\n","*/","\n","\n","typedef","int","rocket",";","\n","\n","//","This","is","a","single","line","comment.","\n","typedef","double","*","pi","[10]",";"]
,這應該在Python做!
我試過這個,但是我的關於正則表達式的小知識暗示我發現了一個可怕的解決方案! :)
謝謝, 達里奧。
你可能會得到一個C語法分析器... –
不會放棄評論,空行嗎? –
@yi_H:這取決於解析器。我已經在Delphi中編寫了一個簡單的C語言分析器(並不是完整的,但足夠用於我的目的),它保留了註釋,甚至在正確的位置將它們格式化爲Delphi的結果。大多數C和C++編譯器附帶的*預處理器*可能會刪除註釋。 –