Help needed about Treetop::Runtime::SyntaxNode.

J

JCLL

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)
[email protected](txt)
if root
puts "\t-Success for parsing"
@[email protected](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
 
J

JCLL

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top