# Treetop grammar problem

**URL:** <https://www.ruby-forum.com/t/treetop-grammar-problem/169905>\
**Category:** Ruby\
**Created:** [June 10, 2009, 4:26am UTC](https://www.ruby-forum.com/t/treetop-grammar-problem/169905 "2009-06-10T04:26:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [June 10, 2009, 4:26am UTC](https://www.ruby-forum.com/t/treetop-grammar-problem/169905/1 "2009-06-10T04:26:03Z")

</div>

I have devised this grammar for roman numerals for treetop.

grammar RomanNumeral

```
 rule roman_numeral
     ('MMM' / 'MM' / 'M')?
         (('C' [DM]) / ('D'? ('CCC' / 'CC' / 'C')?))?
         (('X' [LC]) / ('L'? ('XXX' / 'XX' / 'X')?))?
         (('I' [VX]) / ('V'? ('III' / 'II' / 'I')?))?
 end

```

end

It recognizes numbers in the range 1-3999, and rejects all bad input  
as far as I can tell. T

But the empty string passes!

The problem is that any part is optional, but not ALL parts. At a  
minimum, there must be one digit. Duh.

Treetop is ingenious, and easy to use, except when it’s hard.

Does anyone have a suggestion? I’m new at this, so I beg your patience.

Also, is there a better way of translating “M{0,3}” than “(‘MMM’ /  
‘MM’ / ‘M’)?” ?

Bob Schaaf

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [June 10, 2009, 6:41am UTC](https://www.ruby-forum.com/t/treetop-grammar-problem/169905/2 "2009-06-10T06:41:02Z")

</div>

Robert S. wrote:

> But the empty string passes!

It’s not a general solution, but you can require that _something_  
follows, using &.

rule roman\_numeral  
&. ( stuff )  
end

There’s no better way to write M{0,3}

Clifford H…

---

<div class="post-metadata">

**Author:** ![rjsquire](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/74df32/32.png) [@rjsquire](https://www.ruby-forum.com/u/rjsquire)\
**Post date:** [June 10, 2009, 7:51am UTC](https://www.ruby-forum.com/t/treetop-grammar-problem/169905/3 "2009-06-10T07:51:46Z")

</div>

Of course! Lookahead non-gobbling. A lesson learned.

Thanks mightily,

Bob Schaaf
