zl2003cn
zl2003cn

Reputation: 485

how to use yylval with union in yacc

I was confused about how to use yylval defined in %union{}, when yylval is a int, I can use $1 and $2 reference to tokens, but with union type I don't know how to do. I got definition here:

%union {
    int intval;
    double floatval;
    char *strval;
    int subtok;
}

For example, I have a rule when yylval is string like this

line: SELECT items'\n' { printf("select item %s\n", $2); };

how to change it to strval instead?

Upvotes: 1

Views: 1157

Answers (1)

nameistaken1
nameistaken1

Reputation: 110

Either declare the type of each token when declaring the token...

%token <strval> items

Declare the type after declaring the token...

%type <strval> items

Or specify the type when grabbing the value...

$<strval>2

Upvotes: 3

Related Questions