Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bison 野牛%联合使用_Bison - Fatal编程技术网

Bison 野牛%联合使用

Bison 野牛%联合使用,bison,Bison,因此,我在.y语法中设置了一个AST,下面是如何使用它的示例片段: typedef struct node { struct node *left; struct node *right; char *token; } node; ... exp : term {$$ = $1;} | exp PLUS term {$$ = mknode($1, $3, "+");} | exp MINUS term {

因此,我在.y语法中设置了一个AST,下面是如何使用它的示例片段:

typedef struct node
{
  struct node *left;
  struct node *right;
  char *token;
} node;

...

exp    : term             {$$ = $1;}
        | exp PLUS term     {$$ = mknode($1, $3, "+");}
        | exp MINUS term    {$$ = mknode($1, $3, "-");}
        ;
...
node *mknode(node *left, node *right, char *token)
{
  /* malloc the node */
  node *newnode = (node *)malloc(sizeof(node));
  char *newstr = (char *)malloc(strlen(token)+1);
  strcpy(newstr, token);
  newnode->left = left;
  newnode->right = right;
  newnode->token = newstr;
  return(newnode);
 }
我的问题是,像这样设置,是否仍然可以对其他值使用%union? i、 e

%union{
int值;
}
出口:期限{
$$ = $1;
$$ = $1;
}
|exp加术语{
如果($1==3){
$$=mknode($1,$3,“+”);
$$ = $1;
}
}
等

或者设置$$会覆盖使用联合变量的功能吗?

您的示例显示将两个值分配给同一联合的不同元素,因此第二个值将覆盖第一个值
$
是当前操作正在生成的联合值堆栈槽,而
$1
$3
是与那些非终端相对应的联合值槽,这些非终端在这些规则的操作中写入了
$

使用
只会覆盖要使用的联合中的默认字段,该字段通常由代码中的
%type
声明决定(您不显示该声明)。因此,如果代码中有
%type exp
,则
$$
$$$
引用相同的内容--
yyval
变量的
字段,该字段将在操作运行后推送到bison值堆栈上

如果要在bison值堆栈上为同一个非终端拥有多个值,请在union中使用结构:

%union {
    struct {
        node *node;
        int value;
    } node_and_value;
    :
}

%type <node_and_value> exp term

%%

exp    : term             {
                             $$ = $1; /* copy the whole struct */
                             /* this is the default action, so could be left off */
                          }
        | exp PLUS term   {
                              if($1.value == $3.value) {
                                  $$.node = mknode($1.node, $3.node, "+");
                                  $$.value = $1.value;
                              } else {
                                  ...
                              }
                          }
%union{
结构{
节点*节点;
int值;
}节点_和_值;
:
}
%类型exp术语
%%
出口:期限{
$$=$1;/*复制整个结构*/
/*这是默认操作,因此可以省略*/
}
|exp加术语{
如果($1.value==3.value){
$$.node=mknode($1.node,$3.node,“+”);
$$.value=$1.value;
}否则{
...
}
}
$$
是联合变量。。。
%union {
    struct {
        node *node;
        int value;
    } node_and_value;
    :
}

%type <node_and_value> exp term

%%

exp    : term             {
                             $$ = $1; /* copy the whole struct */
                             /* this is the default action, so could be left off */
                          }
        | exp PLUS term   {
                              if($1.value == $3.value) {
                                  $$.node = mknode($1.node, $3.node, "+");
                                  $$.value = $1.value;
                              } else {
                                  ...
                              }
                          }