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