Sha256: e4b4bb21df4a4c239fc2e88b094533a9b8269b34b9633849f7a8cf314222a212

Contents?: true

Size: 1.37 KB

Versions: 25

Compression:

Stored size: 1.37 KB

Contents

# Purpose: to demonstrate how to build a very simple grammar
require 'rley'  # Load the gem

# Sample grammar for a very limited English language
# based on the language L0 from Jurafsky & Martin

# Let's create the grammar step-by-step with the grammar builder:
builder = Rley::Syntax::GrammarBuilder.new

# Enumerate the POS Part-Of-Speech...
builder.add_terminals('Noun', 'Verb', 'Adjective')
builder.add_terminals('Pronoun', 'Proper-Noun', 'Determiner')
builder.add_terminals('Preposition', 'Conjunction')

# Now the production rules...
builder.add_production('S'=> ['NP', 'VP']) # e.g. I + want a morning flight
builder.add_production('NP' => 'Pronoun')  # e.g. I
builder.add_production('NP' => 'Proper-Noun') # e.g. Los Angeles
builder.add_production('NP' => ['Determiner', 'Nominal'])  # e.g. a + flight
builder.add_production('Nominal' => %w(Nominal Noun)) # morning + flight
builder.add_production('Nominal' => 'Noun') # e.g. flights
builder.add_production('VP' => 'Verb')      # e.g. do
builder.add_production('VP' => ['Verb', 'NP'])  # e.g. want + a flight
builder.add_production('VP' => ['Verb', 'NP', 'PP'])
builder.add_production('VP' => ['Verb', 'PP']) # leaving + on Thursday
builder.add_production('PP' => ['Preposition', 'NP']) # from + Los Angeles

# And now we 're ready to build the grammar...
grammar_L0 = builder.grammar

# Prove that it is a grammar
puts grammar_L0.class.name

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
rley-0.3.08 examples/grammars/grammar_L0.rb
rley-0.3.07 examples/grammars/grammar_L0.rb
rley-0.3.06 examples/grammars/grammar_L0.rb
rley-0.3.05 examples/grammars/grammar_L0.rb
rley-0.3.04 examples/grammars/grammar_L0.rb
rley-0.3.01 examples/grammars/grammar_L0.rb
rley-0.3.00 examples/grammars/grammar_L0.rb
rley-0.2.15 examples/grammars/grammar_L0.rb
rley-0.2.14 examples/grammars/grammar_L0.rb
rley-0.2.12 examples/grammars/grammar_L0.rb
rley-0.2.11 examples/grammars/grammar_L0.rb
rley-0.2.10 examples/grammars/grammar_L0.rb
rley-0.2.09 examples/grammars/grammar_L0.rb
rley-0.2.08 examples/grammars/grammar_L0.rb
rley-0.2.06 examples/grammars/grammar_L0.rb
rley-0.2.05 examples/grammars/grammar_L0.rb
rley-0.2.04 examples/grammars/grammar_L0.rb
rley-0.2.03 examples/grammars/grammar_L0.rb
rley-0.2.02 examples/grammars/grammar_L0.rb
rley-0.2.01 examples/grammars/grammar_L0.rb