2,684 views
17 votes
17 votes

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)

Does the grammar allow expressions with redundant parentheses as in $(id/id)$ or in $id−(id/id)$? If so, convert the grammar into one which does not generate expressions with redundant parentheses. Do this with minimum number of changes to the given production rules and adding at most one more production rule.

Convert the grammar obtained above into one that is not left recursive.

1 Answer

Best answer
21 votes
21 votes

Here we have to convert the grammar into one which does not generate expressions with redundant parentheses.So the grammar which does not generate expressions with redundant parentheses

  • $E\rightarrow E-T\mid T$
  • $E'\rightarrow E-T$
  • $T\rightarrow id\mid T'/F$
  • $T'\rightarrow F\mid T'/F$
  • $F\rightarrow id\mid \left ( E' \right )$

Now equivalent grammar after removing left recursion

  • $E\rightarrow TX'$
  • $X'\rightarrow -TX'\mid \epsilon$
  • $E'\rightarrow E-T$
  • $T\rightarrow id\mid T'/F$
  • $T'\rightarrow FY'$
  • $Y'\rightarrow /FY'\mid \epsilon$
  • $F\rightarrow id\mid \left ( E' \right )$
selected by

Related questions

22 votes
22 votes
3 answers
3