3

Given the following grammar: $$ E \to S$$ $$ S \to T \mid S+T $$ $$ T \to P$$ $$ P \to F\mid P*F $$ $$ F \to V\mid(E)$$ $$ V \to a\mid b\mid c$$

Does this grammar generate the words $(b*a)+c$ and $b*(a+c)$ ?

Mary Star
  • 13,956
  • 3
    So,I did the following,for the first one: $$S+T$$ $$T+T$$ $$P+T$$ $$F+T$$ $$(E)+T$$ $$(S)+T$$ $$(T)+P$$ $$(P)+F$$ $$(PF)V$$ $$(FF)+c$$ $$(VV)+c$$ $$(b*a)+c$$

    So,the word $(b*a)+c$ is generated by the grammar. Is this right?

    – Mary Star Jan 05 '14 at 20:59
  • 1
    Yes you are correct, though you should have $E$ and $S$ come before the first line $S+T$, and there is a typo on line $(P\ast F)\ast V$ it should be $(P\ast F)+V$ – Gina Jan 05 '14 at 21:06
  • 1
    Great!!Thanks a lot again!!! – Mary Star Jan 05 '14 at 21:07

1 Answers1

2

Short answer is yes. You just need to find the rule to generate that, which is easy once you see what they intuitively means.

More intuitively, it's just a standard grammar to generate all mathematical expression in +,* and a,b,c.

E: expression; S: sum; T: term; P: product; F: final element in the product; V: variable

Gina
  • 5,338
  • 1
    I have also an other question.How can I find the parse tree of the word $(b*a)+c$ ? – Mary Star Jan 05 '14 at 22:04
  • Drawing a parse tree is hard by typing, but the technique follow exactly your derivation. The starting symbol is the root, then you keep expanding all the leaf according to your substitution rule until every leaf is a terminal symbol. For example, your derivation start with $E$, $S$, and $S+T$. So you start by drawing a node for $E$. Then draw a node for $S$ as children of $E$ (because you substitute $E$ by $S$). Then draw 3 node $S$,$+$ and $T$ all are children of $S$ (because you substitue $S$ by $S+T$). I think you should consult your textbook because it's too long to explain in comment. – Gina Jan 05 '14 at 22:12