- 论坛徽章:
- 0
|
gcc使用yacc语法分析后,生成一棵语法树,gcc的树定义在tree.h,主要的数据结构有:
enum tree_code {
IDENTIFIER_NODE,
NEGATE_EXPR,
ADDR_EXPR,
TRUTH_NOT_EXPR,
CONVERT_EXPR,
REAL_CST,
INTEGER_CST,
LAST_AND_UNUSED_TREE_CODE
};
struct tree_common {
union tree_node * chain;
union tree_node * type;
enum tree_code code;
};
struct tree_int_cst {
char common[sizeof(struct tree_common)];
int value;
};
struct tree_real_cst {
char common[sizeof(struct tree_common)];
double value;
};
struct tree_identifier {
char common[sizeof(struct tree_common)];
char * name;
};
struct tree_vec {
char common[sizeof(struct tree_common)];
int length;
union tree_node * a[1];
};
struct tree_string {
char common[sizeof(struct tree_common)];
int length;
char * pointer;
};
struct tree_complex {
char common[sizeof(struct tree_common)];
union tree_node * real;
union tree_node * img;
};
struct tree_exp {
char common[sizeof(struct tree_common)];
int complexity;
union tree_node * operands[1];
};
struct tree_type {
char common[sizeof(struct tree_common)];
union tree_node * values;
union tree_node * size;
union tree_node * pointer_to;
union tree_node * reference_to;
};
struct tree_decl {
char common[sizeof(struct tree_common)];
char * filename;
int linenum;
union tree_node * size;
union tree_node * name;
union tree_node * context;
union tree_node * arguments;
union tree_node * result;
union tree_node * initial;
char * print_name;
};
struct tree_list {
char common[sizeof(struct tree_common)];
union tree_node * purpose;
union tree_node * value;
};
union tree_node {
struct tree_common common;
struct tree_int_cst int_cst;
struct tree_real_cst real_cst;
struct tree_string string;
struct tree_complex complexs;
struct tree_identifier identifier;
struct tree_decl decl;
struct tree_type type;
struct tree_list list;
struct tree_vec vec;
struct tree_exp exp;
};
typedef union tree_node * tree;
这些struct乍一看还是比较简单,但我在看那些构造语法树的函数(这些函数由yacc调用)时却很疑惑,比如构造一个表达式节点的函数build_unary_op,这个函数会调用很多其他函数,总之函数之间层层调用,我好不容易跟踪完全,但还是不清楚这个表达式节点构造完成后它对应的tree到底是什么样子。我想请教,当gcc编译程序时,有没有办法使用gdb来查看它生成的tree。 |
|