Sha256: dc71edc661db2e2416afb90d89877d95e2e9b4b1ab91e9f0ef29e3de5be25143

Contents?: true

Size: 726 Bytes

Versions: 17

Compression:

Stored size: 726 Bytes

Contents

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

# A very simple language
# It recognizes/generates strings like 'b', 'abc', 'aabcc', 'aaabccc',...
# (based on example in N. Wirth's book "Compiler Construction", p. 6)
# S ::= A.
# A ::= "a" A "c".
# A ::= "b".


# Let's create the grammar step-by-step with the grammar builder:
builder = Rley::Syntax::GrammarBuilder.new
builder.add_terminals('a', 'b', 'c')
builder.add_production('S' => 'A')
builder.add_production('A' => %w(a A c))
builder.add_production('A' => 'b')

# And now build the grammar...
grammar_abc = builder.grammar

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

# End of file

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
rley-0.1.08 examples/grammars/grammar_abc.rb
rley-0.1.07 examples/grammars/grammar_abc.rb
rley-0.1.06 examples/grammars/grammar_abc.rb
rley-0.1.05 examples/grammars/grammar_abc.rb
rley-0.1.04 examples/grammars/grammar_abc.rb
rley-0.1.03 examples/grammars/grammar_abc.rb
rley-0.1.02 examples/grammars/grammar_abc.rb
rley-0.1.01 examples/grammars/grammar_abc.rb
rley-0.1.00 examples/grammars/grammar_abc.rb
rley-0.0.18 examples/grammars/grammar_abc.rb
rley-0.0.17 examples/grammars/grammar_abc.rb
rley-0.0.16 examples/grammars/grammar_abc.rb
rley-0.0.15 examples/grammars/grammar_abc.rb
rley-0.0.14 examples/grammars/grammar_abc.rb
rley-0.0.13 examples/grammars/grammar_abc.rb
rley-0.0.12 examples/grammars/grammar_abc.rb
rley-0.0.11 examples/grammars/grammar_abc.rb