Reputation: 17
I am working with flex/bison to create an interpreter for a class. The problem that I am encountering seems to be related to using/converting a dynamic vector. I am trying to create a function to evaluate fold operations, but I hit a roadblock. The list type is specified as a dynamic vector to allow for any number of arguments to be passed to it.
%union {
CharPtr iden;
Operators oper;
double value;
vector<double>* list;
}
%type <list> list expressions list_choice
The bison direction is to pass the direction of the fold, the operator for the fold, and the dynamic vector to the function evaulateFold()
.
FOLD direction operator list_choice ENDFOLD SEMICOLON {$$ = evaluateFold($2, $3, $4);}
Once I get it to this point, I cannot do anything with the dynamic array. I tried to copy it to a regular array using for loops but it says that there is no start point reference for the dynamic array this range-based 'for' statement requires a suitable "begin" function and none was foundC/C++(2291)
. I tried passing the array to evaluateFold()
using ,const std::vector<double>&
values, but then it says that there is no way to convert std::vector<double>*
to std::vector<double>&``no suitable constructor exists to convert from "std::vector<double, std::allocator<double>> *" to "std::vector<double, std::allocator<double>> &"C/C++(415)
. How am I supposed to be able to use this array for the function?
double evaluateFold(Operators direction, Operators action, vector<double>* values){
double result = 0;
switch(direction){
case LFOLD:
switch(action){
case SUBTRACT:
subFromLeft(values);
for (auto i : values)
cout << i << endl;
break;
}
case RFOLD:
switch(action){
case SUBTRACT:
for (auto i : values)
cout << i << endl;
break;
}
break;
}
return result;
}
I was trying to use a variadic function for the actual fold function, but I cannot even get the vector to allow me to use it.
template <typename ...Args>
auto subFromLeft(Args ...args)
{
return (... - args);
}
I did just pass the dynamic array all the way using vector<double>* values
, but then it said there was no way to resolve vector<double>* values
to a double
, which would be the result I need from the operation.
Here is another way I tried to complete the fold operation but again, it would not let me use the const std::vector<double>& values
reference because it said there is no viable conversion.
double subFromLeft(const std::vector<double>& values) {
if (values.empty()) return 0; // Handle empty vector
double result = values[0];
for (size_t i = 1; i < values.size(); i++) {
result -= values[i];
}
return result;
}
If anybody could help me resolve this, or at least point me in the right direction, that would be awesome. It is more a question of C++ than flex/bison, but maybe there is some trick in flex/bison that I am not aware of that will help me.
Make generates this:
parser.y: In function ‘int yyparse()’: parser.y:127:146: error: invalid initialization of reference of type ‘std::vector<double>&’ from expression of type ‘std::vector<double>*’
In file included from parser.y:21: values.h:18:76: note: in passing argument 3 of ‘double evaluateFold(Operators, Operators, std::vector<double>&)’
Below is a sample of what the fold function would interpret/evaluate.
// Test Left Fold
function main returns integer;
begin
fold left - (3, 2, 1) endfold;
end;
Upvotes: -1
Views: 65
Reputation: 17
@Yksisarvinen originally commented it but he erased the comment for some reason. Using (*values)
to dereference the dynamic array vector<double>* values
allowed me to send it to the other function easily and get the result. A couple of others answered but @Yksisarvinen was the first to mention it.
Upvotes: 0