Consider the following grammar for arithmetic expressions using binary operators $-$ and $/$ which are not associative

- $E \rightarrow E -T\mid T$
- $T \rightarrow T/F\mid F$
- $F \rightarrow (E) \mid id$

($E$ is the start symbol)

Is the grammar unambiguous? Is so, what is the relative precedence between $-$ and $/$? If not, give an unambiguous grammar that gives $/$ precedence over $-$.

### 3 Comments

@set2018 to prove if a grammar is unambiguous is undecidable. we can try to see if the given grammar is LL(k) or LR(k) since they are unambiguous. note that unambiguity does not imply the grammar is LL(k) or LR(k). The grammar in the question is not LR(1). so unambiguity should be concluded by careful observation.

## 2 Answers

### 10 Comments

See the grammar is always written in such a way that the operator which have high precedence must lie in the lower part of grammar. As you can see in above grammar operator / lies below the operator - which means / has high precedence than -

Similarly you will always see Id comes in last because it is operand which has highest precedence above operators also..

So how we can say that the precedence of '/' is always higher than '-'?? Please explain.

@Raas Star As you said let us say that / has higher precedence as it is within () then how is "id2/**(id3-id4)**)" evaluated? We just know left operand we dont know the value of right operand. For that we surely need to evaluate - first instead of /. The issue is because of last grammar production where 1st Non terminal is again called. So I think there is no concept of precedence here. We can make parse tree where - has more precedence than / and another where / has more precedence than -.