Search code examples
compiler-constructionlanguage-agnosticprogramming-languageslanguage-featureslanguage-translation

Implementing sugar syntax


At which stage of compiling (scanning, parsing, some part of semantic analysis, or maybe even code generation) is syntax sugar usually handled, how and what are the reasons for it?


Solution

  • The easiest place to do it is often at the point of building the AST -- i.e., during the parse itself -- because that makes the desugaring apparent and verifiable, and thereafter the sugar does not need to be dealt with in any way during semantic analysis and code generation.

    That's a very general guideline and there are innumerable circumstances in which you might choose a different strategy.