Due date questions 1 and 2, and paper version of 3 due
Monday September 25. Running version of question 3 due October 2.
1. From text: problem 2.15 (ambiguities in a C-like grammar). Do parts
a and b, and explain how the problem is actually solved in C.
2. from text: problem 2.21 (a grammar that describes a language with
an amusing arithmetic property).
3. Programming assignment in Ada.
In Ada, integer literals can be given in any base between 2 and 16. The
base to be used appears as a prefix of the number, and the digits appear
bracketed by sharp signs. For example, the following are valid representations
of the number 7:
2#111# , 3#21#, 4#13#, 9#7#, etc.
For bases greater than 9, the valid digits are taken from the usual sequence
A, B, C, D, E, F. The base itself is always given in base 10. For example, the
number 2560 can be written: 16#A00#
The assignment is to write a program that reads a string representing a based number, and prints
the corresponding decimal value of the number. The Input/output primitive that
you need are the following:
a) Get_Line (Line, last) reads one line from the input file, and sets Last
to the number of characters read. You should declare Line to be a string of
100 characters, long enough for our purposes.
b) Put_Line (Str) prints a line on the output file. To print integers, use
the image attribute, as follows: Put_Line (Integer'Image (X));
The outline of the program should be:
with ada.text_io; use ada.text_io;
procedure Based_Numbers is
Line : String (1..100);
Last : Integer;
Val : Integer;
Get_Line (Line, Last);
exit when .. -- choose some termination condition
.... scan the number, do the conversion, report illegal input, etc.
Put_Line (Integer'image (Val));