Search code examples
compiler-constructionbison

Looping over $ elements in bison


I am new to bison so this might sound trivial. I have a production suppose:

A: B C D E { $$ = $1 + $2 + $3 + $4 }

Now, is it possible to write a loop for $1 + $2 ... because I might have a large number of non terminals on the RHS and I don't want to manually write it down like this. I tried doing $i where i was a loop variable but that didn't work out.


Solution

  • No, you can't. As far as bison is concerned, $$, $1, $2, etc. are identifiers, and they each have a type. So it would be like asking for a shortcut to a write var1 + var2 + var3... as a loop.