mljli
mljli

Reputation: 603

use C macro in Bison semantic actions

I want to create some macros for typing save which look like:

#define SET_LOCATION(n) \
    { @$ = @n; \
      ...}

But it doesn't make, the error message is '$' is not declared in this scope. Why?

Upvotes: 1

Views: 488

Answers (2)

Chris Dodd
Chris Dodd

Reputation: 126536

Bison macros ($ and @) are only expanding directly in actions -- and this expansion happens as bison generates C code. C macros are expanded in later, when your C compiler runs on the output of bison. So if you want to use the bison macros in a C macro, you need to ensure that they appear directly in the action, generally as an argument to the macro:

#define SET_LOCATION(DEST, SRC) \
       { DEST = SRC; \
         .... }

used as

SET_LOCATION(@$, @n)

Upvotes: 2

mljli
mljli

Reputation: 603

I just found the answer after checking the output of Bison. When you write @$ directly in the semantic actions, it gets replaced with (yyloc) by Bison. But it is not replaced in the case of using a C macro. Bison doesn't expand C macros. They're expanded by GCC afterwards which will certainly leads to a undeclared '$' error.

Upvotes: 1

Related Questions