# Help with "grammar"

**URL:** <https://www.ruby-forum.com/t/help-with-grammar/167908>\
**Category:** Ruby\
**Created:** [May 13, 2009, 9:55pm UTC](https://www.ruby-forum.com/t/help-with-grammar/167908 "2009-05-13T21:55:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![absolute-beginner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@absolute-beginner](https://www.ruby-forum.com/u/absolute-beginner)\
**Post date:** [May 13, 2009, 9:55pm UTC](https://www.ruby-forum.com/t/help-with-grammar/167908/1 "2009-05-13T21:55:50Z")

</div>

Hi

I’m looking into creating a parser that understands a very limited  
subset of RTF (Rich Text Format).

I started with Treetop parser generator [1], and was able to get a  
parser that analysed RTF syntax working in less than an hour. Brill.  
Unfortunately the parser was way too slow for use in a real-time app.

Grammar [2] claims it generates much faster parsers. However, having  
looked at the tests and tried to emulate them, I can’t get a basic  
parser up and running that works sensibly. The super-simple parser (code  
below) is just meant to parse a group of digits and end.

- The version below does nothing
- If I set it to compile gram[0], it crashes out because (I think) it  
only uses the digit rule, so dies when it meets EOF

I’d be grateful if anyone could say what I’m missing, or even better,  
point me to a straightforward example of using grammar, as the  
documentation is pretty sparse.

thanks  
alex

[1] [http://treetop.rubyforge.org/](http://treetop.rubyforge.org/)  
[2] [http://rubyforge.org/projects/grammar/](http://rubyforge.org/projects/grammar/)

* * *

require ‘grammar’  
require ‘stringio’  
include Grammar::Molecules

gram = Grammar.new do  
digit = E(?0…?9)

(digit.repeat0).group(“”) + EOF  
end

require ‘grammar/ruby0’  
rgram = Grammar::Ruby0.compile(gram[0])

class Output  
def \<\<(token)  
p token  
end  
end

rgram.call(Output.new, StringIO.new(‘1234’).method(:getc) )
