This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Criado Free Context Grammar e Chomsky Normal Form
- Loading branch information
Showing
5 changed files
with
120 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|