# How can I debug treetop grammar?

**URL:** <https://www.ruby-forum.com/t/how-can-i-debug-treetop-grammar/147202>\
**Category:** Ruby\
**Created:** [August 29, 2008, 10:01am UTC](https://www.ruby-forum.com/t/how-can-i-debug-treetop-grammar/147202 "2008-08-29T10:01:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lpc2148](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lpc2148](https://www.ruby-forum.com/u/lpc2148)\
**Post date:** [August 29, 2008, 10:01am UTC](https://www.ruby-forum.com/t/how-can-i-debug-treetop-grammar/147202/1 "2008-08-29T10:01:39Z")

</div>

Hi guys !

Parse::RecDescent has RD\_TRACE , which when is set to 1 , tells you  
which rule the parser it’s trying , what it matches , what part of the  
input consumes , and so on . Does treetop have an equivalent to this ?  
I don’t really find the documentation available for it to be that good ,  
and calling parser.failure\_line and parser.failure\_column returns 1,1 no  
matter what. Help !

---

<div class="post-metadata">

**Author:** ![lpc2148](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lpc2148](https://www.ruby-forum.com/u/lpc2148)\
**Post date:** [August 29, 2008, 3:48pm UTC](https://www.ruby-forum.com/t/how-can-i-debug-treetop-grammar/147202/2 "2008-08-29T15:48:44Z")

</div>

Lex W. wrote:

> Parse::RecDescent has RD\_TRACE , which when is set to 1 , tells you  
> which rule the parser it’s trying , what it matches , what part of the  
> input consumes , and so on . Does treetop have an equivalent to this ?

Each SyntaxNode has “interval”, which is the range of byte-offsets  
matched,  
and text\_value, which returns that interval from the input.

You seem to have missed some fine diagnostic methods:

SyntaxNode#inspect shows a wealth of information, including an indented  
node hierarchy, what input that node matched (abbreviated) and what  
accessor methods are added by the modules extended into each node.

Parser#failure\_reason tells you where it got to, and what tokens would  
have allowed it to get further.

If that isn’t enough, try calling your grammar from a lower-level rule  
and working up until you see where it’s failing.

Also don’t forget that there’s a “tt” command you can use to generate  
the grammar into a ruby file you can inspect.

As for your earlier query, it’s a known bug.

Clifford H…

---

<div class="post-metadata">

**Author:** ![lpc2148](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lpc2148](https://www.ruby-forum.com/u/lpc2148)\
**Post date:** [August 29, 2008, 7:00pm UTC](https://www.ruby-forum.com/t/how-can-i-debug-treetop-grammar/147202/3 "2008-08-29T19:00:30Z")

</div>

> SyntaxNode#inspect shows a wealth of information, including an indented  
> node hierarchy, what input that node matched (abbreviated) and what  
> accessor methods are added by the modules extended into each node.
> 
> Parser#failure\_reason tells you where it got to, and what tokens would  
> have allowed it to get further.
> 
> If that isn’t enough, try calling your grammar from a lower-level rule  
> and working up until you see where it’s failing.
> 
> Also don’t forget that there’s a “tt” command you can use to generate  
> the grammar into a ruby file you can inspect.
> 
> As for your earlier query, it’s a known bug.
> 
> Clifford H…

Thank you Clifford , I really appreciate you taking the time to answer  
this .  
I will look into them , and post here whatever questions I will have !

---

<div class="post-metadata">

**Author:** ![lpc2148](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lpc2148](https://www.ruby-forum.com/u/lpc2148)\
**Post date:** [August 29, 2008, 8:53pm UTC](https://www.ruby-forum.com/t/how-can-i-debug-treetop-grammar/147202/4 "2008-08-29T20:53:33Z")

</div>

Would you please walk me through a debugging example ? Considering this  
grammar :

grammar Dp  
rule all  
day space month {  
def value  
day.value.to\_s + " " + month.value  
end  
}  
end

rule day  
[1-9] [1-9] / “” {  
def value  
text\_value.to\_i  
end  
}  
end

```
    rule space
            ' '*
    end

    rule month
            "Jan" / "Feb" / "Mar" / "Apr" / "May" / "Jun" / "Jul" / 

```

“Aug” / “Sep” / “Oct” / “Nov” / “Dec” / “” {  
def value  
text\_value  
end  
}  
end

end

and the following code :

equire “rubygems”  
require “treetop”

Treetop.load “dp”

parser = DpParser.new  
node = parser.parse(“12”)  
another = parser.parse(“Feb”)  
lastone = parser.parse(“12 Feb”)  
puts node.value  
puts another.value  
puts lastone.value

* * *

i’m getting back syntax nodes , but I can’t call value on any of them .  
could you please show me how you would debug the rules ?  
hope I’m not asking for too much !  
Thanks ,  
Lex
