[Q] stack level too deep error

37 views
Skip to first unread message

Moon Ho Hwang

unread,
Dec 24, 2010, 4:59:28 PM12/24/10
to treet...@googlegroups.com
Hi All,
I have a grammar file named simple_graph.tt
=========== simple_graph.tt ==================
# A grammar for defining a Graph
grammar SimpleG
rule graph
edge*
{
def MakeG(v)
elements.map { |e| e.MakeG(v) }
end
}
end

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/

Clifford Heath

unread,
Dec 24, 2010, 6:03:46 PM12/24/10
to treet...@googlegroups.com
On 25/12/2010, at 8:59 AM, Moon Ho Hwang wrote:
> I have a grammar file named simple_graph.tt

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

Moon Ho Hwang

unread,
Dec 25, 2010, 10:09:51 AM12/25/10
to treet...@googlegroups.com
Thanks a lot, Cliff!
Let me focus the left-recursive rule of PEG rather than handling white
spaces in this mail.
(I will try out your recommendation of white space handling later).

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.

test_simple_graph.rb
comment.tt
simple_graph.tt

Clifford Heath

unread,
Dec 25, 2010, 4:45:38 PM12/25/10
to treet...@googlegroups.com
Sorry Moon, but I'm going on vacation for a week - back on the 4th.
I hope someone else can help you.

You need to iterate through the elements - there are examples on the
website.

Clifford Heath.

> <test_simple_graph.rb><comment.tt><simple_graph.tt>

Moon Ho Hwang

unread,
Dec 25, 2010, 6:38:02 PM12/25/10
to treet...@googlegroups.com
Thanks a lot, Cliff.
I will try to search some clue to solve it myself.

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>

Reply all
Reply to author
Forward
0 new messages