In this post, I’ll show you the different types of parser nodes. If you remember from one of the first posts I made in this series, the parser analyzes the tokens it gets from the tokenizer and turns it into structured trees. The nodes in this tree represent the different constructs (additions, multiplications, comparisons, …).
x := …
if … then … else … end if
while … do … end while
for i := … to … by … do … end for
x + y
x - y
x * y
x / y
x and y
x or y
x < y or
x > y or
x == y or …
A statement is an instruction that does not return a value. An expression is an instruction that does return a value. A unary expression has an operator and a single child expression. A binary expression has an operator and 2 child expressions.
You might have noticed that I defined both a FunctionCall and a FunctionCallStatement. The difference is that a FunctionCall is an expression that returns the value of the function call, and a FunctionCallStatement is a statement that ignores the value of the function call.
The basic types look like this:
public abstract class ParserNode
public abstract class Statement : ParserNode
public abstract class Expression : ParserNode
As you can see, they don’t contain any code. Because I’m just explaining how parsers work, they don’t have to contain code. You should add code if you use these classes to build an interpreter or a compiler. For an interpreter, the Statement class could have a method Execute to execute the statement, and the Expression class could have a method Evaluate to evaluate the expression and return the result.
There’s no point in showing the source code in this blog post for all the classes, so you can download them here. Just create a new folder ParserNodes in the TC.Adl project and include the downloaded C# files.
Next time, we’ll write the actual parser and a test application.