Matthias
Matthias

Reputation: 3556

bison syntax for token declaration

I have to read a bison grammar file and do not understand the following declaration:

The grammer has a union declaration

%union {
   int i;
   char *s;
}

The token declaration looks like this:

%token
      TOK0   TOK1 TOK2
      TOK3   TOK4 TOK5
      TOK6

      TOK7

%token <s> TOK8
%token <i> TOK9

My expectation is that because of the union declaration a type must be provided for every token declaration. However TOK0 to TOK7 do not have a type provided. Also I was wondering about the tabular layout of the declaration for TOK0 to TOK7. Is any special meaning given to this layout? I was only finding this source of information about the token declaration ( https://www.gnu.org/software/bison/manual/html_node/Token-Decl.html#Token-Decl ) and it seems it does not cover my usecase.

Upvotes: 0

Views: 322

Answers (1)

Abhishek Kumar
Abhishek Kumar

Reputation: 769

The tabular layout has no meaning AFAIK. You don't need to assign types to tokens if you don't need their type. Almost always tokens like open_bracket, close_bracket or other things whose value you never need are left type less. You can specify open_bracket as <s> but it is not required and for readability I won't do that.

Upvotes: 1

Related Questions