Transforming Syntax
Or: how to write the easy part of a compiler
A Stack Overflow question asked how to translate a VB-like conditional into a C-like ternary. The other answers suggested regexes or treating it as Perl code *shudder*. But transpiling code to another language can be done correctly.
This post aims to cover:
- parsing with Marpa::R2,
- AST manipulation,
- optimization passes,
- compilation, and
- Perl OO.
In the end, we'll be able to do all that in only 200 lines of code!
Since this post is already rather long, we will not discuss parsing theory. You are expected to be familiar with EBNF grammar notation.