有没有相互递归的例子? [英] Are there any example of Mutual recursion?
本文介绍了有没有相互递归的例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有任何递归函数调用另一个调用第一个函数的递归函数的例子?
Are there any examples for a recursive function that calls an other function which calls the first one too ?
示例:
function1()
{
//do something
function2();
//do something
}
function2()
{
//do something
function1();
//do something
}
推荐答案
相互递归在解析数学表达式(和其他语法)的代码中很常见.基于以下语法的递归下降解析器自然会包含相互递归:expression-terms-term-factor-primary-expression
.
Mutual recursion is common in code that parses mathematical expressions (and other grammars). A recursive descent parser based on the grammar below will naturally contain mutual recursion: expression-terms-term-factor-primary-expression
.
expression
+ terms
- terms
terms
terms
term + terms
term - terms
term
factor
factor * term
factor / term
factor
primary
primary ^ factor
primary
( expression )
number
name
name ( expression )
这篇关于有没有相互递归的例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文