Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在“flex & bison”中,有一个创建 AST 的高级计算器示例。计算器可以支持'sqrt'、'log'等内置函数。所有内置函数都是单参数。如果我想让计算器支持多参数的内置函数,比如' pow(a, b)',我该怎么做?
pow(a, b)
您的 AST 计算器中已经有双参数函数,例如 '+' 将 2 个表达式作为参数。你想要的只是做同样的事情,即你想要解析'fcn(expr_a,expr_b)'的'expr_a + expr_b'。请注意,这是针对您指定的内置函数。对于用户指定的功能,它变得更加毛茸茸,但那是在 p 上。书中61篇。
生成的 AST:
+ / \ / \ expr_a expr_b fcn / \ / \ expr_a expr_b
我希望这会有所帮助,即使我不提供源代码。我不是 Flex 和 Bison 方面的专家……