0

Treetop seems to ignore turther rules after the first and fails to parse anything that doesn't match the first rule on the grammar file. I already tried to swap the order of the rules, but still, only the first one is considered.

# grammar_pov.treetop
grammar Pov
    rule numeric
        '-'? [0-9]+ ('.' [0-9]+)? <::PovFabric::Nodes::NumericLiteral>
    end
    rule comma
        ','
    end
    rule space
        [\s]+
    end
end

This grammar file matches all integer and floats, but doesn't match '123, 456' or '123,456' The parser failure_reason property says this 'Expected - at line 1, column 1 (byte 1) after '

Am i missing something?

paul.ago
  • 3,904
  • 1
  • 22
  • 15

2 Answers2

1

Your comma and space rules aren't used anywhere. So, they are just dead code.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • Can you elaborate? Shouldn't the comma rule match '123,456' and '123, 456'? – paul.ago Sep 23 '13 at 08:43
  • No, the `comma` rule matches `','` and nothing else. But that is completely irrelevant anyway, because the rule isn't even used anywhere. It's the same as defining a subroutine and never calling it. – Jörg W Mittag Sep 24 '13 at 10:14
0

Like Jörg mentioned, you need to use your comma and space rules in the grammar. I built a simple example of what I think you're trying to accomplish below. It should match "100", "1,000", "1,000,000", etc.

If you look at the numeric rule, first I test for a subtraction sign '-'?, then I test for one to three digits, then I test for zero or more combinations of comma's and three digits.

require 'treetop'
Treetop.load_from_string DATA.read

parser = PovParser.new

p parser.parse('1,000,000')

__END__
grammar Pov
   rule numeric
      '-'? digit 1..3 (comma space* (digit 3..3))*
   end

   rule digit
      [0-9]
   end

   rule comma
      ','
   end

   rule space
      [\s]
   end
end
Josh Voigts
  • 4,114
  • 1
  • 18
  • 43