【问题标题】:How do I process a treetop parse tree?如何处理树顶解析树?
【发布时间】:2016-08-07 20:22:33
【问题描述】:

我已经使用treetop 编写了一个解析器,它成功生成了一个解析树,下面转载了其中的一部分。

SyntaxNode offset=4043, "   ":
  SyntaxNode offset=4043, " "
  SyntaxNode offset=4044, " "
  SyntaxNode offset=4045, " "
StringLiteral+StringLiteral1 offset=4046, "\"MS Sans Serif\"":
  SyntaxNode offset=4046, "\""
  SyntaxNode offset=4047, "MS Sans Serif":
    SyntaxNode+StringLiteral0 offset=4047, "M":
      SyntaxNode offset=4047, ""
      SyntaxNode offset=4047, "M"
    SyntaxNode+StringLiteral0 offset=4048, "S":
      SyntaxNode offset=4048, ""
      SyntaxNode offset=4048, "S"
    SyntaxNode+StringLiteral0 offset=4049, " ":
      SyntaxNode offset=4049, ""
      SyntaxNode offset=4049, " "
    SyntaxNode+StringLiteral0 offset=4050, "S":
      SyntaxNode offset=4050, ""
      SyntaxNode offset=4050, "S"
    SyntaxNode+StringLiteral0 offset=4051, "a":
      SyntaxNode offset=4051, ""
      SyntaxNode offset=4051, "a"
    SyntaxNode+StringLiteral0 offset=4052, "n":
      SyntaxNode offset=4052, ""
      SyntaxNode offset=4052, "n"
    SyntaxNode+StringLiteral0 offset=4053, "s":
      SyntaxNode offset=4053, ""
      SyntaxNode offset=4053, "s"
    SyntaxNode+StringLiteral0 offset=4054, " ":
      SyntaxNode offset=4054, ""
      SyntaxNode offset=4054, " "
    SyntaxNode+StringLiteral0 offset=4055, "S":
      SyntaxNode offset=4055, ""
      SyntaxNode offset=4055, "S"
    SyntaxNode+StringLiteral0 offset=4056, "e":
      SyntaxNode offset=4056, ""
      SyntaxNode offset=4056, "e"
    SyntaxNode+StringLiteral0 offset=4057, "r":
      SyntaxNode offset=4057, ""
      SyntaxNode offset=4057, "r"
    SyntaxNode+StringLiteral0 offset=4058, "i":
      SyntaxNode offset=4058, ""
      SyntaxNode offset=4058, "i"
    SyntaxNode+StringLiteral0 offset=4059, "f":
      SyntaxNode offset=4059, ""
      SyntaxNode offset=4059, "f"
  SyntaxNode offset=4060, "\""

现在我有了这棵树,我不知道如何过滤它,以便只处理匹配特定规则的特定节点。

我想用一个标识符替换字符串文字,该标识符引用字符串文件中的字符串。


cool_parser.treetop

rule string_literal
  '"' (!'"' . / '""')* '"'
end

require 'treetop'

# Load the grammar
Treetop.load 'cool'

class Parser

  # Create the parser
  @@parser = CoolParser.new

  def self.parse(data)

    # Pass the data over to the parser instance
    tree = @@parser.parse(data)

    if(tree.nil?)
      raise Exception, "Parse error at offset: #{@@parser.index}"
    end

    return tree
  end

end

tree = Parser.parse(File.open("myfile.txt").read)

puts tree.inspect

【问题讨论】:

标签: ruby parsing treetop


【解决方案1】:

我不确定您所说的“替换为标识符”是什么意思?但是,这是一个将字符串节点的值作为数组返回的示例。


cool_parser.treetop

   rule start
      (not_string_literal / string_literal)*
      {
         def value
            elements.map {|e| e.value }.compact
         end
      }
   end

   rule string_literal
      '"' (!'"' . / '""')* '"'
      {
         def value
            text_value
         end
      }
   end

   rule not_string_literal
      !string_literal .
      {
         def value
         end
      }
   end

然后访问解析对象新添加的value方法:

tree = Parser.parse(File.open("myfile.txt").read)

puts tree.value.inspect

# => ["\"strings\"", "\"sentences\""]

【讨论】:

    猜你喜欢
    • 2018-03-18
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多