ruby on rails - Issues using treetop parser library -


i trying create simple text parser in ruby using treetop. although have followed steps mentioned in blog, not being able run program. fails error message:

user1-mbp15:source user1$ ruby myparser.rb  (eval):28:in `_nt_expression': undefined local variable or method `_nt_space' #<sexpparser:0x007fad9b92b210> (nameerror)    /usr/local/lib/ruby/gems/2.3.0/gems/treetop-1.6.5/lib/treetop/runtime/compiled_parser.rb:18:in `parse'    myparser.rb:19:in `parse'    myparser.rb:31:in `<main>' 

i not find lot of resources on web on treetop, glad help. following code:

user1-mbp15:source user1$ ls    myparser.rb    node_extensions.rb    sexp_extensions.rb    sexp_parser.treetop 

-- myparser.rb --

# in file myparser.rb require 'treetop'  # find out our base path $base_path = file.expand_path(file.dirname(__file__))  # load our custom syntax node classes parser can use them require file.join($base_path, 'node_extensions.rb') class parser    # load treetop grammar 'sexp_parser' file, ,     # create new instance of parser class variable     # don't have re-create every time need     # parse string    treetop.load(file.join($base_path, 'sexp_parser.treetop'))    @@parser = sexpparser.new     def self.parse(data)       # pass data on parser instance       tree = @@parser.parse(data)        # if ast nil there error during parsing       # need report simple error message user       if(tree.nil?)          raise exception, "parse error @ offset: #{@@parser.index}"       end        return tree    end end  parser.parse('(this "is" test( 1 2.0 3))') 

-- node_extensions.rb --

module sexp    class integerliteral < treetop::runtime::syntaxnode    end     class stringliteral < treetop::runtime::syntaxnode    end     class floatliteral < treetop::runtime::syntaxnode    end     class identifier < treetop::runtime::syntaxnode    end     class expression < treetop::runtime::syntaxnode    end     class body < treetop::runtime::syntaxnode    end end 

-- sexp_extensions.rb --

grammar sexp     rule integer         ('+' / '-')? [0-9]+ <integerliteral>     end      rule float         ('+' / '-')? [0-9]+ (('.' [0-9]+) / ('e' [0-9]+)) <floatliteral>     end      rule string         '"' ([^"\\] / "\\" . )* '"' <stringliteral>     end      rule identifier         [a-za-z\=\*] [a-za-z0-9_\=\*]* <identifier>     end      rule space         [\s]+     end end 

-- sep_parser.treetop --

# in file sexp_parser.treetop grammar sexp     rule expression         space? '(' body ')' space? <expression>     end      rule body         (expression / identifier / float / integer / string / space )* <body>     end end 

i able solve this. error here sexp_extensions.rb used nowhere. copy code sexp_extensions.rb sep_parser.treetop, , works!


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -