how can I debug treetop grammar?

L

Lex Williams

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 !
 
C

Clifford Heath

Lex said:
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 Heath.
 
L

Lex Williams

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 Heath.

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 !
 
L

Lex Williams

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top