ebnf 1.2.0 → 2.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +223 -199
- data/UNLICENSE +1 -1
- data/VERSION +1 -1
- data/bin/ebnf +38 -19
- data/etc/abnf-core.ebnf +52 -0
- data/etc/abnf.abnf +121 -0
- data/etc/abnf.ebnf +124 -0
- data/etc/abnf.sxp +45 -0
- data/etc/doap.ttl +23 -18
- data/etc/ebnf.ebnf +21 -33
- data/etc/ebnf.html +76 -160
- data/etc/{ebnf.rb → ebnf.ll1.rb} +30 -107
- data/etc/ebnf.ll1.sxp +182 -183
- data/etc/ebnf.peg.rb +90 -0
- data/etc/ebnf.peg.sxp +84 -0
- data/etc/ebnf.sxp +40 -41
- data/etc/iso-ebnf.ebnf +140 -0
- data/etc/iso-ebnf.isoebnf +138 -0
- data/etc/iso-ebnf.sxp +65 -0
- data/etc/sparql.ebnf +4 -4
- data/etc/sparql.html +1603 -1751
- data/etc/sparql.ll1.sxp +7372 -7372
- data/etc/sparql.peg.rb +532 -0
- data/etc/sparql.peg.sxp +597 -0
- data/etc/sparql.sxp +363 -362
- data/etc/turtle.ebnf +3 -3
- data/etc/turtle.html +465 -517
- data/etc/{turtle.rb → turtle.ll1.rb} +3 -4
- data/etc/turtle.ll1.sxp +425 -425
- data/etc/turtle.peg.rb +182 -0
- data/etc/turtle.peg.sxp +199 -0
- data/etc/turtle.sxp +103 -101
- data/lib/ebnf.rb +6 -1
- data/lib/ebnf/abnf.rb +301 -0
- data/lib/ebnf/abnf/core.rb +23 -0
- data/lib/ebnf/abnf/meta.rb +111 -0
- data/lib/ebnf/base.rb +114 -69
- data/lib/ebnf/bnf.rb +1 -26
- data/lib/ebnf/ebnf/meta.rb +90 -0
- data/lib/ebnf/isoebnf.rb +229 -0
- data/lib/ebnf/isoebnf/meta.rb +75 -0
- data/lib/ebnf/ll1.rb +131 -3
- data/lib/ebnf/ll1/lexer.rb +20 -22
- data/lib/ebnf/ll1/parser.rb +97 -64
- data/lib/ebnf/ll1/scanner.rb +82 -50
- data/lib/ebnf/native.rb +320 -0
- data/lib/ebnf/parser.rb +285 -302
- data/lib/ebnf/peg.rb +39 -0
- data/lib/ebnf/peg/parser.rb +561 -0
- data/lib/ebnf/peg/rule.rb +250 -0
- data/lib/ebnf/rule.rb +442 -148
- data/lib/ebnf/terminals.rb +21 -0
- data/lib/ebnf/writer.rb +587 -82
- metadata +125 -18
- data/etc/sparql.rb +0 -45773
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d3b1d692ce9936bb68c8c89722a06b90b7cfd6c42231ff57d49780a98214a325
|
4
|
+
data.tar.gz: da6430584d824a1d070d364879c17b0b3b0bd8c9b32b44af841cd039e153e7f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e0bacbdef9b82ecca13fca9babcf8fe643acb3afaad077b05aebfc237f542859a1e6bea67e6b2e776c2e81b51dd7900cd24e39d9712582de6959d5e65ecfa42
|
7
|
+
data.tar.gz: 3001f1864c2cf8fade2856fb7d3383f4b1ac8519d2914e54cc55b8a461f6f208d41c3fa35539367bad104a74068cc868220255ce933f301894530eacb0abf51e
|
data/README.md
CHANGED
@@ -2,15 +2,25 @@
|
|
2
2
|
|
3
3
|
[EBNF][] parser and generic parser generator.
|
4
4
|
|
5
|
-
[![Gem Version](https://badge.fury.io/rb/ebnf.png)](
|
6
|
-
[![Build Status](https://
|
7
|
-
[![Coverage Status](https://coveralls.io/repos/dryruby/ebnf/badge.svg)](https://coveralls.io/r/dryruby/ebnf)
|
8
|
-
[![
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/ebnf.png)](https://badge.fury.io/rb/ebnf)
|
6
|
+
[![Build Status](https://github.com/dryruby/ebnf/workflows/CI/badge.svg?branch=develop)](https://github.com/dryruby/ebnf/actions?query=workflow%3ACI)
|
7
|
+
[![Coverage Status](https://coveralls.io/repos/dryruby/ebnf/badge.svg?branch=develop)](https://coveralls.io/r/dryruby/ebnf?branch=develop)
|
8
|
+
[![Gitter chat](https://badges.gitter.im/ruby-rdf/rdf.png)](https://gitter.im/ruby-rdf/rdf)
|
9
9
|
|
10
10
|
## Description
|
11
|
-
This is a [Ruby][] implementation of an [EBNF][] and [BNF][] parser and parser generator.
|
11
|
+
This is a [Ruby][] implementation of an [EBNF][] and [BNF][] parser and parser generator.
|
12
12
|
|
13
|
-
|
13
|
+
### [PEG][]/[Packrat][] Parser
|
14
|
+
In the primary mode, it supports a Parsing Expression Grammar ([PEG][]) parser generator. This performs more minmal transformations on the parsed grammar to extract sub-productions, which allows each component of a rule to generate its own parsing event.
|
15
|
+
|
16
|
+
The resulting {EBNF::PEG::Rule} objects then parse each associated rule according to the operator semantics and use a [Packrat][] memoizer to reduce extra work when backtracking.
|
17
|
+
|
18
|
+
These rules are driven using the {EBNF::PEG::Parser} module which calls invokes the starting rule and ensures that all input is consumed.
|
19
|
+
|
20
|
+
### LL(1) Parser
|
21
|
+
In another mode, it parses [EBNF][] grammars to [BNF][], generates [First/Follow][] and Branch tables for [LL(1)][] grammars, which can be used with the stream [Tokenizer][] and [LL(1) Parser][].
|
22
|
+
|
23
|
+
As LL(1) grammars operate using `alt` and `seq` primitives, allowing for a match on alternative productions or a sequence of productions, generating a parser requires turning the [EBNF][] rules into [BNF][]:
|
14
24
|
|
15
25
|
* Transform `a ::= b?` into `a ::= _empty | b`
|
16
26
|
* Transform `a ::= b+` into `a ::= b b*`
|
@@ -23,213 +33,221 @@ As LL(1) grammars operate using `alt` and `seq` primitives, allowing for a match
|
|
23
33
|
|
24
34
|
Of note in this implementation is that the tokenizer and parser are streaming, so that they can process inputs of arbitrary size.
|
25
35
|
|
36
|
+
The _exception operator_ (`A - B`) is only supported on terminals.
|
37
|
+
|
38
|
+
See {EBNF::LL1} and {EBNF::LL1::Parser} for further information.
|
39
|
+
|
26
40
|
## Usage
|
27
|
-
### Parsing an
|
41
|
+
### Parsing an EBNF Grammar
|
28
42
|
|
29
43
|
require 'ebnf'
|
30
44
|
|
31
|
-
|
45
|
+
grammar = EBNF.parse(File.open('./etc/ebnf.ebnf'))
|
32
46
|
|
33
|
-
Output rules and terminals as S-Expressions, Turtle, HTML or BNF
|
47
|
+
Output rules and terminals as [S-Expressions][S-Expression], [Turtle][], HTML or [BNF][]
|
34
48
|
|
35
|
-
puts
|
36
|
-
puts
|
37
|
-
puts
|
38
|
-
puts
|
49
|
+
puts grammar.to_sxp
|
50
|
+
puts grammar.to_ttl
|
51
|
+
puts grammar.to_html
|
52
|
+
puts grammar.to_s
|
39
53
|
|
40
|
-
Transform EBNF to
|
54
|
+
Transform [EBNF][] to [PEG][] (generates sub-rules for embedded expressions) and the RULES table as Ruby for parsing grammars:
|
41
55
|
|
42
|
-
|
56
|
+
grammar.make_peg
|
57
|
+
grammar.to_ruby
|
43
58
|
|
44
|
-
|
59
|
+
Transform [EBNF][] to [BNF][] (generates sub-rules using `alt` or `seq` from `plus`, `star` or `opt`)
|
45
60
|
|
46
|
-
|
61
|
+
grammar.make_bnf
|
47
62
|
|
48
|
-
Generate
|
63
|
+
Generate [First/Follow][] rules for BNF grammars (using "ebnf" as the starting production):
|
49
64
|
|
50
|
-
ebnf
|
51
|
-
ebnf.to_ruby
|
65
|
+
grammar.first_follow(:ebnf)
|
52
66
|
|
53
|
-
Generate
|
67
|
+
Generate Terminal, [First/Follow][], Cleanup and Branch tables as Ruby for parsing grammars:
|
54
68
|
|
55
|
-
|
69
|
+
grammar.build_tables
|
70
|
+
grammar.to_ruby
|
56
71
|
|
57
|
-
|
58
|
-
Intermediate representations of the grammar may be serialized to Lisp-like S-Expressions. For example, the rule `[1] ebnf ::= (declaration | rule)*` is serialized as `(rule ebnf "1" (star (alt declaration rule)))`.
|
59
|
-
|
60
|
-
Once the [LL(1)][] conversion is made, the [First/Follow][] table is generated, this rule expands as follows:
|
61
|
-
|
62
|
-
(rule ebnf "1"
|
63
|
-
(start #t)
|
64
|
-
(first "@pass" "@terminals" LHS _eps)
|
65
|
-
(follow _eof)
|
66
|
-
(cleanup star)
|
67
|
-
(alt _empty _ebnf_2))
|
68
|
-
(rule _ebnf_1 "1.1"
|
69
|
-
(first "@pass" "@terminals" LHS)
|
70
|
-
(follow "@pass" "@terminals" LHS _eof)
|
71
|
-
(alt declaration rule))
|
72
|
-
(rule _ebnf_2 "1.2"
|
73
|
-
(first "@pass" "@terminals" LHS)
|
74
|
-
(follow _eof)
|
75
|
-
(cleanup merge)
|
76
|
-
(seq _ebnf_1 ebnf))
|
77
|
-
(rule _ebnf_3 "1.3" (first "@pass" "@terminals" LHS _eps) (follow _eof) (seq ebnf))
|
78
|
-
|
79
|
-
### Creating terminal definitions and parser rules to parse generated grammars
|
80
|
-
The parser is initialized to callbacks invoked on entry and exit
|
81
|
-
to each `terminal` and `production`. A trivial parser loop can be described as follows:
|
82
|
-
|
83
|
-
require 'ebnf/ll1/parser'
|
84
|
-
require 'meta'
|
85
|
-
|
86
|
-
class Parser
|
87
|
-
include Meta
|
88
|
-
|
89
|
-
terminal(:SYMBOL, /([a-z]|[A-Z]|[0-9]|_)+/) do |prod, token, input|
|
90
|
-
# Add data based on scanned token to input
|
91
|
-
input[:symbol] = token.value
|
92
|
-
end
|
93
|
-
|
94
|
-
start_production(:rule) do |input, current, callback|
|
95
|
-
# Process on start of production
|
96
|
-
# Set state for entry into recursed rules through current
|
97
|
-
|
98
|
-
# Callback to parser loop with callback
|
99
|
-
end
|
100
|
-
|
101
|
-
production(:rule) do |input, current, callback|
|
102
|
-
# Process on end of production
|
103
|
-
# return results in input, retrieve results from recursed rules in current
|
104
|
-
|
105
|
-
# Callback to parser loop with callback
|
106
|
-
end
|
107
|
-
|
108
|
-
def initialize(input)
|
109
|
-
parser_options = {
|
110
|
-
branch: BRANCH,
|
111
|
-
first: FIRST,
|
112
|
-
follow: FOLLOW,
|
113
|
-
cleanup: CLEANUP
|
114
|
-
}
|
115
|
-
parse(input, start_symbol, parser_options) do |context, *data|
|
116
|
-
# Process calls from callback from productions
|
117
|
-
|
118
|
-
rescue ArgumentError, RDF::LL1::Parser::Error => e
|
119
|
-
progress("Parsing completed with errors:\n\t#{e.message}")
|
120
|
-
raise RDF::ReaderError, e.message if validate?
|
121
|
-
end
|
122
|
-
|
123
|
-
### Branch Table
|
124
|
-
The Branch table is a hash mapping production rules to a hash relating terminals appearing in input to sequence of productions to follow when the corresponding input terminal is found. This allows either the `seq` primitive, where all terminals map to the same sequence of productions, or the `alt` primitive, where each terminal may map to a different production.
|
125
|
-
|
126
|
-
BRANCH = {
|
127
|
-
:alt => {
|
128
|
-
"(" => [:seq, :_alt_1],
|
129
|
-
:ENUM => [:seq, :_alt_1],
|
130
|
-
:HEX => [:seq, :_alt_1],
|
131
|
-
:O_ENUM => [:seq, :_alt_1],
|
132
|
-
:O_RANGE => [:seq, :_alt_1],
|
133
|
-
:RANGE => [:seq, :_alt_1],
|
134
|
-
:STRING1 => [:seq, :_alt_1],
|
135
|
-
:STRING2 => [:seq, :_alt_1],
|
136
|
-
:SYMBOL => [:seq, :_alt_1],
|
137
|
-
},
|
138
|
-
...
|
139
|
-
:declaration => {
|
140
|
-
"@pass" => [:pass],
|
141
|
-
"@terminals" => ["@terminals"],
|
142
|
-
},
|
143
|
-
...
|
144
|
-
}
|
145
|
-
|
146
|
-
In this case the `alt` rule is `seq ('|' seq)*` can happen when any of the specified tokens appears on the input stream. The all cause the same token to be passed to the `seq` rule and follow with `_alt_1`, which handles the `('|' seq)*` portion of the rule, after the first sequence is matched.
|
147
|
-
|
148
|
-
The `declaration` rule is `@terminals' | pass` using the `alt` primitive determining the production to run based on the terminal appearing on the input stream. Eventually, a terminal production is found and the token is consumed.
|
149
|
-
|
150
|
-
### First/Follow Table
|
151
|
-
The [First/Follow][] table is a hash mapping production rules to the terminals that may proceed or follow the rule. For example:
|
152
|
-
|
153
|
-
FIRST = {
|
154
|
-
:alt => [
|
155
|
-
:HEX,
|
156
|
-
:SYMBOL,
|
157
|
-
:ENUM,
|
158
|
-
:O_ENUM,
|
159
|
-
:RANGE,
|
160
|
-
:O_RANGE,
|
161
|
-
:STRING1,
|
162
|
-
:STRING2,
|
163
|
-
"("],
|
164
|
-
...
|
165
|
-
}
|
166
|
-
|
167
|
-
### Terminals Table
|
168
|
-
This table is a simple list of the terminal productions found in the grammar. For example:
|
169
|
-
|
170
|
-
TERMINALS = ["(", ")", "-",
|
171
|
-
"@pass", "@terminals",
|
172
|
-
:ENUM, :HEX, :LHS, :O_ENUM, :O_RANGE,:POSTFIX,
|
173
|
-
:RANGE, :STRING1, :STRING2, :SYMBOL,"|"
|
174
|
-
].freeze
|
175
|
-
|
176
|
-
### Cleanup Table
|
177
|
-
This table identifies productions which used EBNF rules, which are transformed to BNF for actual parsing. This allows the parser, in some cases, to reproduce *star*, *plus*, and *opt* rule matches. For example:
|
178
|
-
|
179
|
-
CLEANUP = {
|
180
|
-
:_alt_1 => :star,
|
181
|
-
:_alt_3 => :merge,
|
182
|
-
:_diff_1 => :opt,
|
183
|
-
:ebnf => :star,
|
184
|
-
:_ebnf_2 => :merge,
|
185
|
-
:_postfix_1 => :opt,
|
186
|
-
:seq => :plus,
|
187
|
-
:_seq_1 => :star,
|
188
|
-
:_seq_2 => :merge,
|
189
|
-
}.freeze
|
190
|
-
|
191
|
-
In this case the `ebnf` rule was `(declaration | rule)*`. As BNF does not support a star operator, this is decomposed into a set of rules using `alt` and `seq` primitives:
|
192
|
-
|
193
|
-
ebnf ::= _empty _ebnf_2
|
194
|
-
_ebnf_1 ::= declaration | rule
|
195
|
-
_ebnf_2 ::= _ebnf_1 ebnf
|
196
|
-
_ebnf_3 ::= ebnf
|
197
|
-
|
198
|
-
The `_empty` production matches an empty string, so allows for now value. `_ebnf_2` matches `declaration | rule` (using the `alt` primitive) followed by `ebnf`, creating a sequence of zero or more `declaration` or `alt` members.
|
72
|
+
Generate formatted grammar using HTML (requires [Haml][Haml] gem):
|
199
73
|
|
200
|
-
|
201
|
-
|
202
|
-
|
74
|
+
grammar.to_html
|
75
|
+
|
76
|
+
### Parsing an ISO/IEC 14977 Grammar
|
77
|
+
|
78
|
+
The EBNF gem can also parse [ISO/EIC 14977] Grammars (ISOEBNF) to [S-Expressions][S-Expression].
|
79
|
+
|
80
|
+
grammar = EBNF.parse(File.open('./etc/iso-ebnf.isoebnf', format: :isoebnf))
|
81
|
+
|
82
|
+
### Parsing an ABNF Grammar
|
83
|
+
|
84
|
+
The EBNF gem can also parse [ABNF] Grammars to [S-Expressions][S-Expression].
|
85
|
+
|
86
|
+
grammar = EBNF.parse(File.open('./etc/abnf.abnf', format: :abnf))
|
87
|
+
|
88
|
+
### Parser Debugging
|
89
|
+
|
90
|
+
Inevitably while implementing a parser for some specific grammar, a developer will need greater insight into the operation of the parser. While this can involve sorting through a tremendous amount of data, the parser can be provided a [Logger][] instance which will output messages at varying levels of detail to document the state of the parser at any given point. Most useful is likely the `INFO` level of debugging, but even more detail is revealed using the `DEBUG` level. `WARN` and `ERROR` statements will typically also be provided as part of an exception if parsing fails, but can be shown in the context of other parsing state with appropriate indentation as part of the logger.
|
91
|
+
|
92
|
+
### Writing Grammars
|
93
|
+
|
94
|
+
The {EBNF::Writer} class can be used to write parsed grammars out, either as formatted text, or HTML. Because grammars are written from the Abstract Syntax Tree, represented as [S-Expressions][S-Expression], this provides a means of transforming between grammar formats (e.g., W3C [EBNF][] to [ABNF][]), although with some potential loss in semantic fidelity (case-insensitive string matching vs. case-sensitive matching).
|
95
|
+
|
96
|
+
The formatted HTML results are designed to be appropriate for including in specifications. If the [Nokogumbo](https://rubygems.org/gems/nokogumbo) gem list available, the resulting HTML encoded grammar will also be validated.
|
97
|
+
|
98
|
+
### Parser Errors
|
99
|
+
On a parsing failure, and exception is raised with information that may be useful in determining the source of the error.
|
203
100
|
|
204
|
-
|
101
|
+
## EBNF Grammar
|
102
|
+
The [EBNF][] variant used here is based on [W3C](https://w3.org/) [EBNF][] (see {file:etc/ebnf.ebnf EBNF grammar}) as defined in the
|
103
|
+
[XML 1.0 recommendation](https://www.w3.org/TR/REC-xml/), with minor extensions:
|
104
|
+
|
105
|
+
Note that the grammar includes an optional `[identifer]` in front of rule names, which can be in conflict with the `RANGE` terminal. It is typically not a problem, but if it comes up, try parsing with the `native` parser, add comments or sequences to disambiguate. EBNF does not have beginning of line checks as all whitespace is treated the same, so the common practice of identifying each rule inherently leads to such ambiguity.
|
106
|
+
|
107
|
+
The character set for EBNF is UTF-8.
|
108
|
+
|
109
|
+
The general form of a rule is:
|
110
|
+
|
111
|
+
symbol ::= expression
|
112
|
+
|
113
|
+
which can also be proceeded by an optional number enclosed in square brackets to identify the rule number:
|
114
|
+
|
115
|
+
[1] symbol ::= expression
|
116
|
+
|
117
|
+
(Note, this can introduce an ambiguity if the previous rule ends in a range or enum and the current rule has no identifier. In this case, enclosing `expression` within parentheses, or adding intervening comments can resolve the ambiguity.)
|
118
|
+
|
119
|
+
Symbols are written in CAPITAL CASE if they are the start symbol of a regular language (terminals), otherwise with they are treated as non-terminal rules. Literal strings are quoted.
|
120
|
+
|
121
|
+
Within the expression on the right-hand side of a rule, the following expressions are used to match strings of one or more characters:
|
122
|
+
|
123
|
+
<table>
|
124
|
+
<tr><td><code>#xN</code></td>
|
125
|
+
<td>where <code>N</code> is a hexadecimal integer, the expression matches the character whose number (code point) in ISO/IEC 10646 is <code>N</code>. The number of leading zeros in the <code>#xN</code> form is insignificant.</td></tr>
|
126
|
+
<tr><td><code>[a-zA-Z], [#xN-#xN]</code>
|
127
|
+
<td>matches any Char or HEX with a value in the range(s) indicated (inclusive).</td></tr>
|
128
|
+
<tr><td><code>[abc], [#xN#xN#xN]</code></td>
|
129
|
+
<td>matches any UTF-8 R\_CHAR or HEX with a value among the characters enumerated. The last component may be '-'. Enumerations and ranges may be mixed in one set of brackets.</td></tr>
|
130
|
+
<tr><td><code>[^a-z], [^#xN-#xN]</code></td>
|
131
|
+
<td>matches any UTF-8 Char or HEX a value outside the range indicated.</td></tr>
|
132
|
+
<tr><td><code>[^abc], [^#xN#xN#xN]</code></td>
|
133
|
+
<td>matches any UTF-8 R\_CHAR or HEX with a value not among the characters given. The last component may be '-'. Enumerations and ranges of excluded values may be mixed in one set of brackets.</td></tr>
|
134
|
+
<tr><td><code>"string"</code></td>
|
135
|
+
<td>matches a literal string matching that given inside the double quotes.</td></tr>
|
136
|
+
<tr><td><code>'string'</code></td>
|
137
|
+
<td>matches a literal string matching that given inside the single quotes.</td></tr>
|
138
|
+
<tr><td><code>A (B | C)</code></td>
|
139
|
+
<td><code>(B | C)</code> is treated as a unit and may be combined as described in this list.</td></tr>
|
140
|
+
<tr><td><code>A?</code></td>
|
141
|
+
<td>matches A or nothing; optional A.</td></tr>
|
142
|
+
<tr><td><code>A B</code></td>
|
143
|
+
<td>matches <code>A</code> followed by <code>B</code>. This operator has higher precedence than alternation; thus <code>A B | C D</code> is identical to <code>(A B) | (C D)</code>.</td></tr>
|
144
|
+
<tr><td><code>A | B</code></td>
|
145
|
+
<td>matches <code>A</code> or <code>B</code>.</td></tr>
|
146
|
+
<tr><td><code>A - B</code></td>
|
147
|
+
<td>matches any string that matches <code>A</code> but does not match <code>B</code>. (Only supported on Terminals in LL(1) BNF).</td></tr>
|
148
|
+
<tr><td><code>A+</code></td>
|
149
|
+
<td>matches one or more occurrences of <code>A</code>. Concatenation has higher precedence than alternation; thus <code>A+ | B+</code> is identical to <code>(A+) | (B+)</code>.</td></tr>
|
150
|
+
<tr><td><code>A*</code></td>
|
151
|
+
<td>matches zero or more occurrences of <code>A</code>. Concatenation has higher precedence than alternation; thus <code>A* | B*</code> is identical to <code>(A*) | (B*)</code>.</td></tr>
|
152
|
+
<tr><td><code>@pass " "*</code></td>
|
153
|
+
<td>Defines consumed whitespace in the document. Any whitespace found between non-terminal rules is consumed and ignored.</td></tr>
|
154
|
+
<tr><td><code>@terminals</code></td>
|
155
|
+
<td>Introduces terminal rules. All rules defined after this point are treated as terminals.</td></tr>
|
156
|
+
</table>
|
157
|
+
|
158
|
+
* Comments include `//` and `#` through end of line (other than hex character) and `/* ... */ (* ... *) which may cross lines`
|
205
159
|
* All rules **MAY** start with an identifier, contained within square brackets. For example `[1] rule`, where the value within the brackets is a symbol `([a-z] | [A-Z] | [0-9] | "_" | ".")+`
|
206
|
-
* `@terminals` causes following rules to be treated as terminals. Any terminal which
|
160
|
+
* `@terminals` causes following rules to be treated as terminals. Any terminal which is all upper-case (eg`TERMINAL`), or any rules with expressions that match characters (`#xN`, `[a-z]`, `[^a-z]`, `[abc]`, `[^abc]`, `"string"`, `'string'`, or `A - B`), are also treated as terminals.
|
207
161
|
* `@pass` defines the expression used to detect whitespace, which is removed in processing.
|
208
162
|
* No support for `wfc` (well-formedness constraint) or `vc` (validity constraint).
|
209
163
|
|
210
|
-
Parsing this grammar yields an S-Expression version: {file:etc/ebnf.ll1.sxp}.
|
164
|
+
Parsing this grammar yields an [S-Expression][] version: {file:etc/ebnf.sxp} (or [LL(1)][] version {file:etc/ebnf.ll1.sxp} or [PEG][] version {file:etc/ebnf.peg.sxp}).
|
165
|
+
|
166
|
+
### Parser S-Expressions
|
167
|
+
Intermediate representations of the grammar may be serialized to Lisp-like [S-Expressions][S-Expression]. For example, the rule
|
168
|
+
|
169
|
+
[1] ebnf ::= (declaration | rule)*
|
170
|
+
|
171
|
+
is serialized as
|
172
|
+
|
173
|
+
(rule ebnf "1" (star (alt declaration rule)))
|
174
|
+
|
175
|
+
Different components of an EBNF rule expression are transformed into their own operator:
|
176
|
+
|
177
|
+
<table>
|
178
|
+
<tr><td><code>#xN</code></td><td><code>(hex "#xN")</code></td></tr>
|
179
|
+
<tr><td><code>[a-z#xN-#xN]</code></td><td><code>(range "a-z#xN-#xN")</code></td></tr>
|
180
|
+
<tr><td><code>[abc#xN]</code></td><td><code>(range "abc#xN")</code></td></tr>
|
181
|
+
<tr><td><code>[^a-z#xN-#xN]</code></td><td><code>(range "^a-z#xN-#xN")</code></td></tr>
|
182
|
+
<tr><td><code>[^abc#xN]</code></td><td><code>(range "^abc#xN")</code></td></tr>
|
183
|
+
<tr><td><code>"string"</code></td><td><code>"string"</code></td></tr>
|
184
|
+
<tr><td><code>'string'</code></td><td><code>"string"</code></td></tr>
|
185
|
+
<tr><td><code>A (B | C)</code></td><td><code>(seq (A (alt B C)))</code></td></tr>
|
186
|
+
<tr><td><code>A?</code></td><td><code>(opt A)</code></td></tr>
|
187
|
+
<tr><td><code>A B</code></td><td><code>(seq A B)</code></td></tr>
|
188
|
+
<tr><td><code>A | B</code></td><td><code>(alt A B)</code></td></tr>
|
189
|
+
<tr><td><code>A - B</code></td>
|
190
|
+
<td><code>(diff A B) for terminals.<br/>
|
191
|
+
<code>(seq (not B) A) for non-terminals (PEG parsing only)</code></code></td></tr>
|
192
|
+
<tr><td><code>A+</code></td><td><code>(plus A)</code></td></tr>
|
193
|
+
<tr><td><code>A*</code></td><td><code>(star A)</code></td></tr>
|
194
|
+
<tr><td><code>@pass " "*</code></td><td><code>(pass _pass (star " "))</code></td></tr>
|
195
|
+
<tr><td><code>@terminals</code></td><td></td></tr>
|
196
|
+
</table>
|
197
|
+
|
198
|
+
Other rule operators are not directly supported in [EBNF][], but are included to support other notations (e.g., [ABNF][] and [ISO/IEC 14977][]):
|
199
|
+
|
200
|
+
<table>
|
201
|
+
<tr><td><code>%i"StRiNg"</code></td><td><code>(istr "StRiNg")</code></td><td>Case-insensitive string matching</td></tr>
|
202
|
+
<tr><td><code>'' - A</code></td><td><code>(not A)</code></td><td>Negative look-ahead, used for non-terminal uses of `B - A`.</td></tr>
|
203
|
+
<tr><td><code>n*mA</code></td><td><code>(rept n m A)</code></td><td>Explicit repetition.</td></tr>
|
204
|
+
</table>
|
205
|
+
|
206
|
+
Additionally, rules defined with an UPPERCASE symbol are treated as terminals.
|
207
|
+
|
208
|
+
For an [LL(1)][] parser generator, the {EBNF::BNF.make_bnf} method can be used to transform the EBNF rule into a BNF rule.
|
209
|
+
|
210
|
+
(rule ebnf "1" (alt _empty _ebnf_2))
|
211
|
+
(rule _ebnf_1 "1.1" (alt declaration rule))
|
212
|
+
(rule _ebnf_2 "1.2" (seq _ebnf_1 ebnf))
|
213
|
+
(rule _ebnf_3 "1.3" (seq ebnf))
|
214
|
+
|
215
|
+
This allows [First/Follow][] and other tables used by a parser to parse examples of the associated grammar. For more, see {EBNF::LL1}.
|
216
|
+
|
217
|
+
For a [PEG][] parser generator, there is a simpler transformation that reduces rules containing sub-expressions (composed of `star`, `alt`, `seq` and similar expressions) and creates named rules to allow appropriate callbacks and for naming elements of the generating abstract syntax tree. The {EBNF::PEG.make_peg} method transforms the original rule into the following two rules:
|
218
|
+
|
219
|
+
(rule ebnf "1" (star _ebnf_1))
|
220
|
+
(rule _ebnf_1 "1.1" (alt declaration rule))
|
221
|
+
|
222
|
+
## Example parsers
|
223
|
+
For a [PEG][] parser for a simple grammar implementing a calculator see [Calc example](https://dryruby.github.io/ebnf/examples/calc/doc/calc.html)
|
224
|
+
|
225
|
+
For an example parser built using this gem that parses the [EBNF][] grammar, see [EBNF PEG Parser example](https://dryruby.github.io/ebnf/examples/ebnf-peg-parser/doc/parser.html). This example creates a parser for the [EBNF][] grammar which generates the same Abstract Syntax Tree as the built-in parser in the gem.
|
226
|
+
|
227
|
+
There is also an
|
228
|
+
[EBNF LL(1) Parser example](https://dryruby.github.io/ebnf/examples/ebnf-peg-parser/doc/parser.html).
|
229
|
+
|
230
|
+
The [ISO EBNF Parser](https://dryruby.github.io/ebnf/examples/isoebnf/doc/parser.html) example parses [ISO/IEC 14977][] into [S-Expressions][S-Expression], which can be used to parse compatible grammars using this parser (either [PEG][] or [LL(1)][]).
|
211
231
|
|
212
|
-
|
213
|
-
For an example parser built using this gem, see {file:examples/ebnf-parser/README EBNF Parser example}. This example creates a parser for the [EBNF][] grammar which generates the same Abstract Syntax Tree as the built-in parser in the gem.
|
232
|
+
The [ABNF Parser](https://dryruby.github.io/ebnf/examples/abnf/doc/parser.html) example parses [ABNF][] into [S-Expressions][S-Expression], which can be used to parse compatible grammars using this [PEG][] parser.
|
214
233
|
|
215
234
|
## Acknowledgements
|
216
235
|
Much of this work, particularly the generic parser, is inspired by work originally done by
|
217
|
-
Tim Berners-Lee's Python [predictive parser](
|
236
|
+
Tim Berners-Lee's Python [predictive parser](https://www.w3.org/2000/10/swap/grammar/predictiveParser.py).
|
218
237
|
|
219
|
-
The
|
220
|
-
[EBNF to Turtle processor](
|
221
|
-
[EBNF to BNF Notation-3 rules](
|
222
|
-
and [First Follow Notation-3 rules](
|
238
|
+
The [LL(1)][] parser was inspired by Dan Connolly's
|
239
|
+
[EBNF to Turtle processor](https://www.w3.org/2000/10/swap/grammar/ebnf2turtle.py),
|
240
|
+
[EBNF to BNF Notation-3 rules](https://www.w3.org/2000/10/swap/grammar/ebnf2bnf.n3),
|
241
|
+
and [First Follow Notation-3 rules](https://www.w3.org/2000/10/swap/grammar/first_follow.n3).
|
223
242
|
|
224
243
|
## Documentation
|
225
244
|
Full documentation available on [Rubydoc.info][EBNF doc].
|
226
245
|
|
227
246
|
## Future Work
|
228
247
|
* Better LL(1) parser tests
|
229
|
-
* Either generate [Packrat parser][Packrat] for a [Parsing Regular Expression Grammar][PEG], or integrate with [Treetop][] or similar.
|
230
248
|
|
231
249
|
## Author
|
232
|
-
* [Gregg Kellogg](
|
250
|
+
* [Gregg Kellogg](https://github.com/gkellogg) - <https://greggkellogg.net/>
|
233
251
|
|
234
252
|
## Contributing
|
235
253
|
This repository uses [Git Flow](https://github.com/nvie/gitflow) to mange development and release activity. All submissions _must_ be on a feature branch based on the _develop_ branch to ease staging and integration.
|
@@ -244,26 +262,32 @@ This repository uses [Git Flow](https://github.com/nvie/gitflow) to mange develo
|
|
244
262
|
list in the the `README`. Alphabetical order applies.
|
245
263
|
* Do note that in order for us to merge any non-trivial changes (as a rule
|
246
264
|
of thumb, additions larger than about 15 lines of code), we need an
|
247
|
-
explicit [public domain dedication][PDD] on record from you
|
265
|
+
explicit [public domain dedication][PDD] on record from you,
|
266
|
+
which you will be asked to agree to on the first commit to a repo within the organization.
|
248
267
|
|
249
268
|
## License
|
250
269
|
This is free and unencumbered public domain software. For more information,
|
251
|
-
see <
|
252
|
-
|
253
|
-
A copy of the [Turtle EBNF][] and derived parser files are included in the repository, which are not covered under the UNLICENSE. These files are covered via the [W3C Document License](
|
254
|
-
|
255
|
-
[Ruby]:
|
256
|
-
[YARD]:
|
257
|
-
[YARD-GS]:
|
258
|
-
[PDD]:
|
259
|
-
[
|
260
|
-
[
|
261
|
-
[
|
262
|
-
[
|
263
|
-
[
|
264
|
-
[
|
265
|
-
[
|
266
|
-
[
|
267
|
-
[
|
268
|
-
[
|
269
|
-
[
|
270
|
+
see <https://unlicense.org/> or the accompanying {file:UNLICENSE} file.
|
271
|
+
|
272
|
+
A copy of the [Turtle EBNF][] and derived parser files are included in the repository, which are not covered under the UNLICENSE. These files are covered via the [W3C Document License](https://www.w3.org/Consortium/Legal/2002/copyright-documents-20021231).
|
273
|
+
|
274
|
+
[Ruby]: https://ruby-lang.org/
|
275
|
+
[YARD]: https://yardoc.org/
|
276
|
+
[YARD-GS]: https://rubydoc.info/docs/yard/file/docs/GettingStarted.md
|
277
|
+
[PDD]: https://lists.w3.org/Archives/Public/public-rdf-ruby/2010May/0013.html
|
278
|
+
[ABNF]: https://www.rfc-editor.org/rfc/rfc5234
|
279
|
+
[BNF]: https://en.wikipedia.org/wiki/Backus–Naur_form
|
280
|
+
[EBNF]: https://www.w3.org/TR/REC-xml/#sec-notation
|
281
|
+
[EBNF doc]: https://rubydoc.info/github/dryruby/ebnf
|
282
|
+
[First/Follow]: https://en.wikipedia.org/wiki/LL_parser#Constructing_an_LL.281.29_parsing_table
|
283
|
+
[ISO/IEC 14977]:https://www.iso.org/standard/26153.html
|
284
|
+
[LL(1)]: https://www.csd.uwo.ca/~moreno//CS447/Lectures/Syntax.html/node14.html
|
285
|
+
[LL(1) Parser]: https://en.wikipedia.org/wiki/LL_parser
|
286
|
+
[Logger]: https://ruby-doc.org/stdlib-2.4.0/libdoc/logger/rdoc/Logger.html
|
287
|
+
[S-expression]: https://en.wikipedia.org/wiki/S-expression
|
288
|
+
[Tokenizer]: https://en.wikipedia.org/wiki/Lexical_analysis#Tokenizer
|
289
|
+
[Turtle]: https://www.w3.org/TR/2012/WD-turtle-20120710/
|
290
|
+
[Turtle EBNF]: https://dvcs.w3.org/hg/rdf/file/default/rdf-turtle/turtle.bnf
|
291
|
+
[Packrat]: https://pdos.csail.mit.edu/~baford/packrat/thesis/
|
292
|
+
[PEG]: https://en.wikipedia.org/wiki/Parsing_expression_grammar
|
293
|
+
[Haml]: https://rubygems.org/gems/haml
|