Search code examples
rubytreetop

Treetop seems to fail on a simple grammar (5 rules)


I am trying to write a parser for a subset of C.

The behavior of treetop is difficult to analyze on this simple (further simplified) grammar.

 grammar Shyc

 rule functionDef
    type space identifier '('  ')' bloc
 end

 rule type
    'int'
 end

 rule bloc
    '{'  '}' 
 end

 rule identifier
    [a-zA-Z] [a-zA-Z_]*
 end

 rule space
   [\s]+
 end

end

My test case is "int main(){}"

And the error message from treetop is :

error at line 1, column 9
failure reason : Expected [a-zA-Z_] at line 1, column 9 (byte 9) after 
compiler.rb:25:in `parse': Parse error (RuntimeError)
from compiler.rb:73:in `<main>'enter 

The problem is thus around identifier rule...

The version of treetop : 1.5.3 and Ruby 2.1.1

Any idea ?


Solution

  • The problem was that my test case was in a separate file, with a supplemental end-of-line \n at the end, and that the grammar tested here does not specify how to consume that.

    Here is the code that solve the problem. As discussed here on the mailing list of Treetop, the error is weird and somehow misleading but it is difficult in general to automate the emission of a clear message.

    grammar Shyc
    
    rule functionDef
       type space identifier '('  ')' bloc space?
    end
    
    rule type
      'int'
    end
    
    rule bloc
      '{'  '}' 
    end
    
    rule identifier
       [a-zA-Z] [a-zA-Z_]*
    end
    
    rule space
      [\s\n]+
    end
    

    end