Hohenheimsenberg
Hohenheimsenberg

Reputation: 980

How to return more than one token on Bison parser?

My grammar is something like this

decl:

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


attributes:



  |

  att1 attributes {$$ = $1;}

  |

  att2 attributes {$$ = $1;}

  |

  attn attributes {$$ = $1;};

I want to get all the attributes entered by the user, some of them are optional and the order doesn't matter, types are different

Upvotes: 2

Views: 482

Answers (2)

Hohenheimsenberg
Hohenheimsenberg

Reputation: 980

struct objectStruct{
  char* name;
  int value;
  objectStruct next;
  //...
}

now the grammar

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;
}
//...
;

Upvotes: 1

sarnold
sarnold

Reputation: 104020

You need to return a structure in your $$ variable; assign to its members as you need to. Here's an example from some code I have handy:

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;
    }

Another example from the same parser, which puts more effort into customizing a struct based on an entry than the above simpler rules; the downside is that this is getting quite complex, but the upside is that this is a better demonstration of attributes of a single object:

/* 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;

    };

Upvotes: 1

Related Questions