[請益] 實作判斷code中 { } 的配對 (有不成對的)
大家好
小弟在寫一個程式 目的是parse
一份code的trace過程所丟出的log
我的方式如下
==================
如果原本code是
int func1()
{
do something;
return func2();
}
int func2()
{
return xxx;
}
將其改為
int func1()
{
cout << ">>>func1";
do something;
cout << "call func2()";
int a = func2();
cout << "<<<func1"
return a;
}
int func2()
{
cout << ">>>func2";
do something;
cout << "<<<func2";
return xxx;
}
其中cout的行數是拿來丟到log中
==================
得到log如下
>>>main()
call func1()
>>>func1
call func2()
>>>func2
<<<func2
<<<func1
<<<main()
==================
我以程式對log內容做tab縮排
psuedo code如下
int tabcount = 0;
read line to string Line;
if (Line.pos("<<<"))
tabcount--;
for (int k = 0 ; k < tabcount; k++)
Line = "\t" + Line;
if (Line.pos(">>>"))
tabcount++;
==================
處理結果如下
>>>main()
call func1()
>>>func1
call func2()
>>>func2
<<<func2
<<<func1
<<<main()
==================
上面這是一個簡單且理想的情況
但是在exception 或者 goto的情況
會導致一些>>> 找不到配對的<<<
出現越來越多tab的發散情況
請問一下板上大大們
對於這個問題有沒有什麼建議解決方式
可能可以改丟出的log格式
或者是 保有這個log模式
另外設計對>>> 與 <<<的判斷
感謝大家
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.59.147.226
※ 文章網址: http://www.ptt.cc/bbs/Programming/M.1411099725.A.55C.html
※ 編輯: donkeychen (210.59.147.226), 09/19/2014 12:09:21
※ 編輯: donkeychen (210.59.147.226), 09/19/2014 12:10:33
→
09/19 12:11, , 1F
09/19 12:11, 1F
→
09/19 12:11, , 2F
09/19 12:11, 2F
→
09/19 12:11, , 3F
09/19 12:11, 3F
→
09/19 12:18, , 4F
09/19 12:18, 4F
→
09/19 14:29, , 5F
09/19 14:29, 5F
※ 編輯: donkeychen (210.59.147.226), 09/19/2014 16:30:37
大大 請問一下
http://codepad.org/91NhNGcS
這是我的CODE
OUTPUT會是
[0x00179490] .\test.cpp<76>
>>>[123TOTAIWAN]
[0x00179490] .\test.cpp<76>
<<<[123TOTAIWAN]
[0x00179490] .\test.cpp<78>
arg1 = 2
arg2 = 5
CLASSA.tid = [0x00179490]
[0x00179490] .\test.cpp<76>
<<<[123TOTAIWAN]
請問為何會出現兩次<<< 呢
感謝
※ 編輯: donkeychen (210.59.147.226), 09/19/2014 18:01:10
→
09/22 14:46, , 6F
09/22 14:46, 6F
前兩個 >>> <<< cout出配對的this 是同一個 最後一個cout出<<<的this 是另一個@@
※ 編輯: donkeychen (210.59.147.226), 09/22/2014 14:47:36
→
09/25 10:36, , 7F
09/25 10:36, 7F
Programming 近期熱門文章
PTT數位生活區 即時熱門文章