Recognizing tail-recursive functions with Flex + Bison and converting code to iterative form

I am writing a calculator with the ability to accept new function definitions. Knowing the need for newbies to try recursive functions such as Fibonacci, I would like my calculator to be able to recognize tail-recursive functions with Flex + Bison and convert the code to iterative form. I use Flex and Bison to do the job. If you have any hints or ideas, I welcome them warmly. Thank!

EDIT:

Don't worry about C or C ++ outputs from Flex and Bison. Mostly I want an idea or a hint. Thank.

+3
source share
3

, ...

def func(args)
   #stuff
return func(otherargs)

, AST - return → func → otherargs, whatevers. , F, F - , PUSH ARGS, GOTO F, . .

, , , , . , .

, , . .

+1

, lexer, , , . - :

func f( a ) 
    if ( a == 0 )  
       return a
    return f( a - 1 )

C / . , , , - , , .

+2

, : , . , .

/ (.. Java , ), .

+1
source

Source: https://habr.com/ru/post/1741660/


All Articles