2
$\begingroup$

I am writing a compiler in CPP using Flex and Bison and having trouble understanding passing values up the syntax tree in bison. I have grammar like this below, 'exp' would evaluate to any number.

args_list: exp | args_list comma exp; 
array_list: datatype identifier '[' number']' equal '{' args_list'}'{$$ = new array_node($1, $2, $4, $8);}

I wanted to pass args_list to the array_node but not getting how to pass? As you can see user can initialize array with any length but how to pass this list to CPP node? for example - int a[4] = {1,2,3,4} ; I want to pass something like

new array_node(datatype, a, number, args_list) 

in which I am not getting how to pass args_list so that I can have array type. Or please suggest me another way to pass the array list to the cpp file. or how to pass whole array to cpp file from yacc?

$\endgroup$
1
0
$\begingroup$

args_list’s type can be vector. Then, instead of calling to new array_node you can have a factory method (array_node::factory) which parses the type, casts the args and returns the array_node object

$\endgroup$
2
  • $\begingroup$ I understood that my array_node constructor will have one formal parameter of vector type, but from yacc file how to send these numbers? like {2, 4, 5, 7} as user may enter as many numbers as he want. and from yacc will this be considered as string? or I can not send each numbers position with $1, $2 and so one $\endgroup$ – Prasanna Oct 12 '20 at 12:14
  • $\begingroup$ You can store them in a vector of strings and later convert them or you can put a mid rule action after the type declaration and save the type in a global variable or save it as one of the array's attributes and args_list will look up for it (however you will need to change your rules) $\endgroup$ – Hadar Shavit Oct 12 '20 at 18:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.