Search code examples
bashcompiler-construction

Compiler for Bash


I am taking a Compiler Design course in my undergraduate studies. As a part of the learning process, I'd have to develop the compiler for a language.

Can a compiler be written for Bash? Would it be more difficult than designing a compiler for a regular programming language, like C/C++ and thus outright inconceivable, at least for a newbie?


Solution

  • Can a compiler be written for Bash?

    Yes. (Existence proof - shc.)

    If yes, how?

    That's the hard part.

    POSIX shell languages are very different to typical programming languages because of the effects of things like backticks, variable substitution, quoting, and so on.

    You could ignore this and implement a "bash like" language, either leaving out the difficult features, or treating them in a way that doesn't conform to POSIX behavior.

    Then ... there is the problem of how to generate something that is executable. Again, that is possible (see above), but if your aim is to be faster than a regular shell then you need to do things like emulating the behavior of common Linux commands in the compiled code. That is a huge task.


    I'm not saying this is a bad project, but you will need to do a lot of work, including:

    • finding, reading and (fully) understanding the POSIX shell specs
    • researching how to implement a parser that deals with POSIX idiosyncracies
    • figuring out which linux commands need to be implemented directly, and
    • figuring out how to deal with the ones that you don't; e.g. all the complexity of pipelines.