Search code examples
cparsingprogramming-languages

Easily parsing a DSL with pure C


I'm working on a simple C application and i had the idea of creating a DSL to define some behaviors of the application. The idea is to create a very clean language, similar to Ruby, but that is actually run in C. All the functions are defined in C, the DSL is just... well, an alias to "hide" the verbose syntax of C.

I know lex and yacc but i think they are overkill for what i'm trying to do. Isn't there something simpler? I thought about regular expressions, but i would feel dirty doing that. Maybe theres something better!

An example:

if a = b
    myFunctionInC()

get 'mydata' then
    puts 'Hello!'

Easily translates to:

if (a == b) {
    myFunctionInC();
}

void get(string test)
{
    printf('Hello! %s', test);
}

Solution

  • Defining a good DSL syntax is hard; you have to understand what problems you want it to solve (and which ones you don't, otherwise it ends up with everything in it including the kitchen sink), and you have to figure out how to translate it to a target language (or interpret it on the fly).

    In both cases you need a parser, and interesting DSL syntax isn't generally practical to parse with regexes. So you need a real parser generator. If you are going to tackle something like Ruby, you'll need a strong parser generator!

    Then you need to capture the result of the parse, as some data structure, typically a tree. Then you need to analyze your DSL code for special cases, optimizations, and figure how to generate code. What this all means is that a parser typically is not enough. See my extended discussion on Life After Parsing.