gawk1.01源碼大體結構分析

gawk1.01源碼大體結構分析
昨天找gawk的選項,因爲我想分析其調試過程。我想根據其debug的過程,來分析程序。發現在awk1.c中有-D選項,試一下,結果出現一大堆:
yang@DESKTOP-V9HS3B6:~/gawk101/gawk$ echo "ok" |./gawk '{print $0}'
ok
yang@DESKTOP-V9HS3B6:~/gawk101/gawk$ echo "ok" |./gawk -D '{print $0}'
Starting parse
Entering state 0
Reducing stack 0 by rule 51 (line 264):
-> $$ = nterm optional_newlines ()
Entering state 2
Reading a token: Next token is token '{' ()
Reducing stack 0 by rule 5 (line 124):
-> $$ = nterm pattern ()
Entering state 20
Next token is token '{' ()
Shifting token '{' ()
Entering state 44
Reading a token: Next token is token LEX_PRINT ()
Reducing stack 0 by rule 26 (line 194):
-> $$ = nterm whitespace ()
Entering state 76
Next token is token LEX_PRINT ()
Shifting token LEX_PRINT ()
Entering state 103
Reducing stack 0 by rule 39 (line 220):
-> $$ = nterm $@3 ()
Entering state 123
Reading a token: Next token is token '$' ()
Shifting token '$' ()
Entering state 17
Reading a token: Next token is token NUMBER ()
Shifting token NUMBER ()
Entering state 35
Reducing stack 0 by rule 87 (line 357):
   $1 = token NUMBER ()
-> $$ = nterm v_exp ()
Entering state 41
Reducing stack 0 by rule 97 (line 383):
   $1 = token '$' ()
   $2 = nterm v_exp ()
-> $$ = nterm variable ()
Entering state 23
Reading a token: Next token is token NEWLINE ()
Reducing stack 0 by rule 68 (line 314):
   $1 = nterm variable ()
-> $$ = nterm exp ()
Entering state 62
Next token is token NEWLINE ()
Reducing stack 0 by rule 58 (line 290):
   $1 = nterm exp ()
-> $$ = nterm expression_list ()
Entering state 141
Next token is token NEWLINE ()
Reducing stack 0 by rule 53 (line 272):
-> $$ = nterm redirection ()
Entering state 152
Next token is token NEWLINE ()
Shifting token NEWLINE ()
Entering state 119
Reducing stack 0 by rule 51 (line 264):
-> $$ = nterm optional_newlines ()
Entering state 139
Reading a token: Next token is token '}' ()
Reducing stack 0 by rule 24 (line 186):
   $1 = token NEWLINE ()
   $2 = nterm optional_newlines ()
-> $$ = nterm statement_term ()
Entering state 163
Reducing stack 0 by rule 40 (line 219):
   $1 = token LEX_PRINT ()
   $2 = nterm $@3 ()
   $3 = nterm expression_list ()
   $4 = nterm redirection ()
   $5 = nterm statement_term ()
-> $$ = nterm statement ()
Entering state 109
Reducing stack 0 by rule 22 (line 179):
   $1 = nterm statement ()
-> $$ = nterm statements ()
Entering state 108
Next token is token '}' ()
Shifting token '}' ()
Entering state 130
Reducing stack 0 by rule 21 (line 174):
   $1 = token '{' ()
   $2 = nterm whitespace ()
   $3 = nterm statements ()
   $4 = token '}' ()
-> $$ = nterm action ()
Entering state 45
Reading a token: Next token is token NEWLINE ()
Shifting token NEWLINE ()
Entering state 77
Reducing stack 0 by rule 51 (line 264):
-> $$ = nterm optional_newlines ()
Entering state 112
Reading a token: Now at end of input.
Reducing stack 0 by rule 4 (line 118):
   $1 = nterm pattern ()
   $2 = nterm action ()
   $3 = token NEWLINE ()
   $4 = nterm optional_newlines ()
-> $$ = nterm rule ()
Entering state 19
Reducing stack 0 by rule 2 (line 111):
   $1 = nterm rule ()
-> $$ = nterm program ()
Entering state 18
Now at end of input.
Reducing stack 0 by rule 1 (line 106):
   $1 = nterm optional_newlines ()
   $2 = nterm program ()
-> $$ = nterm start ()
Entering state 1
Now at end of input.
Shifting token $end ()
Entering state 3
Cleanup: popping token $end ()
Cleanup: popping nterm start ()
Found a rule:22492340
Statements:22492316
PRINT:22492316
var_type ref:22492268
DATA:22504512
ok
yang@DESKTOP-V9HS3B6:~/gawk101/gawk$
這麼多內容,我的博客都可以不用寫了。有湊字數之嫌。
我原來開發時,覺得根據作者的日誌是可以很快的理解程序的。但這個日誌太長了。
於是,還是讀源碼吧。我的想法是,如果能把NODE結果的內容打印出來,就好了。
       可是這個要求達不到呀。
昨天做了一個夢,大意是深圳修了一段高鐵,因爲技術發展太快,以至於當時一流的技術居然落伍了。此時,大家就開始批評了,此時,有人就講,這也是一流的設計。等等。
        也許正切合了我此時,讀代碼的心情。
        gawk1.01的文件很少。
        awk1.c是程序的主循環。實現把文件打開,分成$0,$1等字段變量,並調bison其它函數進行程序分析。
        awk2.c主要是按編譯的程序,對每行文件進行處理。
        awk3.c主要是一些內部函數的代碼。
        awk.y是語法文件。
        debug.c是用於進行調試的。我想打印一個NODE在這裏。
        obstack.c是用於分配內存的。
        regex.c是正則表達式包。
        awk.h是分析樹結點的定義。
早上一醒來,就在牀上想,代碼的整體結構,因爲我現在把軟件編譯通過了,我可以對之修改,反映我的理解。太爽了。
        x=3
        x++
        print x
       我想,每次增加10,可以呀,修改程序awk2.c中354行:
     assign_number(lhs,force_number(*lhs) + (tree->type==Node_preincrement ?
           10.0 : -1.0));
     想增加多少就改爲多少。真是太爽了。
     我以爲,象讀小說一樣讀源碼,先把主要情節想清楚,慢慢由粗到細,看不懂的,就大膽的跳過去。等後面理解了,再讀前面的。我又把awk1.c
     awk2.c通讀了一次,感覺真爽。我寫不出來,但可以理解,發現高人,是如何寫作的。我甚至想,我要建立一個git賬號,把代碼上傳到服務器,這樣每個本子上的代碼就一致了。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章