I’m looking into creating a parser that understands a very limited
subset of RTF (Rich Text Format).
I started with Treetop parser generator , 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  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, 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.
gram = Grammar.new do
digit = E(?0…?9)
(digit.repeat0).group("") + EOF
rgram = Grammar::Ruby0.compile(gram)
rgram.call(Output.new, StringIO.new(‘1234’).method(:getc) )