438 views
2 2 votes

Given the following expression grammar:

$$
\begin{gathered}
E \rightarrow E+T|T * E| T \\
T \rightarrow T / F \mid F \\
F \rightarrow \mathrm{id}
\end{gathered}
$$


Which of the following is/are true?

  1. $*$ has higher precedence than $+$
     
  2. $/$ has higher precedence than $*$
     
  3. $+$ is left-associative
     
  4. $*$ is right-associative

3 Answers

2 2 votes

Given grammar:

$E \rightarrow E \,+\, T \;\mid\; T \,*\, E \;\mid\; T$

$T \rightarrow T \,/\, F \;\mid\; F$

$F \rightarrow id$

----------------------------

● Operator appearance by grammar level:

The operator $/$  appears in the rule for $T$:  

  $T \rightarrow T \,/\, F$

The operators $+$ and $*$ appear in the rules for $E$:  

  $E \rightarrow E \,+\, T \;\mid\; T \,*\, E$

Since $T$ is lower in the grammar hierarchy than $E$, any parsing involving $/$

must complete before any reductions that involve $+$ or $*$.  

Thus $/$ binds more tightly than both $+$ and $*$ (i.e., it has higher precedence).

----------------------------

● Associativity

$+$ is left-associative because $E \rightarrow E \,+\, T$ is left-recursive

$*$ is right-associative because $E \rightarrow T \,*\, E$ is right-recursive

$/$ is left-associative because $T \rightarrow T \,/\, F$ is left-recursive

----------------------------

● Final Evaluation of statements:

A. * has higher precedence than +   → False  

B. / has higher precedence than *   → True  

C. + is left-associative            → True  

D. * is right-associative           → True

Final Answer: $B, C, D$
 

Answer:
Position:
Show:

Related questions

1 1 vote
3 3 answers
323
323 views
GO Classes asked Nov 29, 2025
323 views
Consider the following grammar with semantic actions inside braces:$S \rightarrow a A \quad$ {print "1"} $S \rightarrow b \quad$ {print " 2 " } $A \rightarrow S b \quad$ ...
1 1 vote
2 2 answers
302
302 views
GO Classes asked Nov 29, 2025
302 views
A shift-reduce parser carries out the actions specified within braces immediately after reducing with the corresponding rule of the grammar:$A \rightarrow a B \quad$ {pri...
1 1 vote
2 2 answers
316
316 views
GO Classes asked Nov 29, 2025
316 views
The number of tokens in the following C statement is $\_\_\_\_$ . scanf("%d %f",&num,&value); 
2 2 votes
2 2 answers
324
324 views
GO Classes asked Nov 29, 2025
324 views
The number of tokens in the following C statement is $\_\_\_\_$for(int k=0; k<MAX; k++) { sum += data[k];}