Home >Backend Development >C++ >How can I easily obtain a human-readable Abstract Syntax Tree (AST) from C code?

How can I easily obtain a human-readable Abstract Syntax Tree (AST) from C code?

Linda Hamilton
Linda HamiltonOriginal
2024-12-12 18:23:15900browse

How can I easily obtain a human-readable Abstract Syntax Tree (AST) from C   code?

get human readable AST from c code

Clang has previously offered this functionality with the -emit-asm flag, which has since been removed. The question is then raised: Is there currently a simple method for accomplishing this?

There are two instances in the solution, a straightforward one and a challenging one (C 's "most vexing parse"). The following is a sample Fibonacci program from http://talkbinary.com/programming/c/fibonacci-in-c/:

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;
}

With this as input, the DMS Software Reengineering Toolkit (with full C 11/17 parser) produces this AST:

(translation_unit
  (function_definition
    (function_head
      (simple_type_specifier
        ('int' 'int')
      )
      (noptr_declarator
        (IDENTIFIER 'fib')
        ('('
          (parameter_declaration
            (simple_type_specifier
              ('int' 'int')
            )
            (IDENTIFIER 'n')
          )
        )
        (function_qualifiers)
      )
    )
    (compound_statement
      ('{'
        (statement_seq
          (statement_seq
            (statement_seq
              (statement_seq
                (selection_statement
                  ('if'
                    ('('
                      (logical_or_expression
                        (equality_expression
                          (IDENTIFIER 'n')
                          ('==' 'int')
                          (INT_LITERAL '0')
                        )
                        ('||' '||')
                        (equality_expression
                          (IDENTIFIER 'n')
                          ('==' 'int')
                          (INT_LITERAL '1')
                        )
                      )
                    )
                    (jump_statement
                      ('return'
                        (IDENTIFIER 'n')
                        (';' 'int')
                      )
                    )
                  )
              )
              (simple_declaration
                (simple_type_specifier
                  ('int' 'int')
                )
                (init_declarator
                  (IDENTIFIER 'fib1')
                  (initializer
                    ('=' 'int')
                    (INT_LITERAL '0')
                  )
                )
                (';' 'int')
              )
            )
            (simple_declaration
              (simple_type_specifier
                ('int' 'int')
              )
              (init_declarator
                (IDENTIFIER 'fib2')
                (initializer
                  ('=' 'int')
                  (INT_LITERAL '1')
                )
              )
              (';' 'int')
            )
          )
          (simple_declaration
            (simple_type_specifier
              ('int' 'int')
            )
            (init_declarator
              (IDENTIFIER 'fib')
              (initializer
                ('=' 'int')
                (INT_LITERAL '0')
              )
            )
            (';' 'int')
          )
        )
        (iteration_statement
          ('for'
            ('('
              (simple_declaration
                (simple_type_specifier
                  ('int' 'int')
                )
                (init_declarator
                  (IDENTIFIER 'i')
                  (initializer
                    ('=' 'int')
                    (INT_LITERAL '2')
                  )
                )
                (';' 'int')
              )
              (relational_expression
                (IDENTIFIER 'i')
                ('<' '<')
                (IDENTIFIER 'n')
              )
              (';' 'int')
              (postfix_expression
                (IDENTIFIER 'i')
                ('++' 'inc')
              )
            )
            (compound_statement
              ('{'
                (statement_seq
                  (statement_seq
                    (expression_statement
                      (assignment_expression
                        (IDENTIFIER 'fib')
                        ('=' 'int')
                        (additive_expression
                          (IDENTIFIER 'fib1')
                          ('+')
                          (IDENTIFIER 'fib2')
                        )
                      )
                      (';' 'int')
                    )
                  )
                  (expression_statement
                    (assignment_expression
                      (IDENTIFIER 'fib1')
                      ('=' 'int')
                      (IDENTIFIER 'fib2')
                    )
                    (';' 'int')
                  )
                )
                (expression_statement
                  (assignment_expression
                    (IDENTIFIER 'fib2')
                    ('=' 'int')
                    (IDENTIFIER 'fib')
                  )
                  (';' 'int')
                )
              )
            )
          )
        )
        (jump_statement
          ('return'
            (IDENTIFIER 'fib')
            (';' 'int')
          )
        )
      )
    )
  )
)

The above is the detailed content of How can I easily obtain a human-readable Abstract Syntax Tree (AST) from C code?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn