Reading Generic Configuration Files

We use configuration files to set initial parameters and create initial data structures for applications. That means humans have to read and write those files. The problem is that XML is a generic data format, not a specialized DSL for initializing applications. Being good DSL-o-philes, we can do a lot better than XML for this niche.

What we want is a simple C-like notation that lets us build a list of objects with properties such as strings, ints, lists, and references to other configuration objects. Here’s a simple example:

 
Site jguru {
 
port = 80;
 
answers = "www.jguru.com";
 
aliases = ["jguru.com", "www.magelang.com"];
 
}

To begin, we need Pattern 2, LL(1) Recursive-Descent Lexer and Pattern 3,

Get Language Implementation Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.