我想通过bison与节点树的公式制作AST:
以下是制作节点的函数和节点":
的结构typedef enum
{null, opera, var, val} NodeType;
typedef struct Node {
float val;
char * name; // val_name
char * opr; // operator
NodeType node_type;
struct Node * left;
struct Node * right;
} Node;
Node create_op_node(char * op){
Node node;
node.opr = op;
node.node_type = opera;
return node;
}
Node create_var_node(char * op){
Node node;
node.name = op;
node.node_type = var;
return node;
}
Node create_val_node(float value){
Node node;
node.val = value;
node.node_type = val;
return node;
}
贯穿树的功能:
void run_through_tree (Node *node){
switch (node->node_type){
case var:
printf("varable name:%s\n",node->name);
break;
case val:
printf("num value:%f\n",node->val);
break;
case opera:
printf("\noperator:%s\n",node->opr);
printf("LFS\n");
run_through_tree(node->left);
printf("RHS\n");
run_through_tree(node->right);
}
}
和语法规则以及创建和连接的方式如下:
S:S E T_NEWLINE {main_node = $ 2; run_through_tree(main_node);} //遍历节点树 | {} ;
E : E T_ADD E {Node * op_node = (Node *) malloc(sizeof(Node));*op_node = create_op_node("+"); connect_node(op_node,$1,$3);$$ = op_node;}
| E T_SUB E {Node * op_node = (Node *) malloc(sizeof(Node));*op_node = create_op_node("-"); connect_node(op_node,$1,$3);$$ = op_node;}
| E T_MUL E {Node * op_node = (Node *) malloc(sizeof(Node));*op_node = create_op_node("*"); connect_node(op_node,$1,$3);$$ = op_node;}
| E T_DIV E {Node * op_node = (Node *) malloc(sizeof(Node));*op_node = create_op_node("/"); connect_node(op_node,$1,$3);$$ = op_node;}
| T_SUB E %prec NEG {
Node * op_node = (Node *) malloc(sizeof(Node));
Node * zero_node = (Node *) malloc(sizeof(Node));
* zero_node = create_val_node(0.0);
* op_node = create_op_node("-");
connect_node(op_node,zero_node,$2);
$$ = op_node;}
| T_NUM {Node * val_node = (Node *) malloc(sizeof(Node));* val_node = create_val_node($1); $$ = val_node;}
| T_VAR {Node * var_node = (Node *) malloc(sizeof(Node));* var_node = create_var_node($1); $$ = var_node;}
| T_LPATH E T_RPATH {$$ = $2;}
;
解析没有变量的公式的结果是预期的: 2 *(3 + 2)
operator:*
LFS
num value:2.000000
RHS
operator:+
LFS
num value:3.000000
RHS
num value:2.000000
但是,使用变量解析公式是不正确的:
2 * a - a + 7 操作:+ LFS
operator:-
LFS
operator:*
LFS
num value:2.000000
RHS
varable name:a - a + 7
RHS
varable name:a + 7
RHS
num value:7.000000
应该是:
2 * a - a + 7 操作:+ LFS
operator:-
LFS
operator:*
LFS
num value:2.000000
RHS
varable name:a
RHS
varable name:a
RHS
num value:7.000000
我怎么能解决这个问题?