rley 0.1.09 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/CHANGELOG.md +4 -0
- data/examples/parsers/parsing_abc.rb +72 -0
- data/examples/recognizers/recognizer_abc.rb +71 -0
- data/lib/rley.rb +4 -0
- data/lib/rley/constants.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YzgzNjZlYTc1NjRjYTkxZTNkNmVkNmE3MmY2OGRjNzAwYjBjM2E0MQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MmMzNWNiZjRlZDcyOTJiZmE0Nzc4Y2ZjYjBhYmYyM2ZkNTE5MzM5OQ==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YTRiZTJhZjRkOTcyYzQ3NTE4ZDFmZGQ1NTI4ZGM3MTdhOGVlN2Q2NGFlNWE1
|
10
|
+
NmVlMmE2NGUzYjJhOTE0MTJjNmY0ZmI3MDMzYTA3N2VjYzVlNGQ4ODNhNDc2
|
11
|
+
MzdiYzdkMzhlZWVmYTJhNWUxMjg4ZWM2OTQ3Zjg3ZDcyNDY1MjE=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YmE3MmE2OGM5MzBhMDY4NDgyNTA5MmY0NjA3ZjcyNDQ3Yzg3NDJiNTJjNzc0
|
14
|
+
NjczZDFjN2QyNTIwODNmYmE5ZGQwMzBjMTYwMWQwNDJkN2UwYWQ5MTc4MWJh
|
15
|
+
OWU5OTdlNmFkNWYyNTA2NzU3NWI0MjUxZTM1NjRmMzE4MGNhOGE=
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
### 0.1.10 / 2014-12-14
|
2
|
+
* [CHANGE] Added more examples in `examples` folder (e.g. parsing then parse tree ).
|
3
|
+
* [CHANGE] File `rley.rb`: added more requires of the library to ease integration.
|
4
|
+
|
1
5
|
### 0.1.09 / 2014-12-14
|
2
6
|
* [CHANGE] Source code refactored to please Rubocop (0.28.0)
|
3
7
|
* [CHANGE] File `.rubucop.yml` Disabled VariableNam style cop.
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# Purpose: to demonstrate how to build and render a parse tree
|
2
|
+
|
3
|
+
require 'rley' # Load the gem
|
4
|
+
|
5
|
+
# Steps to render a parse tree (of a valid parsed input):
|
6
|
+
# 1. Define a grammar
|
7
|
+
# 2. Create a tokenizer for the language
|
8
|
+
# 3. Create a parser for that grammar
|
9
|
+
# 4. Tokenize the input
|
10
|
+
# 5. Let the parser process the input
|
11
|
+
# 6. Generate a parse tree from the parse result
|
12
|
+
# 7. Render the parse tree (in JSON)
|
13
|
+
|
14
|
+
########################################
|
15
|
+
# Step 1. Define a grammar for a very simple language
|
16
|
+
# It recognizes/generates strings like 'b', 'abc', 'aabcc', 'aaabccc',...
|
17
|
+
# (based on example in N. Wirth's book "Compiler Construction", p. 6)
|
18
|
+
# Let's create the grammar step-by-step with the grammar builder:
|
19
|
+
builder = Rley::Syntax::GrammarBuilder.new
|
20
|
+
builder.add_terminals('a', 'b', 'c')
|
21
|
+
builder.add_production('S' => 'A')
|
22
|
+
builder.add_production('A' => %w(a A c))
|
23
|
+
builder.add_production('A' => 'b')
|
24
|
+
|
25
|
+
# And now build the grammar...
|
26
|
+
grammar_abc = builder.grammar
|
27
|
+
|
28
|
+
|
29
|
+
########################################
|
30
|
+
# 2. Create a tokenizer for the language
|
31
|
+
# The tokenizer transforms the input into an array of tokens
|
32
|
+
def tokenizer(aText, aGrammar)
|
33
|
+
tokens = aText.chars.map do |ch|
|
34
|
+
terminal = aGrammar.name2symbol[ch]
|
35
|
+
fail StandardError, "Unknown input character '#{ch}'" if terminal.nil?
|
36
|
+
Rley::Parser::Token.new(ch, terminal)
|
37
|
+
end
|
38
|
+
|
39
|
+
return tokens
|
40
|
+
end
|
41
|
+
|
42
|
+
########################################
|
43
|
+
# Step 3. Create a parser for that grammar
|
44
|
+
parser = Rley::Parser::EarleyParser.new(grammar_abc)
|
45
|
+
|
46
|
+
########################################
|
47
|
+
# Step 3. Tokenize the input
|
48
|
+
valid_input = 'aabcc'
|
49
|
+
tokens = tokenizer(valid_input, grammar_abc)
|
50
|
+
|
51
|
+
########################################
|
52
|
+
# Step 5. Let the parser process the input
|
53
|
+
result = parser.parse(tokens)
|
54
|
+
|
55
|
+
|
56
|
+
########################################
|
57
|
+
# Step 6. Generate a parse tree from the parse result
|
58
|
+
ptree = result.parse_tree
|
59
|
+
|
60
|
+
########################################
|
61
|
+
# Step 7. Render the parse tree (in JSON)
|
62
|
+
# Let's create a parse tree visitor
|
63
|
+
visitor = Rley::ParseTreeVisitor.new(ptree)
|
64
|
+
|
65
|
+
#Here we create a renderer object...
|
66
|
+
renderer = Rley::Formatter::Json.new(STDOUT)
|
67
|
+
|
68
|
+
# Now emit the parse tree as JSON on the console output
|
69
|
+
puts "JSON rendering of the parse tree for '#{valid_input}' input:"
|
70
|
+
renderer.render(visitor)
|
71
|
+
|
72
|
+
# End of file
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# Purpose: to demonstrate how to build a recognizer
|
2
|
+
# A recognizer is a kind of parser that indicates whether the input
|
3
|
+
# complies to the grammar or not.
|
4
|
+
|
5
|
+
require 'rley' # Load the gem
|
6
|
+
|
7
|
+
# Steps to build a recognizer:
|
8
|
+
# 1. Define a grammar
|
9
|
+
# 2. Create a parser for that grammar
|
10
|
+
# 3. Build the input
|
11
|
+
# 4. Let the parser process the input
|
12
|
+
# 5. Check the parser's result to see whether the input was valid (=recognized)
|
13
|
+
|
14
|
+
########################################
|
15
|
+
# Step 1. Define a grammar for a very simple language
|
16
|
+
# It recognizes/generates strings like 'b', 'abc', 'aabcc', 'aaabccc',...
|
17
|
+
# (based on example in N. Wirth's book "Compiler Construction", p. 6)
|
18
|
+
# Let's create the grammar step-by-step with the grammar builder:
|
19
|
+
builder = Rley::Syntax::GrammarBuilder.new
|
20
|
+
builder.add_terminals('a', 'b', 'c')
|
21
|
+
builder.add_production('S' => 'A')
|
22
|
+
builder.add_production('A' => %w(a A c))
|
23
|
+
builder.add_production('A' => 'b')
|
24
|
+
|
25
|
+
# And now build the grammar...
|
26
|
+
grammar_abc = builder.grammar
|
27
|
+
|
28
|
+
# Keep track of the terminal symbols of the grammar:
|
29
|
+
term_a = grammar_abc.name2symbol['a']
|
30
|
+
term_b = grammar_abc.name2symbol['b']
|
31
|
+
term_c = grammar_abc.name2symbol['c']
|
32
|
+
|
33
|
+
########################################
|
34
|
+
# Step 2. Create a parser for that grammar
|
35
|
+
parser = Rley::Parser::EarleyParser.new(grammar_abc)
|
36
|
+
|
37
|
+
########################################
|
38
|
+
# Step 3. Build the input
|
39
|
+
# Mimicking the output of a tokenizer
|
40
|
+
valid_input = [
|
41
|
+
Rley::Parser::Token.new('a', term_a),
|
42
|
+
Rley::Parser::Token.new('a', term_a),
|
43
|
+
Rley::Parser::Token.new('b', term_b),
|
44
|
+
Rley::Parser::Token.new('c', term_c),
|
45
|
+
Rley::Parser::Token.new('c', term_c)
|
46
|
+
]
|
47
|
+
|
48
|
+
########################################
|
49
|
+
# Step 4. Let the parser process the input
|
50
|
+
result = parser.parse(valid_input)
|
51
|
+
|
52
|
+
|
53
|
+
########################################
|
54
|
+
# Step 5. Check the parser's result to see whether the input was valid
|
55
|
+
puts "Successful parse of 'aabcc'? #{result.success?}"
|
56
|
+
# Output: Successful parse of 'aabcc'? true
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
# Let's redo steps 3, 4, 5 again with an invalid input.
|
61
|
+
invalid_input = [
|
62
|
+
Rley::Parser::Token.new('a', term_a),
|
63
|
+
Rley::Parser::Token.new('a', term_a),
|
64
|
+
Rley::Parser::Token.new('b', term_b),
|
65
|
+
Rley::Parser::Token.new('c', term_c)
|
66
|
+
]
|
67
|
+
result = parser.parse(invalid_input)
|
68
|
+
puts "Successful parse of 'aabc'? #{result.success?}"
|
69
|
+
# Output: Successful parse of 'aabc'? false
|
70
|
+
|
71
|
+
# End of file
|
data/lib/rley.rb
CHANGED
@@ -4,6 +4,10 @@
|
|
4
4
|
|
5
5
|
require_relative './rley/constants'
|
6
6
|
require_relative './rley/syntax/grammar_builder'
|
7
|
+
require_relative './rley/parser/token'
|
7
8
|
require_relative './rley/parser/earley_parser'
|
9
|
+
require_relative './rley/parse_tree_visitor'
|
10
|
+
require_relative './rley/formatter/debug'
|
11
|
+
require_relative './rley/formatter/json'
|
8
12
|
|
9
13
|
# End of file
|
data/lib/rley/constants.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rley
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dimitri Geshef
|
@@ -101,6 +101,8 @@ files:
|
|
101
101
|
- README.md
|
102
102
|
- examples/grammars/grammar_abc.rb
|
103
103
|
- examples/grammars/grammar_L0.rb
|
104
|
+
- examples/parsers/parsing_abc.rb
|
105
|
+
- examples/recognizers/recognizer_abc.rb
|
104
106
|
- lib/rley.rb
|
105
107
|
- lib/rley/constants.rb
|
106
108
|
- lib/rley/formatter/base_formatter.rb
|