recursion

Are there any examples of mutual recursion?


Are there any examples for a recursive function that calls an other function which calls the first one too ?

Example :

function1()
{    
    //do something 
    function2();
    //do something
}

function2()
{
    //do something 
    function1();
    //do something
}

Solution

  • 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 )