Treetop: parsing single node returns nil

Posted by Matchu on Stack Overflow See other posts from Stack Overflow or by Matchu
Published on 2010-05-18T15:59:16Z Indexed on 2010/05/18 16:00 UTC
Read the original article Hit count: 352

Filed under:
|
|
|

I'm trying to get the basic of Treetop parsing. Here's a very simple bit of grammar so that I can say ArithmeticParser.parse('2+2').value == 4.

grammar Arithmetic
  rule additive
    first:number '+' second:number {
      def value
        first.value + second.value
      end
    }
  end

  rule number
    [1-9] [0-9]* {
      def value
        text_value.to_i
      end
    }
  end
end

Parsing 2+2 works correctly. However, parsing 2 or 22 returns nil.

What did I miss?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about treetop