Martin Macak
Martin Macak

Reputation: 3831

Treetop parser error handling mechanism providing useless output

I've been experimenting with Treetop lately to create simple parser for CFG DSL language for one of my clients. I was successful to implement all the features he required, but working with Treetop turned out to be quite a painful experience.

The problem is that I was not able to get any usable error message from Treetop. The only output I am getting is

parser.rb:22:in `parse': Parser error at offset: 0 (Exception)
Error:
#<TranLanParser:0x007f960c852f60>
    from parser.rb:28:in `<class:Parser>'
    from parser.rb:10:in `<main>'

which always points to the first character in the file. This is really terrible to find any error in the parsed language. How should I incrementally develop my parser if I can't find what's wrong whatsoever?

I tried to change my grammar to contain recursive rules, because I thought that this would help the parser to create AST nodes as soon as possible, but it didn't help.

My question is: Am I doing something wrong? Is there any good example how to create PEG grammars for Treetop, which provide meaningful error messages on partially derived trees? Or is it a bug/error in Treetop library?

Thanks for any opinion.

Upvotes: 1

Views: 223

Answers (1)

cliffordheath
cliffordheath

Reputation: 2606

Did you try printing parser.failure_reason? This prints the list of terminals that would have allowed advancing beyond the right-most position the parser reached (before it back-tracked).

Did you try a single token or ultra-simple grammar, working up as you go?

Did you try setting parser.consume_all_input = false, to see whether it was parsing correctly but not to the end of the input?

There are a few more "traps for young players" but you haven't given us enough information to go on. Once you "get it", developing in Treetop is a breeze, but it can take a little while to get to that point.

Upvotes: 2

Related Questions