Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
Criado Free Context Grammar e Chomsky Normal Form
Browse files Browse the repository at this point in the history
  • Loading branch information
jmonteiro committed Jun 23, 2008
1 parent 4986286 commit 5e7dc1d
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 27 deletions.
17 changes: 17 additions & 0 deletions lib/chomsky_normal_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'free_context_grammar'

class ChomskyNormalForm < FreeContextGrammar

def initialize(v, t, p, s)
super(v, t, p, s)
end

def valid?
@productions.values.each do |p|
p.each do |vp|
vp.scan
end
end
end
end

21 changes: 21 additions & 0 deletions lib/free_context_grammar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'chomsky_normal_form'

class FreeContextGrammar
attr_accessor :vars, :terms, :productions, :start

def initialize(v, t, p, s)
@vars = v # ['E']
@terms = t # ['+', '*', '[', ']', 'x']
@productions = p # { 'E' => ["E+E", "E*E", "[E]", "x"] }
@start = s # 'E'
end

def start_productions
@productions[@start]
end

def to_cnf

end
end

56 changes: 56 additions & 0 deletions test/unit/chomsky_normal_form_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'test/unit'
require 'rubygems'
require 'shoulda'
require File.dirname(__FILE__) + '/../../lib/free_context_grammar'
require File.dirname(__FILE__) + '/../../lib/chomsky_normal_form'

class ChomskyNormalFormTest < Test::Unit::TestCase

context 'Invalid CNF' do
should 'be invalid' do
cnf = ChomskyNormalForm.new(
['E'],
['+', '*', '[', ']', 'x'],
{ 'E' => ["E+E", "E*E", "[E]", "x"] },
'E'
)
assert !cnf.valid?
end
end

context 'Valid CNF' do
should 'be valid' do
cnf = ChomskyNormalForm.new(
['A', 'B'],
['a', 'b', 'c'],
{ 'A' => ['AB', 'B'], 'B' => ['a'] },
'A'
)
assert cnf.valid?
end
end
=begin
should "resolv example 10 from the book" do
fcg = FreeContextGrammar.new(
['E'],
['+', '*', '[', ']', 'x'],
{ 'E' => ["E+E", "E*E", "[E]", "x"] },
'E'
)
fnc_converted = fcg.to_fnc
fnc_expected = ChomskyNormalForm.new(
['E', 'C+', 'C*', 'C[', 'C]', 'D1', 'D2', 'D3']
['+', '*', '[', ']', 'x'],
{
'E' => ['ED1', 'ED2', 'C[D3', 'x'],
'D1' => ['C+E'], 'D2' => ['C*E'], 'D3' => ['EC]'],
'C+' => ['+'], 'C*' => ['*'], 'C[' => ['['], 'C]' => [']']
},
'E'
)
assert_equal fnc_expected, fnc_converted
end
=end
end
27 changes: 0 additions & 27 deletions test/unit/chomsky_test.rb

This file was deleted.

26 changes: 26 additions & 0 deletions test/unit/free_context_grammar_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'test/unit'
require 'rubygems'
require 'shoulda'
require File.dirname(__FILE__) + '/../../lib/free_context_grammar'

class FreeContextGrammarTest < Test::Unit::TestCase
context "The first free context grammar example" do
setup do
@fcg = FreeContextGrammar.new(
['E'],
['+', '*', '[', ']', 'x'],
{ 'E' => ["E+E", "E*E", "[E]", "x"] },
'E'
)
end

should "accept free context grammar object" do
assert @fcg
end

should "get the start production" do
assert_equal @fcg.start_productions, ["E+E", "E*E", "[E]", "x"]
end
end
end

0 comments on commit 5e7dc1d

Please sign in to comment.