-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathebnf.go
More file actions
626 lines (589 loc) · 15.7 KB
/
Copy pathebnf.go
File metadata and controls
626 lines (589 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
// Package ebnf parses, represents, and inspects a variant of [Extended Backus-Naur Form] called [Wirth Syntax Notation].
// The first production defines the start nonterminal identifier.
// Nonterminal identifiers must begin with an uppercase letter.
// Terminal identifiers must begin with a lowercase letter.
// Terminal identifiers are assumed to be defined elsewhere and not cause conflicts.
// Epsilon is represented by an empty literal.
//
// Grammars are written like so:
//
// Grammar = {Production}.
// Production = Identifier "=" Expression ".".
// Expression = Term {"|" Term}.
// Term = Factor {Factor}.
// Factor = Group | Identifier | Literal | Option | Repetition.
// Group = "(" Expression ")".
// Identifier = letter {letter}.
// Literal = "\"" {character} "\"".
// Option = "[" Expression "]".
// Repetition = "{" Expression "}".
//
// [Parse] parses a grammar into a [Grammar].
// [Grammar.Validate] determines whether a grammar is valid.
// [Grammar.First] computes the first terminals.
// [Grammar.FirstNonterminals] and [Grammar.Follow] compute the [first and follow terminals] for nonterminal identifiers.
// [Grammar.LL1] determines whether a valid grammar has parse conflicts for an LL(1) parser.
//
// [Extended Backus-Naur Form]: https://en.wikipedia.org/wiki/Extended_Backus–Naur_form
// [Wirth Syntax Notation]: https://en.wikipedia.org/wiki/Wirth_syntax_notation
// [first and follow terminals]: https://en.wikipedia.org/wiki/LL_parser#Conflicts
package ebnf
import (
"bytes"
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
// Identifier is the text of a terminal or nonterminal identifier.
// The text must contain only lowercase and uppercase English letters and must not be empty.
type Identifier struct {
Text string
}
// String returns the text.
// For example, for "foo", it returns "foo".
func (i Identifier) String() string {
return i.Text
}
// Literal is the text between the quotes of a literal.
// If the text is empty, it represents epsilon, the empty string.
type Literal struct {
Text string
}
// String returns the text surrounded by quotes.
// For example, for "foo", it returns `"foo"`.
func (l Literal) String() string {
return fmt.Sprintf("%q", l.Text)
}
// Factor is either a group, identifier, literal, option, or repetition.
// Group groups syntax.
// Option is optional syntax.
// Repetition is syntax that occurs zero or more times.
// Identifier and Literal are concrete syntax.
// All fields must be nil except one.
type Factor struct {
Group *Expression
Identifier *Identifier
Literal *Literal
Option *Expression
Repetition *Expression
}
// String returns a string representation of the non-nil field.
// For Group, its string is surrounded by round brackets like "(...)".
// For Option, its string is surrounded by square brackets like "[...]".
// For Repetition, its string is surrounded by curly brackets like "{...}".
// For Identifier and Literal, their string is returned unchanged.
func (f Factor) String() string {
switch {
case f.Group != nil:
return fmt.Sprintf("(%v)", f.Group)
case f.Identifier != nil:
return f.Identifier.String()
case f.Literal != nil:
return f.Literal.String()
case f.Option != nil:
return fmt.Sprintf("[%v]", f.Option)
case f.Repetition != nil:
return fmt.Sprintf("{%v}", f.Repetition)
default:
panic(f)
}
}
// Term is an expression alternative.
// It has a list of one or more factors.
// All factors must not be nil.
type Term struct {
Factors []*Factor
}
// String returns the factor strings separated by a space.
func (t Term) String() string {
ss := make([]string, len(t.Factors))
for i, f := range t.Factors {
ss[i] = fmt.Sprint(f)
}
return strings.Join(ss, " ")
}
// Expression is a production expression.
// It has a list of one or more terms.
// All terms must not be nil.
type Expression struct {
Terms []*Term
}
// String returns the term strings separated by " | ".
func (e Expression) String() string {
ss := make([]string, len(e.Terms))
for i, t := range e.Terms {
ss[i] = fmt.Sprint(t)
}
return strings.Join(ss, " | ")
}
// Production is a grammar production.
// The identifier and expression must not be nil.
type Production struct {
Identifier *Identifier
Expression *Expression
}
// String returns the identifier and expression strings separated by " = "
// and followed by a period.
func (p Production) String() string {
return fmt.Sprintf("%s = %v.", p.Identifier, p.Expression)
}
// Grammar is a grammar.
// It has a list of one or more productions.
// All productions must not be nil.
type Grammar struct {
Productions []*Production
}
// Error has all lexer and parser errors.
type Error struct {
Errors []error
}
// Error returns the error strings separated by a line feed.
func (e Error) Error() string {
ss := make([]string, len(e.Errors))
for i, err := range e.Errors {
ss[i] = err.Error()
}
return strings.Join(ss, "\n")
}
// Parse returns a Grammar for a valid grammar, or an error otherwise.
func Parse(s string) (*Grammar, error) {
p := newParser(bytes.NewBufferString(s))
g := p.parseGrammar()
var errs []error
if len(p.lexer.errs) > 0 {
errs = p.lexer.errs
}
if len(p.errs) > 0 {
errs = append(errs, p.errs...)
}
if len(errs) > 0 {
return nil, Error{Errors: errs}
}
return g, nil
}
func merge(from, to map[any]struct{}) {
for k, v := range from {
to[k] = v
}
}
func terminal(i *Identifier) bool {
r, _ := utf8.DecodeRuneInString(i.Text)
return unicode.IsLower(r)
}
func first(all map[any]map[any]struct{}, item any) map[any]struct{} {
var this map[any]struct{}
switch item := item.(type) {
case nil:
case *Expression:
if item == nil {
break
}
this = map[any]struct{}{}
for _, t := range item.Terms {
merge(first(all, t), this)
}
all[item] = this
case *Factor:
if item == nil {
break
}
this = map[any]struct{}{}
merge(first(all, item.Group), this)
merge(first(all, item.Identifier), this)
merge(first(all, item.Literal), this)
merge(first(all, item.Option), this)
merge(first(all, item.Repetition), this)
if item.Option != nil || item.Repetition != nil {
this[Literal{}] = struct{}{}
}
all[item] = this
case *Identifier:
if item == nil {
break
}
this = map[any]struct{}{*item: {}}
all[*item] = this
case *Literal:
if item == nil {
break
}
this = map[any]struct{}{*item: {}}
all[*item] = this
case *Production:
if item == nil {
break
}
this = first(all, item.Expression)
all[item] = this
case *Term:
if item == nil {
break
}
for _, f := range item.Factors {
first(all, f)
}
nonterminal := false
for t := range all[item.Factors[0]] {
if i, ok := t.(Identifier); ok && !terminal(&i) {
nonterminal = true
break
}
}
if nonterminal {
this = map[any]struct{}{item: {}}
} else {
this = firstFactors(all, item.Factors)
}
all[item] = this
}
return this
}
func firstFactors(all map[any]map[any]struct{}, fs []*Factor) map[any]struct{} {
this := all[fs[0]]
if len(fs) == 1 {
return this
}
if _, ok := this[Literal{}]; !ok {
return this
}
delete(this, Literal{})
merge(firstFactors(all, fs[1:]), this)
return this
}
// First returns the first terminals of a valid grammar.
// The map keys are [*Production], [*Expression], [*Term], [*Factor], [Identifier], or [Literal] values.
// The map values are sets of [Identifier] and [Literal] values.
func (g Grammar) First() map[any]map[any]struct{} {
all := map[any]map[any]struct{}{}
for _, p := range g.Productions {
this := first(all, p)
delete(this, *p.Identifier)
all[*p.Identifier] = this
}
for {
var merged bool
for _, terminals := range all {
for t := range terminals {
if i, ok := t.(Identifier); ok && !terminal(&i) {
merge(all[i], terminals)
delete(terminals, i)
merged = true
}
}
}
if !merged {
break
}
}
for _, terminals := range all {
for terminal := range terminals {
if term, ok := terminal.(*Term); ok {
delete(terminals, terminal)
merge(firstFactors(all, term.Factors), terminals)
}
}
}
return all
}
// FirstNonterminals returns the first terminals of the nonterminals of a valid grammar.
func (g Grammar) FirstNonterminals() map[string]map[any]struct{} {
first := map[string]map[any]struct{}{}
for k, v := range g.First() {
if p, ok := k.(*Production); ok {
first[p.Identifier.Text] = v
}
}
return first
}
func firstFactorsCopy(first map[any]map[any]struct{}, fs []*Factor) map[any]struct{} {
this := first[fs[0]]
if len(fs) == 1 {
return this
}
if _, ok := this[Literal{}]; !ok {
return this
}
copy := make(map[any]struct{}, len(this))
for k, v := range this {
copy[k] = v
}
delete(this, Literal{})
merge(firstFactorsCopy(first, fs[1:]), copy)
return copy
}
func follow(first map[any]map[any]struct{}, all map[string]map[any]struct{}, p *Production, item any) {
switch item := item.(type) {
case nil:
case *Expression:
if item == nil {
break
}
for _, t := range item.Terms {
follow(first, all, p, t)
}
case *Factor:
if item == nil {
break
}
follow(first, all, p, item.Group)
follow(first, all, p, item.Identifier)
follow(first, all, p, item.Literal)
follow(first, all, p, item.Option)
follow(first, all, p, item.Repetition)
case *Identifier:
case *Literal:
case *Production:
if item == nil {
break
}
if all[item.Identifier.Text] == nil {
all[item.Identifier.Text] = map[any]struct{}{}
}
follow(first, all, item, item.Expression)
case *Term:
if item == nil {
break
}
for i, f := range item.Factors {
if f.Identifier == nil || terminal(f.Identifier) {
follow(first, all, p, f)
continue
}
this := all[f.Identifier.Text]
if this == nil {
this = map[any]struct{}{}
all[f.Identifier.Text] = this
}
if i == len(item.Factors)-1 {
merge(map[any]struct{}{*p.Identifier: {}}, this)
} else {
rest := firstFactorsCopy(first, item.Factors[i+1:])
if _, ok := rest[Literal{}]; ok {
delete(rest, Literal{})
rest[*p.Identifier] = struct{}{}
}
merge(rest, this)
}
}
}
}
func (g Grammar) follow(first map[any]map[any]struct{}) map[string]map[any]struct{} {
all := map[string]map[any]struct{}{}
for _, p := range g.Productions {
follow(first, all, nil, p)
delete(all[p.Identifier.Text], *p.Identifier)
}
for {
var merged bool
for _, terminals := range all {
for t := range terminals {
if i, ok := t.(Identifier); ok && !terminal(&i) {
existing := all[i.Text]
merge(existing, terminals)
delete(terminals, t)
merged = true
}
}
}
if !merged {
break
}
}
return all
}
// Follow returns the follow terminals of a valid grammar.
// The map keys are nonterminal identifier strings.
// The map values are sets of [Identifier] and [Literal] values.
func (g Grammar) Follow() map[string]map[any]struct{} {
return g.follow(g.First())
}
// FirstFirstConflictError indicates that a grammar is not LL(1) due to a first/first conflict.
type FirstFirstConflictError struct {
Nonterminal string
Terminal any // an Identifier or Literal
}
// Error explains the conflict for the nonterminal and terminal involved.
func (f FirstFirstConflictError) Error() string {
var kind, content string
switch t := f.Terminal.(type) {
case Identifier:
kind = "identifier"
content = t.Text
case Literal:
kind = "literal"
content = t.Text
default:
panic(f.Terminal)
}
return fmt.Sprintf("first/first conflict for %s %q for nonterminal %q", kind, content, f.Nonterminal)
}
func (g Grammar) firstFirstConflict(first map[any]map[any]struct{}) error {
for _, p := range g.Productions {
terminals := map[any]struct{}{}
for _, t := range p.Expression.Terms {
for terminal := range first[t] {
if _, ok := terminals[terminal]; ok {
return FirstFirstConflictError{Nonterminal: p.Identifier.Text, Terminal: terminal}
}
terminals[terminal] = struct{}{}
}
}
}
return nil
}
// FirstFollowConflictError indicates that a grammar is not LL(1) due to a first/follow conflict.
type FirstFollowConflictError struct {
Nonterminal string
Terminal any // an Identifier or Literal
}
// Error explains the conflict for the nonterminal and terminal involved.
func (f FirstFollowConflictError) Error() string {
var kind, content string
switch t := f.Terminal.(type) {
case Identifier:
kind = "identifier"
content = t.Text
case Literal:
kind = "literal"
content = t.Text
default:
panic(f.Terminal)
}
return fmt.Sprintf("first/follow conflict for %s %q for nonterminal %q", kind, content, f.Nonterminal)
}
func (g Grammar) firstFollowConflict(first map[any]map[any]struct{}, follow map[string]map[any]struct{}) error {
for _, p := range g.Productions {
thisFirst := first[*p.Identifier]
if _, ok := thisFirst[Literal{}]; !ok {
continue
}
thisFollow := follow[p.Identifier.Text]
var smaller, larger map[any]struct{}
if len(thisFirst) < len(thisFollow) {
smaller = thisFirst
larger = thisFollow
} else {
smaller = thisFollow
larger = thisFirst
}
for x := range smaller {
if _, ok := larger[x]; ok {
return FirstFollowConflictError{Nonterminal: p.Identifier.Text, Terminal: x}
}
}
}
return nil
}
// LL1 determines whether a valid grammar is LL(1).
// It returns first/first and first/follow conflicts.
func (g Grammar) LL1() error {
first := g.First()
follow := g.follow(first)
if err := g.firstFirstConflict(first); err != nil {
return err
}
return g.firstFollowConflict(first, follow)
}
// String returns the production strings joined by a line feed.
func (g Grammar) String() string {
ss := make([]string, len(g.Productions))
for i, p := range g.Productions {
ss[i] = fmt.Sprint(p)
}
return strings.Join(ss, "\n")
}
func traverse(item any, visit func(any)) {
if item == nil {
return
}
visit(item)
switch item := item.(type) {
case *Expression:
for _, t := range item.Terms {
visit(t)
traverse(t, visit)
}
case *Factor:
if item.Group != nil {
visit(item.Group)
traverse(item.Group, visit)
}
if item.Identifier != nil {
visit(item.Identifier)
traverse(item.Identifier, visit)
}
if item.Literal != nil {
visit(item.Literal)
traverse(item.Literal, visit)
}
if item.Option != nil {
visit(item.Option)
traverse(item.Option, visit)
}
if item.Repetition != nil {
visit(item.Repetition)
traverse(item.Repetition, visit)
}
case *Grammar:
for _, p := range item.Productions {
visit(p)
traverse(p, visit)
}
case *Identifier:
case *Literal:
case *Production:
// item.Identifier is not visited or traversed for validation reasons.
visit(item.Expression)
traverse(item.Expression, visit)
case *Term:
for _, f := range item.Factors {
visit(f)
traverse(f, visit)
}
}
}
// Validate checks that production nonterminal identifiers are capitalized, defined, and used.
func (g Grammar) Validate() error {
var errs []error
used := map[string]bool{}
traverse(&g, func(item any) {
switch item := item.(type) {
case *Grammar:
for _, p := range item.Productions {
if p.Identifier != nil && len(p.Identifier.Text) > 0 && !terminal(p.Identifier) {
if _, ok := used[p.Identifier.Text]; ok {
errs = append(errs, fmt.Errorf("identifier %q is defined twice", p.Identifier.Text))
continue
}
used[p.Identifier.Text] = false
}
}
case *Identifier:
if !terminal(item) {
b, ok := used[item.Text]
if !ok {
errs = append(errs, fmt.Errorf("identifier %q is undefined", item.Text))
return
}
if !b {
used[item.Text] = true
}
}
case *Production:
if terminal(item.Identifier) {
errs = append(errs, fmt.Errorf("identifier %q starts with a lowercase character", item.Identifier.Text))
return
}
}
})
for ident, b := range used {
if ident != g.Productions[0].Identifier.Text && !b {
errs = append(errs, fmt.Errorf("identifier %q is unused", ident))
}
}
if len(errs) > 0 {
return Error{Errors: errs}
}
return nil
}