Consider a grammar for calculator language.
This language consists of all arithmetic expressions that can be evaluated by a calculator, i.e. expressions of the form
expression=
where "expression" is an arithmetic expression (without variables) to be evaluated. Examples of such expressions:
6/-2= or 12.5* 2+5=,
The grammar
G=<V,T,P,calculation>
for the calculator language is defined by:
V={calculation, expression, value, number, unsigned, digit, sign, operator} T={0,1,2,3,4,5,6,7,8,9,+,-,*,/,=,.}
P consists of the following productions (rules):
- calculation -> expression =
- expression -> value | value operator expression
- value -> number | sign number
- number -> unsigned | unsigned . unsigned
- unsigned -> digit | digit unsigned
- digit -> 0| 1| 2| 3| 4| 5| 6| 7| 8| 9
- sign -> + | -
- operator -> +| -| *| /
Show:
(a) 100/2.5= is in the calculator language;
My answer:
a.
<calculation>
=> <expression> =
=> <value> <operator> <expression>
=> <value> / <expression>
=> <number> / <expression>
=> <number> / <value>
=> <number> / <number>
=> <unsigned> / <unsigned . unsigned>
=> <digit><unsigned> / <digit . digit>
=> 1<digit><unsigned> / 2.5
=> 10<digit> / 2.5
=> 100 / 2.5
<NR>means; does it mean you are too lazy to type out<number>? If so, then your solution that begins at "My answer: a." is incomplete, because it shows<number>reducing directly to100, but you cannot do that; you can only use rule 4, which says that<number>reduces to<unsigned>or<unsigned> . <unsigned>, and then you must use rule 5 to reduce<unsigned>. – MY USER NAME IS A LIE Jan 31 '15 at 23:46<digit>can produce only one digit. it cannot produce100. And there is no rule that says you can reduce<unsigned>to<digit><digit><digit>; the only rule you can use is rule 5, which says you can replace<unsigned>with<digit>or with<digit><unsigned>. – MY USER NAME IS A LIE Feb 01 '15 at 00:08