C++ 如何在Bison解析器上返回多个令牌?

C++ 如何在Bison解析器上返回多个令牌?,c++,parsing,lex,lexer,C++,Parsing,Lex,Lexer,我的语法是这样的 decl: attributes; {/*create an object here with the attributes $$?*/ } attributes: | att1 attributes {$$ = $1;} | att2 attributes {$$ = $1;} | attn attributes {$$ = $1;}; 我想获得用户输入的所有属性,其中一些属性是可选的,顺序无关紧要,类型不同您需要在$$

我的语法是这样的

decl:

   attributes;  {/*create an object here with the attributes $$?*/ }


attributes:



  |

  att1 attributes {$$ = $1;}

  |

  att2 attributes {$$ = $1;}

  |

  attn attributes {$$ = $1;};

我想获得用户输入的所有属性,其中一些属性是可选的,顺序无关紧要,类型不同

您需要在
$$
变量中返回一个结构;根据需要分配给其成员。下面是我手头的一些代码中的一个示例:

struct value_list {
    char *value;
    struct value_list *next;
};

# ...

valuelist:  TOK_VALUE
    {
        struct value_list *new = calloc(1, sizeof(struct value_list));
        if (!new)
            yyerror(_("Memory allocation error."));
        PDEBUG("Matched: value (%s)\n", $1);

        new->value = $1;
        new->next = NULL;
        $$ = new;
    }

valuelist:  valuelist TOK_VALUE
    {
        struct value_list *new = calloc(1, sizeof(struct value_list));
        if (!new)
            yyerror(_("Memory allocation error."));
        PDEBUG("Matched: value (%s)\n", $1);

        new->value = $2;
        new->next = $1;
        $$ = new;
    }
来自同一解析器的另一个示例,它比上述更简单的规则更努力地基于条目定制
struct
;缺点是它变得相当复杂,但优点是它更好地演示了单个对象的属性:

/* from a header file */
struct codomain {
    char *namespace;
    char *name;             /* codomain name */
    char *attachment;
    struct alt_name *altnames;
    void *xmatch;
    size_t xmatch_size;
    int xmatch_len;
    /* ... and it goes on like this ... */
}

# from the grammar:

profile_base: TOK_ID opt_id flags TOK_OPEN rules TOK_CLOSE
    {
        struct codomain *cod = $5;

        if (!cod) {
            yyerror(_("Memory allocation error."));
        }

        cod->name = $1;
        cod->attachment = $2;
        if ($2 && $2[0] != '/')
            /* we don't support variables as part of the profile
             * name or attachment atm
             */
            yyerror(_("Profile attachment must begin with a '/'."));
        cod->flags = $3;
        if (force_complain)
            cod->flags.complain = 1;

        post_process_nt_entries(cod);
        PDEBUG("%s: flags='%s%s'\n",
               $3,
               cod->flags.complain ? "complain, " : "",
               cod->flags.audit ? "audit" : "");

        $$ = cod;

    };
现在是语法

decl:
  attributes {
    objectStruct* myObject = $1;
    while(myObject!=NULL){
       if(myObject->name!=NULL){
          //do something with name
          myObject=myObject->next;
       }
       else if(myObject->value!=NULL){
          //do something with value
          myObject=myObject->next;
       }
       //...
     }
  };

attributes:
 {$$=NULL;} 
|
 NAME attributes {
   objectStruct* myObject = new objectStruct();
   myObject->name = $1;
   myObject->value = NULL;
   myObject->next = $2;
   $$=myObject;
}
|
 VALUE attributes {
   objectStruct* myObject = new objectStruct();
   myObject->value = $1;
   myObject->name= NULL;
   myObject->next = $2;
   $$=myObject;
}
//...
;

谢谢你的解决方案工作得很好,我在if语句方面遇到了问题,但那是另一个话题。我认为这是一个更通用的解决方案
decl:
  attributes {
    objectStruct* myObject = $1;
    while(myObject!=NULL){
       if(myObject->name!=NULL){
          //do something with name
          myObject=myObject->next;
       }
       else if(myObject->value!=NULL){
          //do something with value
          myObject=myObject->next;
       }
       //...
     }
  };

attributes:
 {$$=NULL;} 
|
 NAME attributes {
   objectStruct* myObject = new objectStruct();
   myObject->name = $1;
   myObject->value = NULL;
   myObject->next = $2;
   $$=myObject;
}
|
 VALUE attributes {
   objectStruct* myObject = new objectStruct();
   myObject->value = $1;
   myObject->name= NULL;
   myObject->next = $2;
   $$=myObject;
}
//...
;