Compilers 102 - Parser
Tags: llvm, compiler, parser
Published: 2018-01-22 17:56:47

---
title: Compilers 102 - Parser
series: Compilers
published: true
description: Learn how the parser works in a compiler.
tags: llvm, compiler, parser
cover_image: https://thepracticaldev.s3.amazonaws.com/i/6qun34secqb1tsrp8yx7.png
---

These compiler posts will all be at a high-level and are based on the LLVM and Compiler session from the [Xojo Developer Conference 2016](http://www.xojo.com/xdc). None of these posts are going to teach you how to write a compiler. The goal of these posts is for you to have a basic understanding of the components of a compiler and how they all work together to create a native app.

This is the second post in an ongoing series on compilers. I recommend that you first read [Compilers 101 – Overview and Lexer](https://dev.to/lefebvre/compilers-101---overview-and-lexer-3i0m) before continuing.

After the Lexer has converted your source code to tokens, it sends them to the Parser. The job of the Parser is to turn these tokens into abstract syntax trees, which are representations of the source code and its meaning.

For reference, this is the simple source code we are “compiling”as we go through the parts of the compiler:

    sum = 3.14 + 2 * 4

The lexer has converted this to a stream of tokens which are now sent to the Parser to process. The tokens are:

1. Type: identifier
 * value: sum
 * start: 0
 * length: 3
1. Type: equals or assigns
 * value: n/a
 * start: 4
 * length: 1
1. Type: double
 * value: 3.14
 * start: 6
 * length: 4
1. Type: plus
 * value: n/a
 * start: 11
 * length: 1
1. Type: integer
 * value: 2
 * start: 15
 * length: 1
1. Type: multiply
 * value: n/a
 * start: 15
 * length: 1
1. Type: integer
 * value: 4
 * start: 17
 * length: 1

## Parser
To see how this works, we’ll go through the tokens and create the syntax tree.

The first token is the identifier, which the parser knows is actually a variable. So it becomes the first node of the tree:

![](https://blog.xojo.com/wp-content/uploads/2017/05/2017-05-17_09-42-30-1.png)

The next token is equals or assigns. The parser knows things about this such as that it is an assignment and that assignment is a binary operator that has two operands, one on the left and one on the right. The variable from above is the left value, so it gets moved to the left side of the Assignment node that is now added to the tree to look like this:

![](https://blog.xojo.com/wp-content/uploads/2017/05/2017-05-17_09-42-30.png)

Continuing, the double token is next with value 3.14. This is the right value for the assignment:

![](https://blog.xojo.com/wp-content/uploads/2017/05/2017-05-17_09-42-31.png)

Moving along, the plus token is next. The parser knows this is the addition operator (BinaryOperator+) that takes two values (and is also left associative). This means that the addition is added to the tree with the double as its left value:

![](https://blog.xojo.com/wp-content/uploads/2017/05/2017-05-17_09-42-32.png)

After the plus token, an integer is next and this becomes the right value for the BinaryOperator+ node:

![](https://blog.xojo.com/wp-content/uploads/2017/05/2017-05-17_09-42-33.png)

Next is the multiply token, another binary operator that is left associative. So it gets the integer as its left value:

![](https://blog.xojo.com/wp-content/uploads/2017/05/2017-05-17_09-42-34.png)

The last token is another integer which becomes the right value for the multiplication:

![](https://blog.xojo.com/wp-content/uploads/2017/05/2017-05-17_14-33-23.png)

And that is the final abstract syntax tree for our simple line of code. The Parser has done its work and has now created a tree that no longer represents the exact source code but is an idealized representation of what the user wrote.

This tree is then provided to the next component, the [Semantic Analyzer](https://dev.to/lefebvre/compilers-103--semantic-analyzer-540k).