CS536-S06: Mini-Project 2

The following BNF specification defines a language for Roman numerals (not, perhaps all Roman numerals). This grammar can also generate invalid Roman numerals.

<roman> ::= <thousands> <hundreds> <tens> <units>
<thousands> ::= <empty> | <thousands> M
<hundreds> ::= <low hundreds> | CD | D <low hundreds> | CM
<
low hundreds> ::= <empty> | <low hundreds> C
<
tens> ::= <low tens> | XL | L <low tens> | XC
<
low tens> ::= <empty> | <low tens> X
<
units> ::= <low units> | IV | V <low units> | IX
<
low units> ::= <empty> | <low units> I

Write an attribute grammar for the above. You need attributes for the following tasks:

  1. Restrict the number of I's, X's, C's, and M'x to no more than three. The first three of these will be in the <low units>, <low tens>, and <low hundreds> productions respectively.
  2. Provide an attribute for the top-level production that gives the decimal value of the numeral being defined.
  3. An alternate representation is to allow IIII instead of IV, XXXX instead of XL, and CCCC instead of CD. Add attributes as appropriate for allowing one or the other representation in the numeral, but not both. That is, you could have CCCCIIII, or CDIV, but not CCCCIV.
You should implement your solution as an ML program. You need to do the following:
You should also provide test cases for all of your functions.

I have put a short write up on attribute grammars in the SourceForge Documents tab in the course SourceForge project.

Last modified: 31-Mar-2006
Gary Pollice