0

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 ?

JCLL
  • 5,379
  • 5
  • 44
  • 64
  • This works for me (tt 1.4.10, ruby 2.0.0p353). Maybe you have a leftover tt-compiled .rb file that's preventing you from loading the up-to-date .treetop file? That has bitten me before. – wdebeaum Jul 10 '14 at 14:16
  • Works for me as well, probably a problem with the way your files are set up. – Josh Voigts Jul 10 '14 at 21:14

1 Answers1

0

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

JCLL
  • 5,379
  • 5
  • 44
  • 64