# Help needed about Treetop::Runtime::SyntaxNode

**URL:** <https://www.ruby-forum.com/t/help-needed-about-treetop-syntaxnode/172582>\
**Category:** Ruby\
**Created:** [July 23, 2009, 3:46pm UTC](https://www.ruby-forum.com/t/help-needed-about-treetop-syntaxnode/172582 "2009-07-23T15:46:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcll](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jcll](https://www.ruby-forum.com/u/jcll)\
**Post date:** [July 23, 2009, 3:46pm UTC](https://www.ruby-forum.com/t/help-needed-about-treetop-syntaxnode/172582/1 "2009-07-23T15:46:12Z")

</div>

Hello,

I am new to Treetop : in the following example, I’d like to get an  
AST, but I am having troubles with Treetop::Runtime::SyntaxNode. I  
though I was manipulating my own ast types instead…

It is probably very awkward.

Can someone help ?  
Thanks  
JC

ruby FoobarCompiler.rb gives :  
test 0 : parsing aa:1,2,3,14  
-Success for parsing  
-top level AST instanceof : Program  
./ast.rb:8:in `print': private method`print’ called for  
#Treetop::Runtime::SyntaxNode:0xb7d70db8 (NoMethodError)  
from ./ast.rb:8:in `map' from ./ast.rb:8:in`print’  
from FoobarCompiler.rb:18:in `parse' from FoobarCompiler.rb:27:in`test’  
from FoobarCompiler.rb:40

# ========treetop grammar for things like " aa:1,2,3,4" =============

require ‘ast’

grammar Foobar

rule program  
ws lit ‘:’ num tail:(’,’ num)\*  
{  
def build  
Program.new all  
end

```
   def all
    [lit] + [num.build] + tail.elements.map {|e| e.num.build}
   end
 }

```

end

rule lit  
[a-z]+  
{  
def build  
Lit.new(“foo”)  
end  
}  
end

rule num  
[0-9]+  
{  
def build  
Num.new(text\_value)  
end  
}  
end

rule ws  
[\n\t]\*  
end  
end

#==========================================================  
class FoobarCompiler

def initialize  
@parser = FoobarParser.new  
end

def parse(txt)  
root=@parser.parse(txt)  
if root  
puts “\t-Success for parsing”  
@tree=@parser.parse(txt).build  
puts “\t-top level AST instanceof : #{@tree.class}”  
@tree.print  
else  
puts “Failure for parsing : #{@parser.failure\_message}”  
end

end

def test num,goal,txt  
puts “test #{num}”.ljust(7)+" : #{goal} ".ljust(50)  
parse txt  
end

end

program\_0 = “aa:1,2,3,14”  
c=FoobarCompiler.new()  
c.test 0, “parsing #{program\_0}”, program\_0

#=====ast.rb ==============  
include Treetop::Runtime

class Ast \< Treetop::Runtime::SyntaxNode  
def initialize(sub=nil)  
@subtrees=sub  
end  
def print  
puts @subtrees.map{|t| t.print}  
end  
end

class Program \< Ast  
def initialize(subt=nil)  
super(subt)  
end  
end

class Num \< Ast  
def initialize(val)  
super  
@val=val  
end

```
def print
  "value=#{@val}"
end

```

end

class Lit \< Ast  
def initialize(val)  
super  
@txt=val  
end  
def print  
“literal=#{@txt}”  
end  
end

---

<div class="post-metadata">

**Author:** ![jcll](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/c77e96/32.png) [@jcll](https://www.ruby-forum.com/u/jcll)\
**Post date:** [July 23, 2009, 9:06pm UTC](https://www.ruby-forum.com/t/help-needed-about-treetop-syntaxnode/172582/2 "2009-07-23T21:06:08Z")

</div>

> ```
> from FoobarCompiler.rb:40
> 
> ```

Sorry !

I finally found my error, which a typo : (lit.build instead of lit)

def all  
[lit.build] + [num.build] + tail.elements.map {|e| e.num.build}  
end

test 0 : parsing aa:1,2,3,14  
-Success for parsing  
-top level AST instanceof : Program  
literal=foo  
value=1  
value=2  
value=3  
value=14

Bye !  
Jean-Christophe
