Вы находитесь на странице: 1из 2

Backus Naur Form / Syntax Diagram Exercises

1. Draw syntax diagram for signed real numbers.

2. (a) Modify the definition for unsigned integer so it doesn’t accept leading zeroes.
(b) Apply to signed integers, signed real numbers.
(c) Draw their syntax diagram.

3. In a particular language, a variable name can consist of letters, numbers and _, but MUST start with a
letter. Examples: sum12, avg_num, sum_3, total would all be valid variable names, but 5length would
ne invalid. Give the definition for a variable using both BNF and syntax diagram.

4. A file of customer records is defined in BNF as follows:


<file> ::= <record> | <record><file>
<record> ::= <name><dob><sex>
<name>::=<string>@
<string>::=<char>|<char><string>
<char>::=<letter>|<sp>
<letter>::= A|B|C|D|E|F….|Z
<sp>::=the space character
<dob>::=<digit><digit><digit><digit><digit><digit>
<digit> ::= 1|2|3|4|5|6|7|8|9|0
<sex> ::= M|F

(a) State, with reasons, which of the following are not valid records.
(i) WILLIAMS@172793M
(ii) DIANE JONES@060693F
(iii)BILL DAVIDSON@121291m
(iv) MARY HIGGINS111090F [4]

(b) Another file has records of products. Each record has fields for product code, description , and number
in stock.

The product code is always two uppercase letters followed by two digits. The description can consist of
uppercase and lower case letters and spaces. The number in stock can consist of up to three digits. Write
down the definition for this record in BNF. You can use any of the definitions given in (a) without
rewriting them. [4]

(c) It is decided to include the cost in this record. The cost will consist of one or more digits, followed by
a decimal point, followed by two digits. Examples are 0.27, 23.40 and 150.00. write a definition for this
cost in BNF. [3]

5. A hexadecimal number consists of the digits 0 to 9, the letters A to F and the letters a to f. it must
terminate with either H or h. Examples are 12affh, F4AH, Bc5degh and adadfh.

Express a hexadecimal number in BNF [6]

6. A variable name in a certain high level language is defined in BNF as:

<VARIABLE NAME>::= <LETTERS>| <LETTERS><DIGIT>


<LETTERS> ::= <LETTER> |<LETTER><LETTERS>
<LETTER> ::= A|B|C|D|E|F….|Z
<DIGIT> ::= 1|2|3|4|5|6|7|8|9|0
(i) Which of the following will not be accepted as a variable name? Give your reasons for each
rejection: a7, A, AA7, AB17, 7A

(ii) Rewrite the BNF rules so that all the identifiers in (i) will be accepted. However, when
rewriting the rules you should also ensure that it is not possible to have a leading 0 in the
digits – your rules should not allow A01 or A00027. [13]

7. In a certain language, a real number is defined by the following BNF rules:

<real> ::= <number>.<number>


<number>::= <digit>|<number><digit>
<digit> ::= 1|2|3|4|5|6|7|8|9|0

(a) Which of the following are valid real numbers in this language? For any which is not valid, give a
reason why. 23, 12.34, 45. ,12.0, .567 , 0.00 [3]
(b) It is wished to extend the above definition so that it will also include real numbers which are
expressed in exponential form. The following will then also be valid,
2E+3 12.3E7 2.34E-6 0E0

Rewrite the above BNF rules in order to include numbers in exponential form. [8]

8. In a certain language, an identifier may consist of any number of letters and digits, the first of which
is a letter. A print statement can consist of the word PRINT alone, or PRINT followed by any number
of identifiers, separated from each other by commas.

Use syntax diagrams to define:


(i) the syntax of an identifier
(ii) the syntax of a print statement [6]

9. An amount of money can be defined as


• A$ sign followed by either
• A positive integer or
• A positive integer, a point, and a two digit number or
• A point and a two digit number

A positive integer has been defined as <INTEGER>


A digit is defined as <DIGIT>::= 0/1/2/3/4/5/6/7/8/9

a) Define, using Backus Naur form, the variable <AMOUNT OF MONEY>


b) Using the previously defined values of INTEGER and DIGIT, draw a syntax diagram to define AMOUNT
OF MONEY.

Вам также может понравиться