0

我正在尝试学习使用 Treetop PEG 语法解析器,但我从一开始就遇到了奇怪的错误。

我有这个文件结构

node_extensions.rb parser.rb          tranlan.treetop

文件内容如下(列表按上面列出的文件顺序)

node_extensions.rb

# node_extensions.rb
module TranLan

end

解析器.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

当我运行 parser.rb 时,我收到此错误

/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>'

怎么了?有什么帮助吗?

4

1 回答 1

1

您有多个错误:

  • 您正在尝试加载 tranlan_parser 而不是 tranlan
  • 您正在尝试加载 .treetop 文件,但它被称为 .tt
  • 您正在尝试实例化 SexpParser 但您创建了 TranLanParser
  • TranLanParser 没有规则,所以没有顶级规则,所以不包含解析器
  • 你不需要做所有花哨的文件名修改。只需要文件。
  • 您不需要 Parser 类,Treetop 会为您生成一个(重新打开它以扩展​​它)

这是一个开始。解决这些问题,您就可以开始编写语法了。

于 2014-12-26T11:16:02.290 回答