While I read a compiler book, I have struggled to understand the reason why the arithmetic operations are expressed as follows.

add = mul add'

mul = term mul'
mul = ε
mul' = "*" term mul'
mul' = "/" term mul'

term = <num>


That must be easy to understand those who are familiar with BNF. The calculus expression can be reduced from non-terminal, add. You can construct the LL(1) parser based on the grammar systematically as demonstrated in the book.

But in the beginning, it looks very redundant to me. For example, an expression 1 + (2 + 3) is interpreted as shown in the first illustration. In spite of the simplicity of the expression itself, why can the syntax tree be such complicated? There must be a good reason because the theory of compiler and context-free grammar is one of the most researched fields in computer science. In this post, I’m going to try to clarify the reason why the definition of the grammar of arithmetic operations looks redundant and complicated than I expected.

Table Of Contents
- The Simplest Definition I Could Imagine
- Another Definition Supporting Consecutive Binary Operation
- Eliminate Direct Left Recursion


# The Simplest Definition I Could Imagine

First, I tried to come up with the simplest grammar definition for arithmetic operations. It has only 8 lines of the definitions.

add = mul

mul = term
mul = term "*" term
mul = term "/" term

term = <num>


But I found it did not work as expected immediately. It could not parse the following like 1 + 2 + 3 because add is not reduced add itself. We could not put consecutive “+” operations in the expression. In order to achieve arbitrary consecutive binary operations, we should have add itself in either left or right of the binary operation.

# Another Definition Supporting Consecutive Binary Operation

add = mul

mul = term
mul = mul "*" term
mul = mul "/" term

term = <num>


It looks good but I wondered whether it is possible to mul before add in the second line.

add = mul "+" add


Actually, it’s possible. The order of these non-terminator decides the priority of how each operator is applied. Let’s say we have a grammar defined like this. You can see the right operand of “+” is evaluated first.

add = term


On the other hand, this grammar leads the left operand is evaluated first.

add = term


So overall, the difference between add "+" mul and mul "+" add will lead the difference of the associativity of the same operators. Associativity of the most arithmetic operators is left to right. It’s natural to use add "+" mul so that we can get the syntax tree with the deep left child.

But there is one problem called Left Recursion. When you try to implement a parser of this grammar, the function to parse the node add will look like this.

add()
{
mul();
}


As function add calls add at the beginning, it causes an infinite loop so that we are not able to make progress at all. In order to avoid this type of problem, it’s necessary to rewrite the grammar a little bit.

# Eliminate Direct Left Recursion

Generally, a grammar is called left recursive if and only if there is a non-terminal symbol which can be derived with itself as the leftmost symbol. The left recursion which can be written as follows is called direct left recursion especially.

$$A \Rightarrow A\alpha | \beta$$

$$\alpha$$ and $$\beta$$ are the terminal symbols. Simply writing a parser with this grammar cause the infinite loop. How can we remove the direct left recursion?

Let’s assume that $$\beta$$ is a terminal or non-terminal symbol which does not start with A. Then we can rewrite the rule like this.

$$A \Rightarrow \beta A’$$

$$A’ \Rightarrow \alpha A’ | \epsilon$$

The rewritten rule enables us to implement a parser free from infinite recursion. Let’s try to rewrite the rule I introduced at the beginning to eliminate left recursion. This rule causes the left recursion as aforementioned.

add = mul


Let’s introduce new symbol add'. Assuming mul corresponds to $$\beta$$ and “+” or “-“ does to $$\alpha$$.

add = mul add'


This rule can be written as follows.

add()
{
mul();
}

{
if (isPlusToken()) {
mul();
}
else if(isMinusToken()) {
mul();
}
}


But in practice, it seems to be common to write like this. That’s short, simple and efficient than the previous one.

add()
{
mul();
for (;;) {
if (isPlusToken()) {
mul();
}
else if (isMinusToken()) {