Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert integers to roman numerals using a syntax-directed translation scheme?

The Dragon Book includes an exercise on converting integers to roman numerals using a syntax-directed translation scheme.

How can this be completed?

like image 880
Daniel Magliola Avatar asked Nov 06 '08 01:11

Daniel Magliola


1 Answers

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.

like image 166
Yola Avatar answered Sep 23 '22 04:09

Yola