Ruby解析字符串

ast*_*nic 5 ruby string parsing

我有一个字符串

input = "maybe (this is | that was) some ((nice | ugly) (day |night) | (strange (weather | time)))"
Run Code Online (Sandbox Code Playgroud)

Ruby中解析此字符串的最佳方法是什么?

我的意思是脚本应该能够像这样构建sententes:

也许这是一个丑陋的夜晚

也许这是一个美好的夜晚

也许这是一段奇怪的时间

等等,你明白了......

我应该通过char读取字符串char并使用堆栈来建立状态机以存储括号值以供稍后计算,还是有更好的方法?

为此目的,也许是一个现成的,开箱即用的库?

mol*_*olf 8

试试Treetop.它是一个类似Ruby的DSL来描述语法.解析你给出的字符串应该非常简单,通过使用真正的解析器,你可以很容易地扩展你的语法.

要解析的字符串类型的示例语法(另存为sentences.treetop):

grammar Sentences
  rule sentence
    # A sentence is a combination of one or more expressions.
    expression* <Sentence>
  end

  rule expression
    # An expression is either a literal or a parenthesised expression.
    parenthesised / literal
  end

  rule parenthesised
    # A parenthesised expression contains one or more sentences.
    "(" (multiple / sentence) ")" <Parenthesised>
  end

  rule multiple
    # Multiple sentences are delimited by a pipe.
    sentence "|" (multiple / sentence) <Multiple>
  end

  rule literal
    # A literal string contains of word characters (a-z) and/or spaces.
    # Expand the character class to allow other characters too.
    [a-zA-Z ]+ <Literal>
  end
end
Run Code Online (Sandbox Code Playgroud)

上面的语法需要一个附带的文件,它定义了允许我们访问节点值的类(另存为sentence_nodes.rb).

class Sentence < Treetop::Runtime::SyntaxNode
  def combine(a, b)
    return b if a.empty?
    a.inject([]) do |values, val_a|
      values + b.collect { |val_b| val_a + val_b }
    end
  end

  def values
    elements.inject([]) do |values, element|
      combine(values, element.values)
    end
  end
end

class Parenthesised < Treetop::Runtime::SyntaxNode
  def values
    elements[1].values
  end
end

class Multiple < Treetop::Runtime::SyntaxNode
  def values
    elements[0].values + elements[2].values
  end
end

class Literal < Treetop::Runtime::SyntaxNode
  def values
    [text_value]
  end
end
Run Code Online (Sandbox Code Playgroud)

以下示例程序显示解析您给出的示例句子非常简单.

require "rubygems"
require "treetop"
require "sentence_nodes"

str = 'maybe (this is|that was) some' +
  ' ((nice|ugly) (day|night)|(strange (weather|time)))'

Treetop.load "sentences"
if sentence = SentencesParser.new.parse(str)
  puts sentence.values
else
  puts "Parse error"
end
Run Code Online (Sandbox Code Playgroud)

该程序的输出是:

maybe this is some nice day
maybe this is some nice night
maybe this is some ugly day
maybe this is some ugly night
maybe this is some strange weather
maybe this is some strange time
maybe that was some nice day
maybe that was some nice night
maybe that was some ugly day
maybe that was some ugly night
maybe that was some strange weather
maybe that was some strange time
Run Code Online (Sandbox Code Playgroud)

您还可以访问语法树:

p sentence
Run Code Online (Sandbox Code Playgroud)

输出在这里.

你有它:一个可扩展的解析解决方案,应该与你想要在大约50行代码中做的非常接近.这有帮助吗?