[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

treetop grammar problem

Robert Schaaf

6/10/2009 2:25:00 AM

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

2 Answers

Clifford Heath

6/10/2009 4:36:00 AM

0

Robert Schaaf 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 Heath.

Robert Schaaf

6/10/2009 5:51:00 AM

0

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

Thanks mightily,

Bob Schaaf


On Jun 10, 2009, at 12:40 AM, Clifford Heath wrote:

> Robert Schaaf 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 Heath.
>