Re: Cannot print correct values in Bison code

Martin Alexander Neumann <[email protected]>
Newsgroups gmane.comp.parsers.bison.general
Message-ID <[email protected]>
Good morning Christian,

during reductions Bison does "hand over" a single semantic value only
from the right side of your productions to the left side of the
productions. By default it is the first, i.e. "$$ = $1" in Bison terms.

I suppose your productions of array_vals look like the following:

--------------------------------------------------------------
array_vals  : array_vals COMMA vals { $$ = $1; }
            | vals;  { $$ = $1; }
--------------------------------------------------------------

If these rules match yours ones, then in your example,

1. "10" is reduced to "vals",
2. "vals" is reduced to "array_vals",
3. "20 is reduced to "vals",
3. "array_vals COMMA vals" is reduced to "array_vals"
...

Therefore, your example is constantly handing over "10" only as semantic
value.

If you want to hand over more semantic values during a reduction, you
have to extend this code yourself, e.g.

--------------------------------------------------------------
array_vals  : array_vals COMMA vals { $$ = concatenate*($1, $3); }
            | vals;  { $$ = $1; }
--------------------------------------------------------------

* Some self-made function that somehow can join two semantic values into
one. It could for example save these two values in a linked list.

Greetings,
Alex
On 30.05.20 02:59, M. P. D Productions wrote:
> *BISON VERSION: 3.6.2*
> 
> Hey! I'm very new to Flex/Bison, so please go easy on me. I am trying to
> declare the syntax of an "array" as follows:
> *array: '[' array_vals ']'*
> 
> *array_vals:*
> *| vals*
> 
> *vals: STRING //String values defined in my lexer*
> *| FLOAT //Float values defined in my lexer*
> *| INT //Integer values defined in my lexer*
> 
> I feel like this is pretty self explanatory, but there's just *one *problem:
> Wherever I print the "array_vals", using this:  *array: '[' array_vals ']'
> { cout << $2 << endl; }* I get only the first value of the array in my
> source code, so if I have this in my source file: "[10, 20, 30, 40]" then
> it only prints "10." I've tried printing the "vals" in "array_vals", and
> it's even weirder and inconsistent. I know that this isn't a bug since I
> went from Bison version 3.5.1 to 3.6.2 and there was no difference. If
> someone knows, then thank you, but once again, I am new to this, so please
> go easy on me.
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.