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) |
Re: Grammar for roman numerals DrDiettrich1@aol.com (Hans-Peter Diettrich) (2007-04-08) |
From: | Martin Ward <martin@gkc.org.uk> |
Newsgroups: | comp.compilers |
Date: | 30 Mar 2007 23:05:47 -0400 |
Organization: | Compilers Central |
References: | 07-03-095 07-03-107 |
Keywords: | parse |
Posted-Date: | 30 Mar 2007 23:05:47 EDT |
On Friday 30 Mar 2007 04:05, Ivan Boldyrev wrote:
> On 9791 day of my life msully wrote:
> > One of the exercises I am doing is writing a grammar for roman
> > numerals.
>
> Now rewrite the grammar as a regular expression :)
Following the "Robustness Principle" attributed to John Postal,
("Be liberal in what you accept, and conservative in what you send."
http://ironick.typepad.com/ironick/2005/05/my_history_of_t.html),
there is a very simple regular expression for Roman Numerals
[MDCLXVI]+
Taking the most liberal interpretation of the rule "a smaller number
placed before a larger numeral should be subtracted from it,
instead of added to it", every string of numerals has a valid interpretation.
For example, "IXM" is interpreted as as IX (9) subtraced from M (1000)
to give 991.
This method has the advantage of providing representations for zero
and negative numbers: for example VVX is one representation of zero,
If only the Romans had followed the Robustness Principle, they might
have discovered zero and negative numbers, and changed the course
of mathematical history!
--
Martin
martin@gkc.org.uk http://www.cse.dmu.ac.uk/~mward/ Erdos number: 4
G.K.Chesterton web site: http://www.cse.dmu.ac.uk/~mward/gkc/
Return to the
comp.compilers page.
Search the
comp.compilers archives again.