edited by
775 views
1 votes
1 votes
Consider translation rules :
 
S → S1 * A {S. Val = S1. Val + A. Val}
/A {S. Val = A. Val}
A → A1 – B {A. Val = A1. Val – B. Val}
/B {A. Val = B. Val}
B → C | B1 {B. Val = C. Val / B1. Val}
/C {B. Val = C. Val}
C → id {C. Val = Id. Val}

Let say output for input 8/4 * 2 – 6 * 4 is a1 and output for input 6 * 2 / 1 – 2 * 3 is a2, then a1 – a2 is ___________.
edited by

1 Answer

Best answer
2 votes
2 votes

In SDT above the operator that occurs at lowest level has highest precedence and one that occurs nearest to start symbol has lowest precedence.

Precedence order of operators is $/ \gt - \gt *$. But $*$ operation here means addition. Thus all divisions take place first, then subtraction and then Addition.

So, $a_1 = 8/4 * 2 – 6 * 4$ evaluates as $(8/4)+(2-6)+4 = 2 + (-4) + 4 = 2$

And $a_2 = 6 * ((2 / 1) – 2) * 3  = 2 + (2-2) + 3 = 5$

$a_1 -a_2 = 2 - 5 = -3$

edited by
Answer:

Related questions

2 votes
2 votes
2 answers
1
1 votes
1 votes
1 answer
2
worst_engineer asked Jan 5, 2016
911 views
The way I solved it was :is this correct evaluation order ?Because in the answer they have mentioned different order :
1 votes
1 votes
1 answer
3