Home >Backend Development >C++ >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!