Machine-translated by Google. Questions are authored in English.
Top

Discussion

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 *

Answer: B

The true statement is that - has higher precedence than *.

In an expression grammar, an operator introduced at a lower level (closer to id) binds more tightly. Here * and + are both produced by E, while - is produced by F, which sits below E. So any - is grouped first, inside an F, before * or + combine the results.

Because * and + appear at the same level, neither has higher precedence than the other, which rules out the options giving * over + or + over *. And + and - are at different levels, so they do not have the same precedence.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.