cjheath / treetop

A Ruby-based parsing DSL based on parsing expression grammars.
https://cjheath.github.io/treetop
MIT License
306 stars 22 forks source link

Potential Bug: Had to move a rule to the top of the grammar #19

Closed spundun closed 10 years ago

spundun commented 10 years ago

Hi,

It's my first time using treetop gem and I'm following this tutorial: http://thingsaaronmade.com/blog/a-quick-intro-to-writing-a-parser-using-treetop.html

I kept getting an error

spundun$ bundle exec pry
[1] pry(main)> require './parser'
=> true
[2] pry(main)> Parser.parse('(this "is" a test( 1 2.0 3))')
Exception: Parse error at offset: 0
from /Users/spundun/Documents/code_samples/s_expression_parser/parser.rb:35:in `parse'
[3] pry(main)> 

Then I moved the body rule in the sexp_parser.treetop to the top of the grammar and everything works.

Is this by design? Is there a reason for it? It seems like a bug, and at least the tutorial is inconsistent with treetop 1.5 .

You can find all the code in the gist that I have created. https://gist.github.com/spundun/92ce4524c9fae95a944e

cjheath commented 10 years ago

Your two invocations of the parser look the same to me. Should I be seeing some difference that I've overlooked?

Treetop will always attempt to match the first rule specified in the grammar, unless you pass an alternate top rule to the parse function, using the options hash. I think this probably explains your problem.

spundun commented 10 years ago

Yes the second paragraph explains the problem. In gist I put the working code (with the root rule at the top) in the tutorial from thingsaaronmade.com that rule was not at the top. I am wondering if that is a change that happened to treetop after that tutorial blog.

spundun commented 10 years ago

Nevermind, I can see that he put the correct rule at the top here: https://github.com/aarongough/treetop-sexp-parser/blob/master/sexp_parser.treetop

cjheath commented 10 years ago

I don't think this aspect of Treetop has ever changed.

spundun commented 10 years ago

You are right, the author of the tutorial just skipped mentioning it. Speaking of which, it is also not mentioned in here: http://cjheath.github.io/treetop/syntactic_recognition.html I kinda discovered it by experimenting. You may want to write it down somewhere.

BTW Thanks for the prompt responses.