Unambiguous Expression Grammar in Compiler

In this class, We discuss Unambiguous Expression Grammar in Compiler.

For Complete YouTube Video: Click Here

The reader should have prior knowledge of ambiguous expression grammar. Click Here.

In our last class, we discussed the expression grammar should maintain precedence and associative conditions.

The below example grammar will maintain the precedence and associative conditions.

The three non-terminals used are Expression E, Term T, and Factor F.

The grammar starting symbol is E.

In the expression grammar, we use ‘+,’ ‘-, ‘‘*,’ ‘/,’ and ‘(. ‘

The brackets have the highest precedence.

The second highest precedence is for * and /.

The least precedence is for + and -.

How are we maintaining the precedence condition?

The + and – have the same precedence, giving the same non-terminal E.

Similarly, * and / given to the same non-terminal T.

The least precedence + and – used in non-terminal E and E calling T.

From the non-terminal E, we are moving to Non-terminal T.

Similarly, the non-terminal T is called F.

The last production F

We need to pass E to go for T and use * and / symbol. With this, we are maintaining precedence.

The below example helps the reader to understand the precedence condition better.

Example: a + b * c

The first production is E – E + T.

To take b * c, we should expand T. Without T; We can not get the symbol * and /.

We are maintaining precedence by giving step-wise in grammar.

The below example shows the associative condition.

Example: a+ b – c

First, we expand E – E – T.

The non-terminal T will check the id.

To take +, we should expand E., i.e., we should expand the left side non-terminal.

So we are maintaining left-associative with the expansion of left non-terminal for the same precedence.

The below examples show the precedence for brackets.

Example: (a+b+c) and a+(b+c)

The below example shows the derivation tree for (a+b+c) and a+(b+c)