Showing posts from December, 2015

Parsing and code generation

In this post we're going to take a close look at how to put together a basic parser and how to generate real code from the Abstract Syntax Tree (AST).

# Overview A couple of posts ago, we talked about the basics behind the creation of a programming language, but we didn't look at any real code how to actually do that. This time, we're going to get our hands dirty and write a real parser and a code generator for a very small language subset.
# The parser  For simplicity, our parser will be able to understand only variable declarations, numbers, arithmetic operators, nested expressions and the say keyword which will print something to the screen.
The first thing before writing a parser, it's the design of the AST that it should produce. The AST have a recursive definition with sub-branches that resembles the top of the tree. A very simple design for the AST, is the following: mainthe top of the ASTselfan object or an expressioncalla list of calls for the self objectopan …