1
votes

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?

1
If you're happy with the answer it would be cool if you accepted it so future visitors can see it's fairly solved ;)nic

1 Answers

1
votes

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...