首頁 >後端開發 >C++ >由於 clang 中的「-emit-asm」標誌不再可用,如何使用替代方法從 C 程式碼中取得人類可讀的抽象語法樹(AST)?

由於 clang 中的「-emit-asm」標誌不再可用,如何使用替代方法從 C 程式碼中取得人類可讀的抽象語法樹(AST)?

Linda Hamilton
Linda Hamilton原創
2024-12-13 06:29:081001瀏覽

How to obtain a human-readable Abstract Syntax Tree (AST) from C   code using alternative methods since the `-emit-asm` flag in clang is no longer available?

如何從C 代碼獲取人類可讀的AST

clang 曾經透過-emit-asm 標誌實現此功能,但是,此後它已刪除。然而,您可以採用一些可行的替代方案來實現此目的:

簡單斐波那契程式:

int fib(int n) {
  if (n == 0 || n == 1) 
    return n;

  int fib1 = 0; 
  int fib2 = 1;
  int fib = 0;

  for ( int i = 2; i < n; i++ ) 
  {
    fib = fib1 + fib2;
    fib1 = fib2;
    fib2 = fib;
  }

  return fib;
}

AST 表示:

(translation_unit@Cpp~GCC4=2#4dc3a00^0 Line 1 Column 1 File C:/temp/fibonacci.cpp
 (function_definition@Cpp~GCC4=1615#4dc39e0^1#4dc3a00:1 Line 1 Column 1 File C:/temp/fibonacci.cpp
  (function_head@Cpp~GCC4=1627#4d89de0^1#4dc39e0:1 Line 1 Column 1 File C:/temp/fibonacci.cpp
   (simple_type_specifier@Cpp~GCC4=1109#4d89920^1#4d89de0:1 Line 1 Column 1 File C:/temp/fibonacci.cpp
   |('int'@Cpp~GCC4=2760#4d898c0^1#4d89920:1[Keyword:0] Line 1 Column 1 File C:/temp/fibonacci.cpp)'int'
   )simple_type_specifier
   (noptr_declarator@Cpp~GCC4=1401#4d89a80^1#4d89de0:2 Line 1 Column 5 File C:/temp/fibonacci.cpp
   |(IDENTIFIER@Cpp~GCC4=2645#4d89900^1#4d89a80:1[`fib'] Line 1 Column 5 File C:/temp/fibonacci.cpp)IDENTIFIER
   |('('@Cpp~GCC4=2885#4d89ac0^1#4d89a80:2[Keyword:0] Line 1 Column 8 File C:/temp/fibonacci.cpp)'('
   |(parameter_declaration@Cpp~GCC4=1590#4d89e00^1#4d89a80:3 Line 1 Column 9 File C:/temp/fibonacci.cpp
   | (simple_type_specifier@Cpp~GCC4=1109#4d89b20^1#4d89e00:1 Line 1 Column 9 File C:/temp/fibonacci.cpp
   |  ('int'@Cpp~GCC4=2760#4d89d60^1#4d89b20:1[Keyword:0] Line 1 Column 9 File C:/temp/fibonacci.cpp)'int'
   | )simple_type_specifier
   | (IDENTIFIER@Cpp~GCC4=2645#4d89b00^1#4d89e00:2[`n'] Line 1 Column 13 File C:/temp/fibonacci.cpp)IDENTIFIER
   |)parameter_declaration
   |(')'@Cpp~GCC4=2886#4d89bc0^1#4d89a80:4[Keyword:0] Line 1 Column 14 File C:/temp/fibonacci.cpp)')'
   |(function_qualifiers@Cpp~GCC4=1417#4d89dc0^1#4d89a80:5 Line 1 Column 16 File C:/temp/fibonacci.cpp)function_qualifiers
   )noptr_declarator
  )function_head
  (compound_statement@Cpp~GCC4=872#4dc39c0^1#4dc39e0:2 Line 1 Column 16 File C:/temp/fibonacci.cpp
   ('{'@Cpp~GCC4=2938#4d89f20^1#4dc39c0:1[Keyword:0] Line 1 Column 16 File C:/temp/fibonacci.cpp)'{'
   (statement_seq@Cpp~GCC4=876#4dc3060^1#4dc39c0:2 Line 2 Column 5 File C:/temp/fibonacci.cpp
   |(statement_seq@Cpp~GCC4=876#4dc3920^1#4dc3060:1 Line 2 Column 5 File C:/temp/fibonacci.cpp
   | (statement_seq@Cpp~GCC4=876#4dc2880^1#4dc3920:1 Line 2 Column 5 File C:/temp/fibonacci.cpp
   |  (statement_seq@Cpp~GCC4=876#4dc2700^1#4dc2880:1 Line 2 Column 5 File C:/temp/fibonacci.cpp
   |   (statement_seq@Cpp~GCC4=876#4dc2640^1#4dc2700:1 Line 2 Column 5 File C:/temp/fibonacci.cpp
   |   |(selection_statement@Cpp~GCC4=892#4dc25c0^1#4dc2640:1 Line 2 Column 5 File C:/temp/fibonacci.cpp
   |   | ('if'@Cpp~GCC4=2753#4d89f40^1#4dc25c0:1[Keyword:0] Line 2 Column 5 File C:/temp/fibonacci.cpp)'if'
   |   | ('('@Cpp~GCC4=2885#4d89f60^1#4dc25c0:2[Keyword:0] Line 2 Column 8 File C:/temp/fibonacci.cpp)'('
   |   | (logical_or_expression@Cpp~GCC4=763#4dc2220^1#4dc25c0:3 Line 2 Column 10 File C:/temp/fibonacci.cpp
   |   |  (equality_expression@Cpp~GCC4=696#4d89fa0^1#4dc2220:1 Line 2 Column 10 File C:/temp/fibonacci.cpp
   |   |   (IDENTIFIER@Cpp~GCC4=2645#4d89d80^1#4d89fa0:1[`n'] Line 2 Column 10 File C:/temp/fibonacci.cpp)IDENTIFIER
   |   |   ('=='@Cpp~GCC4=2918#4d89da0^1#4d89fa0:2[Keyword:0] Line 2 Column 12 File C:/temp/fibonacci.cpp)'=='
   |   |   (INT_LITERAL@Cpp~GCC4=2809#4d89fe0^1#4d89fa0:3[0] Line 2 Column 15 File C:/temp/fibonacci.cpp)INT_LITERAL
   |   |  )equality_expression
   |   |  ('||'@Cpp~GCC4=2922#4d89f80^1#4dc2220:2[Keyword:0] Line 2 Column 17 File C:/temp/fibonacci.cpp)'||'
   |   |  (equality_expression@Cpp~GCC4=696#4dc2200^1#4dc2220:3 Line 2 Column 20 File C:/temp/fibonacci.cpp
   |   |   (IDENTIFIER@Cpp~GCC4=2645#4dc2180^1#4dc2200:1[`n'] Line 2 Column 20 File C:/temp/fibonacci.cpp)IDENTIFIER
   |   |   ('=='@Cpp~GCC4=2918#4dc21a0^1#4dc2200:2[Keyword:0] Line 2 Column 22 File C:/temp/fibonacci.cpp)'=='
   |   |   (INT_LITERAL@Cpp~GCC4=2809#4dc21c0^1#4dc2200:3[1] Line 2 Column 25 File C:/temp/fibonacci.cpp)INT_LITERAL
   |   |  )equality_expression
   |   | )logical_or_expression

以上是由於 clang 中的「-emit-asm」標誌不再可用,如何使用替代方法從 C 程式碼中取得人類可讀的抽象語法樹(AST)?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn