Kecise
Kecise

Reputation: 61

How to parse multi-argumemts built-in function in Bison

In 'flex & bison', there is an advanced calculator example that creates ASTs. The calculator can support built-in functions such as 'sqrt','log' and so on. All of the built-in functions are single parameter. If I want the calculator to support multi-parameter built-in functions, such as 'pow(a, b)' ,how should I do?

Upvotes: 1

Views: 190

Answers (1)

nic
nic

Reputation: 443

You already have bi-parameter functions in your AST calculator, for example '+' that takes 2 expressions as argument. What you want is simply to do the same thing, i.e. insted of 'expr_a + expr_b' you want to parse 'fcn(expr_a, expr_b)'. Note that this is for built-in functions, which you have specified. It gets hairier for user specified functions but that is on p. 61 in the book.

The resulting ASTs:

       +
      / \             
     /   \ 
expr_a    expr_b


      fcn
      / \             
     /   \ 
expr_a    expr_b

I hope this helps, even though i don't provide source code. I'm not really an expert on Flex & Bison...

Upvotes: 1

Related Questions