[Q] TreeTop action codes

24 views
Skip to first unread message

Moon Ho Hwang

unread,
Dec 23, 2010, 10:38:36 AM12/23/10
to treet...@googlegroups.com
Hi TreeTop users,
I am trying to understand action parts when a rule is satisfied in TreeTop.
In the document of
http://treetop.rubyforge.org/syntactic_recognition.html, we can find
the following example.
--------------------------
rule alts
( foo bar / baz '' )
{
def value
elements.map{ |e| e.text_value }
end
}
end
--------------------------
I understood the above codes within '{'  '}' works as the "action"
code like in bison. I guess there are two predefined (member or global
) data: elements and text_value. It looks like elements contains all
'text_value' s. Can we say like that?

Here is another example that I got from the Internet.
---------------------------
grammar SimpleHTML
rule document
(text / tag)* {
def content
elements.map{ |e| e.content }
end
}
end

rule text
[^<]+ {
def content
[:text, text_value]
end
}
end

rule tag
"<" [^>]+ ">" {
def content
[:tag, text_value]
end
}
end
end
--------------------------
And the sample code use this parser class as

pp SimpleHTMLParser.new.parse(html).content

-------
How can elements contains all hash elements (that can be returned from
content function) for the cases of text and tag rules?
What are the implicit rules for action parts?

Moon

Justin Collins

unread,
Dec 23, 2010, 12:29:33 PM12/23/10
to Treetop Development

You can find descriptions of the methods available on SyntaxNodes at
the very bottom of http://treetop.rubyforge.org/semantic_interpretation.html

The 'elements' method returns the SyntaxNodes from the sequence. In
this case, they have a 'content' method defined for those nodes, which
is called in the 'document' rule.

Hope that helps.

-Justin

On Dec 23, 7:38 am, Moon Ho Hwang <moonho.hw...@gmail.com> wrote:
> Hi TreeTop users,
> I am trying to understand action parts when a rule is satisfied in TreeTop.
> In the document ofhttp://treetop.rubyforge.org/syntactic_recognition.html, we can find

Clifford Heath

unread,
Dec 23, 2010, 3:21:40 PM12/23/10
to treet...@googlegroups.com
On 24/12/2010, at 2:38 AM, Moon Ho Hwang wrote:
> I am trying to understand action parts when a rule is satisfied in
> TreeTop.
> I understood the above codes within '{' '}' works as the "action"
> code like in bison.

No, it's not at all like in bison. In treetop, the code in {...} is just
a module containing methods which are mixed in to the node.

This code never gets executed unless you execute it, whereas
in bison it gets executed *during* the parse as each rule is
matched.

In Treetop, after the parse is finished and you have a parse tree,
you should call one of these methods to produce whatever result
you need. Don't try to use the tree itself as the result.

Clifford Heath.

Moon Ho Hwang

unread,
Dec 23, 2010, 9:23:41 PM12/23/10
to treet...@googlegroups.com
Sounds interesting to me because very different from bison.
As Justin recommended, I tried to understand the contents of
http://treetop.rubyforge.org/semantic_interpretation.html.
============ ParenLanguage.tt ===
grammar ParenLanguage
rule parenthesized_letter
( '(' parenthesized_letter ')' / [a-z]*) {
def depth
if nonterminal?
parenthesized_letter.depth + 1
else
0
end
end
}
end
end
===============================
================ sample.rb =============
require "treetop"
require "polyglot"
$:.push '.' # push the current directoy path
require "ParenLanguage"
require "pp"

str = %q{((aacd))}

puts "test "+str
parser = ParenLanguageParser.new
pp parser.parse(str).depth
===================================

But I got the following error:
(eval):18:in `depth': undefined local variable or method
`parenthesized_letter' for #<Treetop::Runtime::SyntaxNode:0xe83cc8>
(NameError)
Why?

As your explanation, parser.parse(str) returns SyntaxNode.
What's the functionality "pp" of pp parser.parse(str).depth?

Thanks a lot, guys.

Moon
PS: The following one has the same error as above. I cut the ParenNode
code into above sample.rb.
================
# in .treetop file
grammar ParenLanguage
rule parenthesized_letter
'(' parenthesized_letter ')' <ParenNode>
/
[a-z] <ParenNode>
end
end

# in separate .rb file
class ParenNode < Treetop::Runtime::SyntaxNode
def depth
if nonterminal?
parenthesized_letter.depth + 1
else
0
end
end
end
===========

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

--
Moon Ho Hwang
http://moonho.hwang.googlepages.com/

Justin Collins

unread,
Dec 24, 2010, 3:28:09 AM12/24/10
to Treetop Development
When [a-z]* matches, then there is no parenthesized_letter method
created to call.

[a-z]* Is not a terminal node. "a", "b", "c", etc. are.

-Justin

On Dec 23, 6:23 pm, Moon Ho Hwang <moonho.hw...@gmail.com> wrote:
> Sounds interesting to me because very different from bison.
> As Justin recommended, I tried to understand the contents ofhttp://treetop.rubyforge.org/semantic_interpretation.html.
Reply all
Reply to author
Forward
0 new messages