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:
- 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.
- Provide an attribute for the top-level production that gives the decimal value of the numeral being defined.
- 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:
- Create a function, parseRoman: string -> parseTree. It takes a
string an produces a parse tree that corresponds to your grammar.
- Create a function, printParseTree: parseTree -> string that produces a "readable" form of the parse tree.
- Create functions that, given a parseTree, will show the values of the attributes you defined in your attribute grammar.
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