0

I'm trying to make sure if i did a) correct. I believe it makes sense, just trying to see if anyone has any suggestions.

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):

  1. calculation -> expression =
  2. expression -> value | value operator expression
  3. value -> number | sign number
  4. number -> unsigned | unsigned . unsigned
  5. unsigned -> digit | digit unsigned
  6. digit -> 0| 1| 2| 3| 4| 5| 6| 7| 8| 9
  7. sign -> + | -
  8. 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
geforce
  • 217
  • 1
    It's a correct derivation. If I were to be picky, then I would say that for consistence it should be <unsigned> . <unsigned> rather than <unsigned . unsigned>. – dtldarek Feb 02 '15 at 09:13
  • Oh, you're right. Thanks a lot. – geforce Feb 02 '15 at 09:17
  • Also, should i have kept "=" at the end of each line since rule 1? – geforce Feb 02 '15 at 09:19
  • I would remove it because you have => already. More importantly, if that detail is important for your TA, then I would change the group/course or even school if that's a prevalent attitude (although it may be justified if it is about readability in some concrete programming language, but it's not that case here). – dtldarek Feb 02 '15 at 09:27
  • Well logically speaking, '=' exists in the terminal symbols. So it can be used, and productions rule one states calculation simplifies to = .. and so on – geforce Feb 02 '15 at 09:30
  • 2
    I would definitely keep the "=" since rule 1, since a) the expression you are asked to find includes "=" and b) rule 1 specifically says that "=" is included. I would say that it is clearly wrong to omit it, and displays a lack of understanding of BNF notation. – mrp Feb 02 '15 at 09:30
  • Alright, I was exactly thinking that.. thanks. – geforce Feb 02 '15 at 09:31
  • 1
    I'm sorry, @mrp is right. – dtldarek Feb 02 '15 at 10:38

0 Answers0