0

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?

Martin Macak
  • 3,507
  • 2
  • 30
  • 54

1 Answers1

1

You have multiple errors:

  • You are trying to load tranlan_parser instead of tranlan
  • You are trying to load a .treetop file but it's called .tt
  • You are trying to instantiate SexpParser but you created TranLanParser
  • TranLanParser has no rule, so no top rule, so contains no parser
  • You don't need to be doing all that fancy filename-munging. Just require the files.
  • You don't need a Parser class, Treetop generates one for you (re-open it to extend it)

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

cliffordheath
  • 2,536
  • 15
  • 16