rubyparsingpegtreetop

Ruby & Treetop - No such file or directory @ rb_sysopen


I am trying to learn working with Treetop PEG grammar parser, but I am getting weird error straight from beginning.

I have this file structure

node_extensions.rb parser.rb          tranlan.treetop

And contents of files is following (listings are in order of files listed above)

node_extensions.rb

# node_extensions.rb
module TranLan

end

parser.rb

# parser.rb
require 'treetop'

# Find out what our base path is
base_path = File.expand_path(File.dirname(__FILE__))

# Load our custom syntax node classes so the parser can use them
require File.join(base_path, 'node_extensions.rb')

class Parser
  base_path = File.expand_path(File.dirname(__FILE__))
  Treetop.load(File.join(base_path, 'tranlan_parser.treetop'))
  @@parser = SexpParser.new

  def self.parse(data)
    tree = @@parser.parse(data)
    raise Exception, "Parser error at offset: #{@@parser.index}" if tree.nil?
    tree
  end
end

tranlan.treetop

# tranlan.treetop
grammar TranLan

end

When I run parser.rb, I get this error

/Users/maca/.rvm/gems/ruby-2.1.4/gems/treetop-1.5.3/lib/treetop/compiler/grammar_compiler.rb:37:in `initialize': No such file or directory @ rb_sysopen - /Users/maca/devel/playground/treetop-grammar/tranlan_parser.treetop (Errno::ENOENT)
from /Users/maca/.rvm/gems/ruby-2.1.4/gems/treetop-1.5.3/lib/treetop/compiler/grammar_compiler.rb:37:in `open'
from /Users/maca/.rvm/gems/ruby-2.1.4/gems/treetop-1.5.3/lib/treetop/compiler/grammar_compiler.rb:37:in `load'
from parser.rb:17:in `<class:Parser>'
from parser.rb:10:in `<main>'

What's wrong? Any help?


Solution

  • You have multiple errors:

    That's a start. Fix those things and you'll be able to start writing a grammar.