Re: Grammar for roman numerals

"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
30 Mar 2007 08:31:20 -0400

          From comp.compilers

Related articles
Grammar for roman numerals msully4321@gmail.com (2007-03-27)
Re: Grammar for roman numerals martin@gkc.org.uk (Martin Ward) (2007-03-29)
Re: Grammar for roman numerals boldyrev+nospam@cgitftp.uiggm.nsc.ru (Ivan Boldyrev) (2007-03-29)
Re: Grammar for roman numerals mailbox@dmitry-kazakov.de (Dmitry A. Kazakov) (2007-03-30)
Re: Grammar for roman numerals martin@gkc.org.uk (Martin Ward) (2007-03-30)
Re: Grammar for roman numerals mailbox@dmitry-kazakov.de (Dmitry A. Kazakov) (2007-04-01)
Re: Grammar for roman numerals DrDiettrich1@aol.com (Hans-Peter Diettrich) (2007-04-01)
Re: Grammar for roman numerals alex.habar.nam@gmail.com (whiskey) (2007-04-06)
Re: Grammar for roman numerals dickey@saltmine.radix.net (Thomas Dickey) (2007-04-06)
Re: Grammar for roman numerals mailbox@dmitry-kazakov.de (Dmitry A. Kazakov) (2007-04-06)
[1 later articles]
| List of all articles for this month |
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Newsgroups: comp.compilers
Date: 30 Mar 2007 08:31:20 -0400
Organization: cbb software GmbH
References: 07-03-095
Keywords: parse
Posted-Date: 30 Mar 2007 08:31:20 EDT

On 27 Mar 2007 09:27:36 -0400, msully4321@gmail.com wrote:


> I am doing a self-taught independent study in compiler design through
> my school using the Red Dragon book as a text. One of the exercises I
> am doing is writing a grammar for roman numerals. I wanted to check my
> grammar's correctness, but could not find any grammars on the internet
> that covered all of the letters (up to M).
>
> Here is my grammar (I allow an arbitrary number of Ms)
>
> numeral -> thousands
> thousands -> thous_part hundreds | thous_part | hundreds
> thous_part -> thous_part M | M
> hundreds -> hun_part tens | hun_part | tens
> hun_part -> hun_rep | CD | D | D hun_rep | CM
> hun_rep -> C | CC | CCC
> tens -> tens_part ones | tens_part | ones
> tens_part -> tens_rep | XL | L | L tens_rep | XC
> tens_rep -> X | XX | XXX
> ones -> ones_rep | IV | V | V ones_rep | IX
> ones_rep -> I | II | III
>
> Comments?


Roman numerals (in their standard form without IIII and other stuff) don't
much differ from decimal positional system. The only problem is that glyphs
of the decimal places depend on the position. The grammar could look like


numeral := [ numeral ] decimal-place(n)
decimal-place(n) := 0(n) | 1(n) | 2(n) | 3(n) | ... | 9(n)
0(n) := <empty>
1(n) := x(n)
2(n) := x(n)x(n)
3(n) := x(n)x(n)x(n)
4(n) := x(n)v(n)
5(n) := v(n)
6(n) := v(n)x(n)
7(n) := v(n)x(n)x(n)
8(n) := v(n)x(n)x(n)x(n)
9(n) := x(n)x(n+1)


Here n is the number of the decimal place and finally:


x(1) = I
x(2) = X
x(3) = C
x(4) = M


v(1) = V
v(2) = L
v(3) = D


--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.