The following is a guide for how to get started with programming side projects. If something's missing and you know about it, feel free to submit a pull request and add it.
Building languages is not as hard as it may appear! In general, the key concepts are:
- Grammars, or the formal structure of a language.
- Lexing, or the conversion of text into abstract "tokens" that can be used in the grammar. For example, most languages convert the
+
into aPLUS
token. - Parsing, or the conversion of "tokens" (pieces of a language) into an Abstract Syntax Tree.
A good first project is to build a calculator that can compute things like this:
1 + 2 * 5
You should probably write this in EBNF form. It doesn't need to have the exact same syntax, but it's useful to understand the conventions.
To get you started, here's a partial grammar for the calculator:
add: add '+' add | NUM.
NUM: [0-9].
Notice that grammars are written as recursive "rules". In the example above, the string 1 + 2
would enter the add
rule, then recurse till it matched the NUM
.