Associativity and Precedence Questions | Compiler Design

Question 1
Given the following expression grammar:
E→ E*F | F+E | F
F→ F-F | id

Which of the following is true?
A
* has higher precedence than +
B
– has higher precedence than *
C
+ and – have same precedence
D
+ has higher precedence than *
Question 1 Explanation: 
Operator which is at lower level in the grammar is termed to have higher precedence.
  • * and + have least priority because  are present at the top most level .
  • * and + have equal precedence because they are same level .
  • '-' have higher precedence than '+' and '*' because is present at the bottom most level
Let us take an example 6*7-8 when we draw parse tree according to grammar
      E
   /  |  \
  E   *   F
  |     / | \
  F    F  -  F
  |    |     |
id(6) id(7) id(8)

As we can see that first ‘- ‘ will be evaluated then ‘ * ‘ is evaluated
Thus, ‘ – ‘ has higher precedence then *.

So correct answer is option(B)
There is 1 question to complete.

Leave a Reply