user9362751
user9362751

Reputation:

Cannot print correct values in Bison code

So I'm trying to declare the syntax of an array (using Bison 3.6.2) as follows:

array: '[' array_vals ']'

array_vals:
| vals array_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. Thanks in advance!

Upvotes: 0

Views: 100

Answers (1)

OrenIshShalom
OrenIshShalom

Reputation: 7162

A general flex+bison advice: make them print everything they do. So the lexer would have lines like this:

">=" { cout << ">= on line: " << line << "\n"; return parser::make_GEQ(loc); }

And the parser will have lines like this:

vals:
val      { cout << "vals -> val\n";      $$ = {$1};                  } |
val vals { cout << "vals -> val vals\n"; $2.push_front($1); $$ = $2; } ;

Upvotes: 1

Related Questions