Question

I am having issues with a Backus Naur assignment I want to define rules that allow...

I am having issues with a Backus Naur assignment

I want to define rules that allow for an integer named x to have one of the following:

0-F followed by “h” to define a hex value,

0-1 to define a binary value, or

0-9 followed by a “d” to define a decimal value.

For example, int x = 3d would be a legal statement

On the other hand, int x= 4b would not be legal (binary numbers can only be 0 r 1

I would like to know how to go about doing that.

Check out the algorithm for solving the problem.

1. Convert the given integer to a string.
2. Check for the last character in the string.
1. If last character is 'h', iterate the string and check for all characters whether they are from 0-F or not. If not, return invalid, else valid.
2. If last character is 'd', iterate the string and check for all characters whether they are from 0-9 or not. If not, return invalid, else valid.
3. If last character is neither 'h' nor 'd', iterate the string and check whether all the characters are either '0' or '1'. If not, return invalid, else valid.

Earn Coins

Coins can be redeemed for fabulous gifts.