Thursday, June 18, 2015

Ten Simple Rules for Creating Your Own Compiler on the CLR by Philip Laureano

Don’t we all secretly dream of constructing our own language?

The good news is that everything does not need to be written from scratch. The extremely quick run trough of a compiler is that the code is parsed into a parse tree which is converted into an abstract syntax tree which is compiled to an assembly.

Don’t write your own parser, use a prewritten one for instance ANTLR. When constructing the grammar rules it is important to avoid circular loops since that can cause an infinite parsing recursion. The parser that is generated is quite unreadable.

Going from the parse tree to the abstract syntax tree requires high usage of the visitor pattern. The abstract syntax tree then forms the heart of the compiler. Mr. Laureano has used LINQ expression trees to handle the abstract syntax tree.

Mr Laureano’s toy language, Not Quite Lisp™, is available on GitHub.

I wish I could say that this talk taught me all I needed to construct my own language but alas it turns out I’m either to dumb or the problem is more complex than an hour’s seminar can explain. It was fun to listen too though.

No comments:

Post a Comment