treetop grammar problem

R

Robert Schaaf

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
 
C

Clifford Heath

Robert said:
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.
 
R

Robert Schaaf

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

Thanks mightily,

Bob Schaaf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top