views:

168

answers:

3

Has anyone out there actually used inversion of control containers within compiler implementations yet? I know that by design, compilers need to be very fast, but I've always been curious about how IoC/DI could affect the construction of a programming language--hot-swappable syntaxes, anyone?

A: 

LR(k) grammars typically use a generic parser system driven by tables (action-goto/shift-reduce tables), so you use a table generator tool which produces these tables and feed them to the generic parser system which then can parse your input using the tables. In general these parser systems then signal you that a non-terminal has been reduced. See for example the GoldParser system which is free.

Frans Bouma
Actually Frans, I was referring to the design of a compiler as a whole, not just the parser frontend. It would be nice to have a compiler that would let you swap its syntax 'on the fly', or do other interesting things like change its code generation backend from IL to JVM bytecode..
plaureano
A: 

I wouldn't really call it inversion of control because it's natural for compilers. They are usually a series of passes that transform code in the input language to code in the output language. You can, of course, swap in a different pass (for example, gcc compiles multiple languages by using a different frontend).

Jules
+1  A: 

Lisp-style languages often do this. Reader macros are pieces of user-written code which extend the reader (and hence, the syntax) of a language. Plain-old macros are pieces of user-written code which also extend the language.

The entire syntaxes aren't hot-swappable, but certain pieces are extendable in various ways.

All this isn't a new idea. Before it was deemed worthy of a three-letter acronym, IoC was known as "late binding", and pretty agreed on as a Good Idea.

Andrey Fedorov