381 views
2 votes
2 votes

How to get the second string,i.e., 6*2/1-2*3 from the given grammar?

1 Answer

0 votes
0 votes
More away from the Start symbol, Higher the precedence.

Here, $B\rightarrow C/b_{1}$ is 2 levels down from Start symbol

and $a\rightarrow A_{1} - B$ is 1 level away from start symbol, which clears that ' / ' has higher precedence than ' - ' and ' * '.

$So, / > - > *$

$a_{1} = 8/4 * 2 - 6*4; \\ = ( (8/4)*(2-6)*4);$

But as per first grammer, Multiplacation prints addition of two numbers. Therefore

$a_{1} = (2 + (-4) + 4) = 2;$

and, similarly $a_{2} = 6*2/1-2*3; \\ (6*((2/1)-2)*3); i.e. (6 +0 + 3) = 9$

Now, $a_{1} - a_{2} = 2 - 9 \\  = -7$