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.
I would like a visual representation of it.
Like how it would look like in BNF grammer.
You can write the BNF grammar as:
<int> ::= <bin> | <hex> h | <dec> d
<bin> ::= <bindigit> | <bin> <bindigit>
<bindigit> ::= 0 | 1
<hex> ::= <hexdigit> | <hex> <hexdigit>
<dec> ::= <decdigit> | <dec> <decdigit>
<hexdigit> ::= 0|1|2|3|4|5|6|7|8|9|A| B|C|D|E|F
<decdigit> ::= 0|1|2|3|4|5|6|7|8|9
Here, the integer can be a binary number or a hexadecimal followed by h or a decimal value followed by d.
Now a binary number can be a single binary digit or multiple digits (bin followed by bindigit) where a binary digit can be either a 0 or a 1.
Same for hexadecimal and decimal numbers.
Get Answers For Free
Most questions answered within 1 hours.