Leif
Leif

Reputation: 177

Adding arguments to a vector (Yacc/Bison)

I'm wanting to add objects to a vector representing an argument list in Yacc/Bison. I have the following grammar rule:

argument_list:  expression 
                {
                 //push back object representing expression onto arglist vector
                }

                |
                expression ',' argument_list
                {
                 //same here
                };

I'm not sure how to go about this, since you can't declare argument_list as a vector in type declarations. I want to pass this vector into a method which creates an AST node representing a method, via a rule such as this:

arg_method_invocation: IDENT PERIOD IDENT LPAR argument_list RPAR 
              { 
              $$=new MethodCallStatement(yylineno,new MethodCallExpression(yylineno,$1,$3, $5 ));
                     if ($$==NULL)
                     fatal("method stmt: ", "error method stmt call");
              }

Is this even possible? I'm new to compiler design and this approach may not be do-able. Any suggestions are welcome.

Upvotes: 0

Views: 965

Answers (1)

user207421
user207421

Reputation: 310850

Just make it left-recursive:

argument_list:  expression 
            {
              $$ = new vector();
              $$.add($1); // or whatever the API is
            }

            |
            argument_list ',' expression
            {
             $1.add($3); // ditto
            };

I don't see why you can't declare argument_list as a vector. I assume you are referring to the %type and %union directives here? If you aren't, that's how you do it.

Upvotes: 0

Related Questions