[問題]如何去get C variable的scope ?

看板PLT (程式語言與理論)作者 (allstars)時間17年前 (2008/06/26 11:49), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/3 (看更多)
我的問題是想如何得到variables的scope number 例如 void foo() { int a; { int b; } b=1; // <- compile error here } b=1那會有compile error 但是我想了解 "compiler" 是如何知道的 在書上看到 compiler會把這些variable加到symbol table裡 但由於有scope的關系 所以symbol table會有地方來store scope number 並藉由maintain 一個scope stack 來作variable的binding 所以我想把上面的程式加上一個function void foo() { int a; get_scope_number_and_print(); //assume scope# is 2 { int b; get_scope_number_and_print(); //assume scope# is 3 } get_scope_number_and_print(); // assume scope# is 2 b=1; } and the first and last get_scope_number_and_print() should get the same result 所以compiler 在b=1前面時 能在symtab裡找scope is 2 的var 發現並沒有b and complains 'b not found !' 但是我想scope ,symtab這些東西是compiler dependant的 所以想問gcc有沒有提供API來得到這些information 我是有知道gdb可以看的出 像gdb裡面的 maint print type or maint print symbols 例如maint print type 裡 <gdb> ....<skip> type node 0x10113ed0 name 'int' (0x1010cde0) tagname '<NULL>' (0x0) code 0x7 (TYPE_CODE_INT) length 4 upper_bound_type 0x0 (BOUND_ lower_bound_type 0x0 (BOUND_ objfile 0x10105260 target_type 0x0 pointer_type 0x0 reference_type 0x0 type_chain 0x10113ed0 instance_flags 0x0 flags 0x0 nfields 0 0x0 vptr_basetype 0x0 vptr_fieldno -1 type_specific 0x0 </gdb> 這裡面.......好像就沒有scope的information??? 或者是由上面的某個memory address來知道scope information?? 如果能有大大來說明一下上面是啥鬼東西就好了 不過在maint print symbols 可以看到有Blockvector 的東西 而這block number 的index 就跟我說的scope number 一樣 由這個Blockvector 就可以來作variable的binding了 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 211.22.63.250
文章代碼(AID): #18On7Yve (PLT)
文章代碼(AID): #18On7Yve (PLT)