Ambiguous Expression Grammar in Compiler
In this class, we discuss Ambiguous Expression Grammar in Compiler.
For Complete YouTube Video: Click Here
The reader should have prior knowledge of the syntax analysis phase. Click Here.
Our syntax analysis phase video discussed simple context-free grammar to identify pascal code.
In the syntax analysis context-free grammar, we left blank to expression identification grammar.
In this class, we discuss the conditions to write expression grammar.
First, we understand some basics of expressions.
Example:
Precedence:
a + b * c expression should evaluate first b* c.
The b * c is added to a. because ‘*’ has the highest precedence.
The grammar written to identify expressions should follow precedence.
Associative:
a + b -c expression is evaluated with a + b first.
Both plus and minus have the same precedence. So it follows left-associative.
The expression grammar should follow associative conditions.
Problems we face with ambiguous expression grammar.
The below example shows ambiguous expression grammar for evaluating expressions.
In our next class, we discuss writing grammar to evaluate expressions maintaining precedence and associative conditions.
E – E + E | E – E | E * E | id
Example: a + b * c
Using ambiguous grammar, we evaluate the expression in two ways.
The below diagram shows the two ways of evaluation.
The ambiguous grammar is not maintaining the precedence condition.
Similarly, ambiguous grammar does not maintain the associative condition.
Example: a + b -c
The below diagram shows the evaluation of an expression in two different ways.