compiler-constructionsemantics

How do I check whether all code paths return a value


I am writing a compiler for an embedded scripting language that will run in my application. I am currently working on the semantic analysis portion of the compiler. I would like to know, in theory, how to check that all code paths in a given script will return a value.

Doing a Google search on how to check if all code paths return a value yields only results about people seeing the error in their own code when not all code paths return a value (mostly SO questions), so I have been unable to find sources that explain how the actual checking can be done. Can anyone point me in the right direction?

NOTE: I am specifically looking for an authoritative source outlining a rigorous algorithm, if at all possible.


Solution

  • You can do this with a recursive walk over the AST. For example:

    Hope this helps!