2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2007-08-18 13:04:06 -04:00
|
|
|
# == sentence library
|
|
|
|
#
|
|
|
|
# = Features
|
|
|
|
#
|
|
|
|
# * syntax based sentences generation
|
|
|
|
# * sentence operations such as substitution.
|
|
|
|
#
|
|
|
|
# = Example
|
|
|
|
#
|
|
|
|
# Some arithmetic expressions using "+", "-", "*" and "/" are generated as follows.
|
|
|
|
#
|
|
|
|
# require 'sentence'
|
|
|
|
# Sentence.each({
|
|
|
|
# :exp => [["num"],
|
|
|
|
# [:exp, "+", :exp],
|
|
|
|
# [:exp, "-", :exp],
|
|
|
|
# [:exp, "*", :exp],
|
|
|
|
# [:exp, "/", :exp]]
|
|
|
|
# }, :exp, 2) {|sent| p sent }
|
|
|
|
# #=>
|
|
|
|
# #<Sentence: "num">
|
|
|
|
# #<Sentence: ("num") "+" ("num")>
|
|
|
|
# #<Sentence: ("num") "+" (("num") "+" ("num"))>
|
|
|
|
# #<Sentence: ("num") "+" (("num") "-" ("num"))>
|
|
|
|
# #<Sentence: ("num") "+" (("num") "*" ("num"))>
|
|
|
|
# #<Sentence: ("num") "+" (("num") "/" ("num"))>
|
|
|
|
# #<Sentence: (("num") "+" ("num")) "+" ("num")>
|
|
|
|
# ...
|
|
|
|
#
|
|
|
|
# Sentence.each takes 3 arguments.
|
|
|
|
# The first argument is the syntax for the expressions.
|
|
|
|
# The second argument, :exp, is a generating nonterminal.
|
|
|
|
# The third argument, 2, limits derivation to restrict results finitely.
|
|
|
|
#
|
|
|
|
# Some arithmetic expressions including parenthesis can be generated as follows.
|
2007-08-19 11:43:04 -04:00
|
|
|
#
|
2007-08-18 23:37:14 -04:00
|
|
|
# syntax = {
|
2007-08-18 13:04:06 -04:00
|
|
|
# :factor => [["n"],
|
|
|
|
# ["(", :exp, ")"]],
|
|
|
|
# :term => [[:factor],
|
|
|
|
# [:term, "*", :factor],
|
|
|
|
# [:term, "/", :factor]],
|
|
|
|
# :exp => [[:term],
|
|
|
|
# [:exp, "+", :term],
|
|
|
|
# [:exp, "-", :term]]
|
|
|
|
# }
|
|
|
|
# Sentence.each(syntax, :exp, 2) {|sent| p sent }
|
|
|
|
# #=>
|
|
|
|
# #<Sentence: (("n"))>
|
|
|
|
# #<Sentence: (("(" ((("n"))) ")"))>
|
|
|
|
# #<Sentence: (("(" ((("(" ((("n"))) ")"))) ")"))>
|
|
|
|
# #<Sentence: (("(" (((("n")) "*" ("n"))) ")"))>
|
|
|
|
# #<Sentence: (("(" (((("n")) "/" ("n"))) ")"))>
|
|
|
|
# #<Sentence: (("(" (((("n"))) "+" (("n"))) ")"))>
|
|
|
|
# #<Sentence: (("(" (((("n"))) "-" (("n"))) ")"))>
|
|
|
|
# #<Sentence: ((("n")) "*" ("n"))>
|
|
|
|
# #<Sentence: ((("n")) "*" ("(" ((("n"))) ")"))>
|
|
|
|
# ...
|
|
|
|
#
|
|
|
|
# Sentence#to_s can be used to concatenate strings
|
|
|
|
# in a sentence:
|
|
|
|
#
|
|
|
|
# Sentence.each(syntax, :exp, 2) {|sent| p sent.to_s }
|
|
|
|
# #=>
|
|
|
|
# "n"
|
|
|
|
# "(n)"
|
|
|
|
# "((n))"
|
|
|
|
# "(n*n)"
|
|
|
|
# "(n/n)"
|
|
|
|
# "(n+n)"
|
|
|
|
# "(n-n)"
|
|
|
|
# "n*n"
|
|
|
|
# "n*(n)"
|
|
|
|
# ...
|
|
|
|
#
|
2007-08-16 06:21:16 -04:00
|
|
|
|
2007-08-18 23:37:14 -04:00
|
|
|
# Sentence() instantiates a sentence object.
|
|
|
|
#
|
|
|
|
# Sentence("foo", "bar")
|
|
|
|
# #=> #<Sentence: "foo" "bar">
|
|
|
|
#
|
|
|
|
# Sentence("foo", ["bar", "baz"])
|
|
|
|
# #=> #<Sentence: "foo" ("bar" "baz")>
|
|
|
|
#
|
|
|
|
def Sentence(*ary)
|
|
|
|
Sentence.new(ary)
|
|
|
|
end
|
|
|
|
|
2007-08-18 13:04:06 -04:00
|
|
|
# Sentence class represents a tree with string leaves.
|
|
|
|
#
|
2007-08-16 06:21:16 -04:00
|
|
|
class Sentence
|
2007-08-18 13:04:06 -04:00
|
|
|
# _ary_ represents a tree.
|
|
|
|
# It should be a possibly nested array which contains strings.
|
|
|
|
#
|
|
|
|
# Note that _ary_ is not copied.
|
|
|
|
# Don't modify _ary_ after the sentence object is instantiated.
|
|
|
|
#
|
|
|
|
# Sentence.new(["a", "pen"])
|
|
|
|
# #<Sentence: "a" "pen">
|
|
|
|
#
|
|
|
|
# Sentence.new(["I", "have", ["a", "pen"]])
|
|
|
|
# #<Sentence: "I" "have" ("a" "pen")>
|
|
|
|
#
|
2007-08-16 06:21:16 -04:00
|
|
|
def initialize(ary)
|
|
|
|
@sent = ary
|
|
|
|
end
|
|
|
|
|
2007-08-18 13:04:06 -04:00
|
|
|
# returns a string which is concatenation of all strings.
|
|
|
|
# No separator is used.
|
|
|
|
#
|
2007-08-18 23:37:14 -04:00
|
|
|
# Sentence("2", "+", "3").to_s
|
2007-08-18 13:04:06 -04:00
|
|
|
# "2+3"
|
|
|
|
#
|
2007-08-18 23:37:14 -04:00
|
|
|
# Sentence("2", "+", ["3", "*", "5"]).to_s
|
2007-08-18 13:04:06 -04:00
|
|
|
# "2+3*5"
|
|
|
|
#
|
2007-08-16 06:21:16 -04:00
|
|
|
def to_s
|
|
|
|
@sent.join('')
|
|
|
|
end
|
|
|
|
|
2007-08-18 13:04:06 -04:00
|
|
|
# returns a string which is concatenation of all strings separated by _sep_.
|
|
|
|
# If _sep_ is not given, single space is used.
|
|
|
|
#
|
2007-08-18 23:37:14 -04:00
|
|
|
# Sentence("I", "have", ["a", "pen"]).join
|
2007-08-18 13:04:06 -04:00
|
|
|
# "I have a pen"
|
|
|
|
#
|
2007-08-18 23:37:14 -04:00
|
|
|
# Sentence("I", "have", ["a", "pen"]).join("/")
|
2007-08-18 13:04:06 -04:00
|
|
|
# "I/have/a/pen"
|
|
|
|
#
|
2007-08-18 23:37:14 -04:00
|
|
|
# Sentence("a", [], "b").join("/")
|
2007-08-18 13:04:06 -04:00
|
|
|
# "a/b"
|
|
|
|
#
|
|
|
|
def join(sep=' ')
|
|
|
|
@sent.flatten.join(sep)
|
|
|
|
end
|
|
|
|
|
|
|
|
# returns a tree as a nested array.
|
|
|
|
#
|
|
|
|
# Note that the result is not copied.
|
|
|
|
# Don't modify the result.
|
|
|
|
#
|
2007-08-18 23:37:14 -04:00
|
|
|
# Sentence(["foo", "bar"], "baz").to_a
|
2007-08-18 13:04:06 -04:00
|
|
|
# #=> [["foo", "bar"], "baz"]
|
|
|
|
#
|
2007-08-16 06:21:16 -04:00
|
|
|
def to_a
|
|
|
|
@sent
|
|
|
|
end
|
|
|
|
|
2007-08-18 13:04:06 -04:00
|
|
|
# returns <i>i</i>th element as a sentence or string.
|
|
|
|
#
|
2007-08-18 23:37:14 -04:00
|
|
|
# s = Sentence(["foo", "bar"], "baz")
|
2007-08-18 13:04:06 -04:00
|
|
|
# s #=> #<Sentence: ("foo" "bar") "baz">
|
|
|
|
# s[0] #=> #<Sentence: "foo" "bar">
|
|
|
|
# s[1] #=> "baz"
|
|
|
|
#
|
2007-08-16 06:21:16 -04:00
|
|
|
def [](i)
|
2007-08-18 13:04:06 -04:00
|
|
|
e = @sent[i]
|
|
|
|
e.respond_to?(:to_ary) ? Sentence.new(e) : e
|
|
|
|
end
|
|
|
|
|
|
|
|
# returns the number of top level elements.
|
|
|
|
#
|
2007-08-19 11:43:04 -04:00
|
|
|
# Sentence.new(%w[foo bar]).length
|
2007-08-18 13:04:06 -04:00
|
|
|
# #=> 2
|
|
|
|
#
|
2007-08-19 11:43:04 -04:00
|
|
|
# Sentence(%w[2 * 7], "+", %w[3 * 5]).length
|
2007-08-18 13:04:06 -04:00
|
|
|
# #=> 3
|
|
|
|
#
|
|
|
|
def length
|
|
|
|
@sent.length
|
2007-08-16 06:21:16 -04:00
|
|
|
end
|
|
|
|
|
2007-08-24 15:01:46 -04:00
|
|
|
# iterates over children.
|
|
|
|
#
|
|
|
|
# Sentence(%w[2 * 7], "+", %w[3 * 5]).each {|v| p v }
|
|
|
|
# #=>
|
|
|
|
# #<Sentence: "2" "*" "7">
|
|
|
|
# "+"
|
|
|
|
# #<Sentence: "3" "*" "5">
|
|
|
|
#
|
|
|
|
def each # :yield: element
|
|
|
|
@sent.each_index {|i|
|
|
|
|
yield self[i]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
include Enumerable
|
|
|
|
|
2007-08-16 06:21:16 -04:00
|
|
|
def inspect
|
2007-08-18 13:04:06 -04:00
|
|
|
"#<#{self.class}: #{inner_inspect(@sent, '')}>"
|
2007-08-16 06:21:16 -04:00
|
|
|
end
|
|
|
|
|
2007-08-18 13:04:06 -04:00
|
|
|
# :stopdoc:
|
|
|
|
def inner_inspect(ary, r)
|
|
|
|
first = true
|
|
|
|
ary.each {|obj|
|
|
|
|
r << ' ' if !first
|
|
|
|
first = false
|
|
|
|
if obj.respond_to? :to_ary
|
|
|
|
r << '('
|
|
|
|
inner_inspect(obj, r)
|
|
|
|
r << ')'
|
|
|
|
else
|
|
|
|
r << obj.inspect
|
|
|
|
end
|
|
|
|
}
|
|
|
|
r
|
|
|
|
end
|
|
|
|
# :startdoc:
|
|
|
|
|
|
|
|
# returns new sentence object which
|
|
|
|
# _target_ is substituted by the block.
|
|
|
|
#
|
|
|
|
# Sentence#subst invokes <tt>_target_ === _string_</tt> for each
|
|
|
|
# string in the sentence.
|
|
|
|
# The strings which === returns true are substituted by the block.
|
|
|
|
# The block is invoked with the substituting string.
|
|
|
|
#
|
|
|
|
# Sentence.new(%w[2 + 3]).subst("+") { "*" }
|
|
|
|
# #<Sentence: "2" "*" "3">
|
|
|
|
#
|
|
|
|
# Sentence.new(%w[2 + 3]).subst(/\A\d+\z/) {|s| ((s.to_i)*2).to_s }
|
|
|
|
# #=> #<Sentence: "4" "+" "6">
|
|
|
|
#
|
2007-08-24 15:01:46 -04:00
|
|
|
def subst(target, &b) # :yield: string
|
2007-08-16 06:21:16 -04:00
|
|
|
Sentence.new(subst_rec(@sent, target, &b))
|
|
|
|
end
|
|
|
|
|
2007-08-18 13:04:06 -04:00
|
|
|
# :stopdoc:
|
2007-08-16 06:21:16 -04:00
|
|
|
def subst_rec(obj, target, &b)
|
|
|
|
if obj.respond_to? :to_ary
|
|
|
|
a = []
|
|
|
|
obj.each {|e| a << subst_rec(e, target, &b) }
|
|
|
|
a
|
|
|
|
elsif target === obj
|
|
|
|
yield obj
|
|
|
|
else
|
|
|
|
obj
|
|
|
|
end
|
|
|
|
end
|
2007-08-18 13:04:06 -04:00
|
|
|
# :startdoc:
|
2007-08-16 06:21:16 -04:00
|
|
|
|
2007-08-18 13:04:06 -04:00
|
|
|
# find a subsentence and return it.
|
|
|
|
# The block is invoked for each subsentence in preorder manner.
|
|
|
|
# The first subsentence which the block returns true is returned.
|
|
|
|
#
|
2007-08-18 23:37:14 -04:00
|
|
|
# Sentence(%w[2 * 7], "+", %w[3 * 5]).find_subtree {|s| s[1] == "*" }
|
2007-08-18 13:04:06 -04:00
|
|
|
# #=> #<Sentence: "2" "*" "7">
|
|
|
|
#
|
2007-08-24 15:01:46 -04:00
|
|
|
def find_subtree(&b) # :yield: sentence
|
2007-08-18 13:04:06 -04:00
|
|
|
find_subtree_rec(@sent, &b)
|
2007-08-16 06:21:16 -04:00
|
|
|
end
|
|
|
|
|
2007-08-18 13:04:06 -04:00
|
|
|
# :stopdoc:
|
2007-08-16 06:21:16 -04:00
|
|
|
def find_subtree_rec(obj, &b)
|
|
|
|
if obj.respond_to? :to_ary
|
2007-08-18 13:04:06 -04:00
|
|
|
s = Sentence.new(obj)
|
|
|
|
if b.call s
|
|
|
|
return s
|
2007-08-16 06:21:16 -04:00
|
|
|
else
|
|
|
|
obj.each {|e|
|
|
|
|
r = find_subtree_rec(e, &b)
|
|
|
|
return r if r
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
2007-08-18 13:04:06 -04:00
|
|
|
# :startdoc:
|
2007-08-16 06:21:16 -04:00
|
|
|
|
2007-08-18 13:04:06 -04:00
|
|
|
# returns a new sentence object which expands according to the condition
|
|
|
|
# given by the block.
|
|
|
|
#
|
|
|
|
# The block is invoked for each subsentence.
|
|
|
|
# The subsentences which the block returns true are
|
|
|
|
# expanded into parent.
|
|
|
|
#
|
2007-08-18 23:37:14 -04:00
|
|
|
# s = Sentence(%w[2 * 7], "+", %w[3 * 5])
|
2007-08-18 13:04:06 -04:00
|
|
|
# #=> #<Sentence: ("2" "*" "7") "+" ("3" "*" "5")>
|
|
|
|
#
|
|
|
|
# s.expand { true }
|
|
|
|
# #=> #<Sentence: "2" "*" "7" "+" "3" "*" "5">
|
|
|
|
#
|
|
|
|
# s.expand {|s| s[0] == "3" }
|
|
|
|
# #=> #<Sentence: (("2" "*" "7") "+" "3" "*" "5")>
|
|
|
|
#
|
2007-08-24 15:01:46 -04:00
|
|
|
def expand(&b) # :yield: sentence
|
2007-08-16 06:21:16 -04:00
|
|
|
Sentence.new(expand_rec(@sent, &b))
|
|
|
|
end
|
|
|
|
|
2007-08-18 13:04:06 -04:00
|
|
|
# :stopdoc:
|
2007-08-16 06:21:16 -04:00
|
|
|
def expand_rec(obj, r=[], &b)
|
|
|
|
if obj.respond_to? :to_ary
|
2007-08-24 15:01:46 -04:00
|
|
|
obj.each {|o|
|
|
|
|
s = Sentence.new(o)
|
|
|
|
if b.call s
|
2007-08-16 06:21:16 -04:00
|
|
|
expand_rec(o, r, &b)
|
2007-08-24 15:01:46 -04:00
|
|
|
else
|
|
|
|
a = []
|
2007-08-16 06:21:16 -04:00
|
|
|
expand_rec(o, a, &b)
|
2007-08-24 15:01:46 -04:00
|
|
|
r << a
|
|
|
|
end
|
|
|
|
}
|
2007-08-16 06:21:16 -04:00
|
|
|
else
|
|
|
|
r << obj
|
|
|
|
end
|
|
|
|
r
|
|
|
|
end
|
2007-08-18 13:04:06 -04:00
|
|
|
# :startdoc:
|
2007-08-16 06:21:16 -04:00
|
|
|
|
2007-08-18 13:04:06 -04:00
|
|
|
# Sentence.each generates sentences
|
|
|
|
# by deriving the start symbol _sym_ using _syntax_.
|
|
|
|
# The derivation is restricted by an positive integer _limit_ to
|
|
|
|
# avoid infinite generation.
|
|
|
|
#
|
|
|
|
# Sentence.each yields the block with a generated sentence.
|
|
|
|
#
|
|
|
|
# Sentence.each({
|
|
|
|
# :exp => [["n"],
|
|
|
|
# [:exp, "+", :exp],
|
|
|
|
# [:exp, "*", :exp]]
|
|
|
|
# }, :exp, 1) {|sent| p sent }
|
|
|
|
# #=>
|
|
|
|
# #<Sentence: "n">
|
|
|
|
# #<Sentence: ("n") "+" ("n")>
|
|
|
|
# #<Sentence: ("n") "*" ("n")>
|
2007-08-19 11:43:04 -04:00
|
|
|
#
|
2007-08-18 13:04:06 -04:00
|
|
|
# Sentence.each({
|
|
|
|
# :exp => [["n"],
|
|
|
|
# [:exp, "+", :exp],
|
|
|
|
# [:exp, "*", :exp]]
|
|
|
|
# }, :exp, 2) {|sent| p sent }
|
|
|
|
# #=>
|
|
|
|
# #<Sentence: "n">
|
|
|
|
# #<Sentence: ("n") "+" ("n")>
|
|
|
|
# #<Sentence: ("n") "+" (("n") "+" ("n"))>
|
|
|
|
# #<Sentence: ("n") "+" (("n") "*" ("n"))>
|
|
|
|
# #<Sentence: (("n") "+" ("n")) "+" ("n")>
|
|
|
|
# #<Sentence: (("n") "*" ("n")) "+" ("n")>
|
|
|
|
# #<Sentence: ("n") "*" ("n")>
|
|
|
|
# #<Sentence: ("n") "*" (("n") "+" ("n"))>
|
|
|
|
# #<Sentence: ("n") "*" (("n") "*" ("n"))>
|
|
|
|
# #<Sentence: (("n") "+" ("n")) "*" ("n")>
|
|
|
|
# #<Sentence: (("n") "*" ("n")) "*" ("n")>
|
|
|
|
#
|
2007-08-16 06:21:16 -04:00
|
|
|
def Sentence.each(syntax, sym, limit)
|
|
|
|
Gen.new(syntax).each_tree(sym, limit) {|tree|
|
|
|
|
yield Sentence.new(tree)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2007-08-18 13:04:06 -04:00
|
|
|
# Sentence.expand_syntax returns an expanded syntax:
|
2007-08-19 11:43:04 -04:00
|
|
|
# * No rule derives to empty sequence
|
|
|
|
# * Underivable rule simplified
|
|
|
|
# * No channel rule
|
2019-12-19 19:19:39 -05:00
|
|
|
# * Symbols which has zero or one choices are not appeared in rhs.
|
2007-08-18 13:04:06 -04:00
|
|
|
#
|
|
|
|
# Note that the rules which can derive empty and non-empty
|
|
|
|
# sequences are modified to derive only non-empty sequences.
|
|
|
|
#
|
|
|
|
# Sentence.expand_syntax({
|
2007-08-19 11:43:04 -04:00
|
|
|
# :underivable1 => [],
|
|
|
|
# :underivable2 => [[:underivable1]],
|
|
|
|
# :underivable3 => [[:underivable3]],
|
|
|
|
# :empty_only1 => [[]],
|
|
|
|
# :empty_only2 => [[:just_empty1, :just_empty1]],
|
2007-08-18 13:04:06 -04:00
|
|
|
# :empty_or_not => [[], ["foo"]],
|
|
|
|
# :empty_or_not_2 => [[:empty_or_not, :empty_or_not]],
|
2007-08-19 11:43:04 -04:00
|
|
|
# :empty_or_not_3 => [[:empty_or_not, :empty_or_not, :empty_or_not]],
|
|
|
|
# :empty_or_not_4 => [[:empty_or_not_2, :empty_or_not_2]],
|
|
|
|
# :channel1 => [[:channeled_data]],
|
|
|
|
# :channeled_data => [["a", "b"], ["c", "d"]],
|
|
|
|
# :single_choice => [["single", "choice"]],
|
|
|
|
# :single_choice_2 => [[:single_choice, :single_choice]],
|
|
|
|
# })
|
2007-08-18 13:04:06 -04:00
|
|
|
# #=>
|
2007-08-19 11:43:04 -04:00
|
|
|
# {
|
|
|
|
# :underivable1=>[], # underivable rules are simplified to [].
|
|
|
|
# :underivable2=>[],
|
|
|
|
# :underivable3=>[],
|
|
|
|
# :empty_only1=>[], # derivation to empty sequence are removed.
|
|
|
|
# :empty_only2=>[],
|
|
|
|
# :empty_or_not=>[["foo"]], # empty sequences are removed too.
|
|
|
|
# :empty_or_not_2=>[["foo"], ["foo", "foo"]],
|
|
|
|
# :empty_or_not_3=>[["foo"], ["foo", "foo"], ["foo", "foo", "foo"]],
|
|
|
|
# :empty_or_not_4=> [["foo"], ["foo", "foo"], [:empty_or_not_2, :empty_or_not_2]],
|
|
|
|
# :channel1=>[["a", "b"], ["c", "d"]], # channel rules are removed.
|
|
|
|
# :channeled_data=>[["a", "b"], ["c", "d"]],
|
|
|
|
# :single_choice=>[["single", "choice"]], # single choice rules are expanded.
|
|
|
|
# :single_choice_2=>[["single", "choice", "single", "choice"]],
|
|
|
|
# }
|
2007-08-18 23:37:14 -04:00
|
|
|
#
|
2007-08-19 11:43:04 -04:00
|
|
|
# Sentence.expand_syntax({
|
2007-08-18 23:37:14 -04:00
|
|
|
# :factor => [["n"],
|
|
|
|
# ["(", :exp, ")"]],
|
|
|
|
# :term => [[:factor],
|
|
|
|
# [:term, "*", :factor],
|
|
|
|
# [:term, "/", :factor]],
|
|
|
|
# :exp => [[:term],
|
|
|
|
# [:exp, "+", :term],
|
|
|
|
# [:exp, "-", :term]]
|
|
|
|
# })
|
|
|
|
# #=>
|
|
|
|
# {:exp=> [["n"],
|
|
|
|
# ["(", :exp, ")"],
|
|
|
|
# [:exp, "+", :term],
|
|
|
|
# [:exp, "-", :term],
|
|
|
|
# [:term, "*", :factor],
|
|
|
|
# [:term, "/", :factor]],
|
|
|
|
# :factor=> [["n"],
|
|
|
|
# ["(", :exp, ")"]],
|
|
|
|
# :term=> [["n"],
|
|
|
|
# ["(", :exp, ")"],
|
|
|
|
# [:term, "*", :factor],
|
|
|
|
# [:term, "/", :factor]]
|
|
|
|
# }
|
|
|
|
#
|
2007-08-18 13:04:06 -04:00
|
|
|
def Sentence.expand_syntax(syntax)
|
|
|
|
Sentence::Gen.expand_syntax(syntax)
|
|
|
|
end
|
|
|
|
|
|
|
|
# :stopdoc:
|
2007-08-16 06:21:16 -04:00
|
|
|
class Gen
|
|
|
|
def Gen.each_tree(syntax, sym, limit, &b)
|
|
|
|
Gen.new(syntax).each_tree(sym, limit, &b)
|
|
|
|
end
|
|
|
|
|
|
|
|
def Gen.each_string(syntax, sym, limit, &b)
|
|
|
|
Gen.new(syntax).each_string(sym, limit, &b)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(syntax)
|
|
|
|
@syntax = syntax
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.expand_syntax(syntax)
|
2007-08-19 11:43:04 -04:00
|
|
|
syntax = simplify_underivable_rules(syntax)
|
|
|
|
syntax = simplify_emptyonly_rules(syntax)
|
|
|
|
syntax = make_rules_no_empseq(syntax)
|
2007-08-16 06:21:16 -04:00
|
|
|
syntax = expand_channel_rules(syntax)
|
|
|
|
|
|
|
|
syntax = expand_noalt_rules(syntax)
|
|
|
|
syntax = reorder_rules(syntax)
|
|
|
|
end
|
|
|
|
|
2007-08-19 11:43:04 -04:00
|
|
|
def self.simplify_underivable_rules(syntax)
|
2007-08-16 06:21:16 -04:00
|
|
|
deribable_syms = {}
|
|
|
|
changed = true
|
|
|
|
while changed
|
|
|
|
changed = false
|
|
|
|
syntax.each {|sym, rules|
|
|
|
|
next if deribable_syms[sym]
|
|
|
|
rules.each {|rhs|
|
|
|
|
if rhs.all? {|e| String === e || deribable_syms[e] }
|
|
|
|
deribable_syms[sym] = true
|
|
|
|
changed = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
result = {}
|
|
|
|
syntax.each {|sym, rules|
|
2007-08-19 11:43:04 -04:00
|
|
|
if deribable_syms[sym]
|
|
|
|
rules2 = []
|
|
|
|
rules.each {|rhs|
|
|
|
|
rules2 << rhs if rhs.all? {|e| String === e || deribable_syms[e] }
|
|
|
|
}
|
|
|
|
result[sym] = rules2.uniq
|
|
|
|
else
|
|
|
|
result[sym] = []
|
|
|
|
end
|
2007-08-16 06:21:16 -04:00
|
|
|
}
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2007-08-19 11:43:04 -04:00
|
|
|
def self.simplify_emptyonly_rules(syntax)
|
2007-08-16 06:21:16 -04:00
|
|
|
justempty_syms = {}
|
|
|
|
changed = true
|
|
|
|
while changed
|
|
|
|
changed = false
|
|
|
|
syntax.each {|sym, rules|
|
|
|
|
next if justempty_syms[sym]
|
2007-08-19 11:43:04 -04:00
|
|
|
if !rules.empty? && rules.all? {|rhs| rhs.all? {|e| justempty_syms[e] } }
|
2007-08-16 06:21:16 -04:00
|
|
|
justempty_syms[sym] = true
|
|
|
|
changed = true
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
result = {}
|
|
|
|
syntax.each {|sym, rules|
|
|
|
|
result[sym] = rules.map {|rhs| rhs.reject {|e| justempty_syms[e] } }.uniq
|
|
|
|
}
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.expand_emptyable_syms(rhs, emptyable_syms)
|
|
|
|
if rhs.empty?
|
2007-08-24 15:01:46 -04:00
|
|
|
yield []
|
2007-08-16 06:21:16 -04:00
|
|
|
else
|
2007-08-24 15:01:46 -04:00
|
|
|
first = rhs[0]
|
|
|
|
rest = rhs[1..-1]
|
2007-08-19 11:43:04 -04:00
|
|
|
if emptyable_syms[first]
|
|
|
|
expand_emptyable_syms(rest, emptyable_syms) {|rhs2|
|
|
|
|
yield [first] + rhs2
|
2007-08-16 06:21:16 -04:00
|
|
|
yield rhs2
|
|
|
|
}
|
|
|
|
else
|
2007-08-19 11:43:04 -04:00
|
|
|
expand_emptyable_syms(rest, emptyable_syms) {|rhs2|
|
|
|
|
yield [first] + rhs2
|
2007-08-16 06:21:16 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-08-19 11:43:04 -04:00
|
|
|
def self.make_rules_no_empseq(syntax)
|
2007-08-16 06:21:16 -04:00
|
|
|
emptyable_syms = {}
|
|
|
|
changed = true
|
|
|
|
while changed
|
|
|
|
changed = false
|
|
|
|
syntax.each {|sym, rules|
|
|
|
|
next if emptyable_syms[sym]
|
|
|
|
rules.each {|rhs|
|
|
|
|
if rhs.all? {|e| emptyable_syms[e] }
|
|
|
|
emptyable_syms[sym] = true
|
|
|
|
changed = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
result = {}
|
|
|
|
syntax.each {|sym, rules|
|
|
|
|
rules2 = []
|
|
|
|
rules.each {|rhs|
|
|
|
|
expand_emptyable_syms(rhs, emptyable_syms) {|rhs2|
|
2007-08-19 11:43:04 -04:00
|
|
|
next if rhs2.empty?
|
2007-08-16 06:21:16 -04:00
|
|
|
rules2 << rhs2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result[sym] = rules2.uniq
|
|
|
|
}
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.expand_channel_rules(syntax)
|
|
|
|
channel_rules = {}
|
|
|
|
syntax.each {|sym, rules|
|
|
|
|
channel_rules[sym] = {sym=>true}
|
|
|
|
rules.each {|rhs|
|
|
|
|
if rhs.length == 1 && Symbol === rhs[0]
|
|
|
|
channel_rules[sym][rhs[0]] = true
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
changed = true
|
|
|
|
while changed
|
2007-08-19 11:43:04 -04:00
|
|
|
changed = false
|
2007-08-16 06:21:16 -04:00
|
|
|
channel_rules.each {|sym, set|
|
|
|
|
n1 = set.size
|
|
|
|
set.keys.each {|s|
|
|
|
|
set.update(channel_rules[s])
|
|
|
|
}
|
|
|
|
n2 = set.size
|
|
|
|
changed = true if n1 < n2
|
|
|
|
}
|
|
|
|
end
|
|
|
|
result = {}
|
|
|
|
syntax.each {|sym, rules|
|
|
|
|
rules2 = []
|
|
|
|
channel_rules[sym].each_key {|s|
|
|
|
|
syntax[s].each {|rhs|
|
|
|
|
unless rhs.length == 1 && Symbol === rhs[0]
|
|
|
|
rules2 << rhs
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result[sym] = rules2.uniq
|
|
|
|
}
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.expand_noalt_rules(syntax)
|
|
|
|
noalt_syms = {}
|
|
|
|
syntax.each {|sym, rules|
|
|
|
|
if rules.length == 1
|
|
|
|
noalt_syms[sym] = true
|
|
|
|
end
|
|
|
|
}
|
|
|
|
result = {}
|
|
|
|
syntax.each {|sym, rules|
|
|
|
|
rules2 = []
|
|
|
|
rules.each {|rhs|
|
|
|
|
rhs2 = []
|
|
|
|
rhs.each {|e|
|
|
|
|
if noalt_syms[e]
|
|
|
|
rhs2.concat syntax[e][0]
|
|
|
|
else
|
|
|
|
rhs2 << e
|
|
|
|
end
|
|
|
|
}
|
|
|
|
rules2 << rhs2
|
|
|
|
}
|
|
|
|
result[sym] = rules2.uniq
|
|
|
|
}
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reorder_rules(syntax)
|
|
|
|
result = {}
|
|
|
|
syntax.each {|sym, rules|
|
|
|
|
result[sym] = rules.sort_by {|rhs|
|
|
|
|
[rhs.find_all {|e| Symbol === e }.length, rhs.length]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_tree(sym, limit)
|
|
|
|
generate_from_sym(sym, limit) {|_, tree|
|
|
|
|
yield tree
|
|
|
|
}
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def each_string(sym, limit)
|
|
|
|
generate_from_sym(sym, limit) {|_, tree|
|
|
|
|
yield [tree].join('')
|
|
|
|
}
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_from_sym(sym, limit, &b)
|
|
|
|
return if limit < 0
|
|
|
|
if String === sym
|
|
|
|
yield limit, sym
|
|
|
|
else
|
|
|
|
rules = @syntax[sym]
|
|
|
|
raise "undefined rule: #{sym}" if !rules
|
|
|
|
rules.each {|rhs|
|
|
|
|
if rhs.length == 1 || rules.length == 1
|
|
|
|
limit1 = limit
|
|
|
|
else
|
|
|
|
limit1 = limit-1
|
|
|
|
end
|
|
|
|
generate_from_rhs(rhs, limit1, &b)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_from_rhs(rhs, limit)
|
|
|
|
return if limit < 0
|
|
|
|
if rhs.empty?
|
|
|
|
yield limit, []
|
|
|
|
else
|
|
|
|
generate_from_sym(rhs[0], limit) {|limit1, child|
|
|
|
|
generate_from_rhs(rhs[1..-1], limit1) {|limit2, arr|
|
|
|
|
yield limit2, [child, *arr]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2007-08-18 13:04:06 -04:00
|
|
|
# :startdoc:
|
2007-08-16 06:21:16 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|