Matthew Bellantoni

Panini 1.1 Released

panini, ruby

I've just released a new version of Panini, a Ruby gem that generates sentences from a given grammar.  This release adds a new derivator that will exhaustively create every sentence in a grammar if called enough times.

Example

This code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  grammar = Panini::Grammar.new
  n_s = grammar.add_nonterminal
  
  n_a = grammar.add_nonterminal
  n_b = grammar.add_nonterminal
  
  n_s.add_production([@n_a, @n_a, @n_b])  # S -> AAB
  n_a.add_production(['a'])               # A -> 'a'
  n_a.add_production(['x'])               # A -> 'x'
  n_b.add_production(['b'])               # B -> 'b'
  
  derivator = Panini::DerivationStrategy::Exhaustive.new(grammar)
  begin
    s = derivator.sentence
    puts s.join(' ') unless s.nil?
  end while !s.nil?

Will create this output:

"a a b"
"a x b"
"x a b"
"x x b"

Enjoy!