rule edge
edge "=>" word
{
def MakeG(v)
elements[2].MakeG(v)
end
}
/
word "=>" word
{
def MakeG(v)
elements[0].MakeG(v)
elements[2].MakeG(v)
end
}
end
rule word
([\s]* [\w]+ [\s]* / [\s]* quoted_string [\s]*)
{
def MakeG(v)
v << elements[1].text_value
end
}
end
rule quoted_string
( ["] ( !["] (. / [\n]) )* ["] / # double quoted
['] ( !['] (. / [\n]) )* ['] # single quoted
)
end
# include Comment
end # of grammar
================ end of simple_graph.tt ============
Here is a Ruby file consuming the above grammar file.
------------------------------------------------------------------------test.rb
---
require "treetop"
require "polyglot"
$:.push '.' # push the current directoy path
require "pp"
require "comment" # this one is also needs
require "simple_graph"
str = %q{
aa => ba
cc => dd
11 => 22e32
"P (a) " => 'P (c ) '
#xxx => yyy => zzz
}
puts str
p = SimpleGParser.new
v = []
node = p.parse(str)
node.MakeG(v)
puts v.size
puts v
------------------------------------------------------------- end of test.rb
If I commented out the following recursive rule of edge in simple_graph.tt file
# edge "=>" word
# {
# def MakeG(v)
# elements[2].MakeG(v)
# end
# }
# /
Above one has no stack level too deep error but I could not parse the
following form of edges.
xxx => yyy => zzz
Any comments or suggestions will be very helpful for me at this status.
Thank you guys in advance.
--
Moon Ho Hwang
http://moonho.hwang.googlepages.com/
> rule edge
> edge "=>" word
> Above one has no stack level too deep error but I could not parse the
> following form of edges.
Your grammar is left-recursive. PEGs cannot handle left recursion
(at least, without some tweaks that Treetop doesn't have). You're
saying to Treetop "to get an edge, first get an edge".
In this case, it looks easy to refactor like this:
rule edge
word "=>" edge
/ word
end
or
rule edge
word ("=>" edge)*
end
I don't like the way you're embedding all your white-space
handling into "word". it might work for now, but it's not a good
pattern. I try to arrange for each invoked rule to skip trailing
whitespace, and then insert any additional skipping where
needed.
Clifford Heath.
I changed the "edge" rule as below.
rule edge
word ('=>' edge)*
{
def MakeG(v)
elements[0].MakeG(v)
elements[2].MakeG(v) if elements[2] != nil
end
}
end
I expected I can get 8 vertices but I can only 4 vertices when I run
test_graph.rb.
Let me attached tt file and rb so you can take a look at them.
Thanks.
Moon
> --
> You received this message because you are subscribed to the Google Groups
> "Treetop Development" group.
> To post to this group, send email to treet...@googlegroups.com.
> To unsubscribe from this group, send email to
> treetop-dev...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/treetop-dev?hl=en.
You need to iterate through the elements - there are examples on the
website.
Clifford Heath.
> <test_simple_graph.rb><comment.tt><simple_graph.tt>
Have a good vacation
Moon
On Sat, Dec 25, 2010 at 4:45 PM, Clifford Heath
>> <test_simple_graph.rb><comment.tt><simple_graph.tt>