How To Make A Language: Parsing
CSharpIsGud

The last tutorial didn't really give an in depth explanation as to what everything was doing aside from some comments, however for parsing I feel I should explain how it works.

Markdown was really weird on this post.

We are going to be using a recursive descent parser, written all by hand and with no dependencies.

Lets start with an expression 8 + 5 * 4 / 2

The first thing we are going to do is tokenize it, so this expression will tokenize to this

Im going to do a little experiment here, and if it works you should be able to read the above stream of tokens just like an rdp parser. When you see Add(), Sub(), Mul(), Div() and Literal() in the text below jump to the corresponding label!

Start with Add.

Add:
Left = Sub()

Sub:
Left = Mul()

Mul:
Left = Div()

Div:
Left = Literal()

Literal:
Return the current token and advance to the next one

Now, if you went through everything above and kept track of where you were in the sequence you will notice that you end with a tree that looks roughly like this:

You are viewing a single comment. View All
[deleted]

@CSharpIsGud Well, it's always better to look at all of your options, actually, I take that back. Humans suck at making decisions with too many choices.