The Dragon Book includes an exercise on converting integers to roman numerals using a syntax-directed translation scheme.
How can this be completed?
Next is grammar to represent syntax directed translation from number in format 1xxx into roman numerals.
number = OneThousand digit3 digit2 digit1 | nzdigit3 digit2 digit1 | nzdigit2 digit1 | nzdigit1
OneThousand -> 1 {print('M')}
digit3 -> 0 digit3 -> nzdigit3
nzdigit3 -> 1 print('C') nzdigit3 -> 2 print('CC') nzdigit3 -> 3 print('CCC') nzdigit3 -> 4 print('CCCC') nzdigit3 -> 5 print('D') nzdigit3 -> 6 print('DC') nzdigit3 -> 7 print('DCC') nzdigit3 -> 8 print('DCCC') nzdigit3 -> 9 print('DCCCc')
In similar manner write definition for digits in 2 and 1 position and you will have needed translation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With