0

I have a simple config file as shown below

[main]
e_type=0x1b

In my lex file (test.l) when I encounter token e_type I call the following function and retunr T_E_TYPE token.

void tag ()  {
    yylval.str = malloc(strlen(yytext)+1);
    strcpy (yylval.str, yytext);
}

In the parser file (test.y) I have declared the union as follows.

%union {
   char *str;
   unsigned int n;
}

I am able to recognize [main] and e_type but If I try to print yylval.str from test.y file I get a segmentation fault.

How can I access the values I passed from lexer in the test.y file.

1 Answer 1

0

The value of yylval corresponding to each token has been pushed onto the stack, and is available in the reduction rule as $n for some n (the symbols on the right hand side are numbered in order). You must declare the type-tag of each token so that bison knows which member of the union to use.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.