<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><!--Converted with LaTeX2HTML 96.1-beta (Jan 15, 1996) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds -->Homework 1

  1. Write a grammar using the BNF meta-language for four-function calculator 'programs' using the calculator pictured below. Each 'program' consists of pressing the ON button, followed by any number of calculations followed by pressing the OFF button. Your grammar should only include meaningful programs that produce useful results (any sequence of button pushes is allowed, but not all these sequences produce meaningful results). In particular, notice that parentheses must be matched for a result to be produced. Use the normal notion of parenthesized infix expressions to guide your grammar. The +/- button changes the sign of the previous operand. The following sequences are valid programs:

·        ON 1 ´ ( 2 + 3 ) = 5 .1 1 1 1 1 ¸ 7 . 1 2 3 4 5 +/- + 1 2 3 4 . 5 6 7 8 = ( 3 2 - 5 6 +/- ) - ( 8 9 +/- 2 4 ) = OFF

·        ON OFF

·        ON 1 2 3 4 5 6 = OFF

·        ON ( ( ( 3 + 2 ) ´ ( 4 +/- - 5 ) ) ¸ 2 2 ) + 1 5 = OFF

  1. Draw the syntax tree for the last example above to demonstrate that your grammar works.
  2. Rewrite your BNF grammar in EBNF form aiming to remove as much recursion as possible from the BNF form.
  3. Redraw your EBNF grammar in syntax diagram form.

 

 

OFF

ON

 

7

8

9

(

+

4

5

6

)

-

1

2

3

 

´

0

.

+/-

=

¸

 

Assignment due: Friday, February 9th., in class.