|How To Create Pragmatic, Lightweight Languages|
Page 1 of 3
Author: Federico Tomassetti
A book that makes language design and compilers construction accessible.
At last, a guide that makes creating a language with its associated baggage of lexers, parsers and compilers, accessible to mere mortals, rather to a group of a few hardcore eclectics as it stood until now.
The first thing that catches the eye, is the subtitle:
The unix philosophy applied to language design, for GPLs and DSLs"
What is meant by "unix philosophy" ?. It's taking simple, high quality components and combining them together in smart ways to obtain a complex result; the exact approach the book adopts.
I'm getting ahead here, but a first sample of this philosophy becomes apparent at the beginnings of Chapter 5 where the Parser treats and calls the Lexer like unix's pipes as in lexer|parser. Until the end of the book, this pipeline is going to become larger, like a chain, due to the amount of components that end up interacting together.
The book opens by putting things into perspective in Chapter 1: Motivation: why do you want to build language tools?
There are two different scenarios in which you may want to do that:
1. you want to create a new language: maybe a general purpose language (GPL), maybe a domain specific language (DSL). In any case you may want to build some support for this language of yours. Maybe you want to generate C and compile the generated code, maybe you want to interpret it. Maybe you want to build a compiler or a simulator for your language. Or you want to do all of this stuff and more.
It's also important to note that the author is not using any GUIs or IDEs that generate code behind the scenes, but rather hand-codes everything in Kotlin, using Gradle to build and run it.
And why Kotlin? Because it is very concise, reduces the boilerplate, is well supported and reasonably clear. No worries though as the ideas discussed throughout the book should be applicable to any language.
The first few pages close with a brief rundown on the General Purpose Languages (GPL) vs the Domain Specific ones (DSL), or rather when to use which.
Chapter 2: The general plan continues with a more detailed look on what we're going to build:
that is, build, not just use...
At this point the author points out that the underlying theory of building a grammar for your language, poses the very first barrier which puts people off progressing any further, thus he keeps theory down to a minimum, instead adopting a "practical to the bone" approach. As he puts it "there is no better way to learn design principles than by building things".
Quickly then, Chapter 3: The example languages we are going to build, provides a high level overview of the toy languages we're going to work on - MiniCalc, a language to show how to work with expressions. At its core it will support:
and has the following particularities:
(click on book cover for details on Lean Pub site)
Part I: The Basics
At this point the intro ends and we reach the main material, beginnining with 4: Writing a lexer. The lexer is the piece of code that takes a textual document and breaks it down into elements called tokens, which essentially are the portions of text with a specific role. Tokens like that could be numeric literals, string literals, comments and keywords.
A lexer's purpose can be clearly observed in the context of the syntax highlighting built into IDE's and text editors;do you want to show the keywords in green? you first need to recognize which parts are the keywords.
To build our lexer we are going to use ANTLR, a very mature tool for writing lexers and parsers. Indeed we will use ANTLR to generate both our lexer and our parser, as typically a lexer and a parser need to work together, therefore it makes sense that just one tool generates both of them.
In addition to that, ANTLR 4 makes it easy to write simple grammars as it solves the left recursive definition for you, so you do not have to write many intermediate node types for specifying precedence rules for your expressions ... More on this when we look into the parser,
However I could not find that explanation; as already mentioned, it is very light on theory.
The complete lexer grammar for MiniCalc is then provided, part of which I relay below, subsequently dissected and explained line by line.
and so on.
|Last Updated ( Tuesday, 08 August 2017 )|