229k views
3 votes
Given the following expression grammar:

E -> E * F | F + E | F
F -> F - F | id

which of the following is true? (You have to validate your answer, without validation your answer won’t be evaluated).
a. * has higher precedence than +
b. – has higher precedence than *
c. + and – have same precedence
d. + has higher precedence than *

User DNNX
by
8.7k points

1 Answer

1 vote

Final answer:

D. The + operator has higher precedence than * in the given expression grammar.

Step-by-step explanation:

The correct answer is d. + has higher precedence than *. This means that the + operator has higher priority than the * operator when evaluating expressions.

For example, consider the expression: 2 + 3 * 4. According to the grammar, the * operator should be evaluated first. So, 3 * 4 = 12. Then, the + operator is evaluated, resulting in 2 + 12 = 14.

On the other hand, in the expression: 2 * 3 + 4, the + operator is evaluated first because its precedence is higher. So, 2 * 3 = 6. Then, the + operator is evaluated, resulting in 6 + 4 = 10.

User Blubase
by
8.3k points