Skip to content

Commit 9be9acd

Browse files
committed
Specify grammar syntax with a grammar
1 parent 4035c6c commit 9be9acd

3 files changed

Lines changed: 32 additions & 10 deletions

File tree

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ Package ebnf represents and parses a variant of [Extended Backus-Naur Form](http
44

55
Grammars are written like so:
66

7-
Expression = Term {("+" | "-") Term}.
8-
Term = Factor {("*" | "/") Factor}.
9-
Factor = Number | "(" Expression ")".
10-
Number = Digit {Digit}.
11-
Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9".
7+
Grammar = {Production}.
8+
Production = Identifier "=" Expression ".".
9+
Expression = Term {"|" Term}.
10+
Term = Factor {Factor}.
11+
Factor = Group | Identifier | Literal | Option | Repetition.
12+
Group = "(" Expression ")".
13+
Identifier = letter {letter}.
14+
Literal = "\"" {character} "\"".
15+
Option = "[" Expression "]".
16+
Repetition = "{" Expression "}".
1217

1318
They can be parsed by [Parse](https://pkg.go.dev/github.com/willfaught/ebnf#Parse) into a [Grammar](https://pkg.go.dev/github.com/willfaught/ebnf#Grammar). [Grammar.Validate](https://pkg.go.dev/github.com/willfaught/ebnf#Grammar.Validate) determines whether a grammar is valid. [Grammar.First](https://pkg.go.dev/github.com/willfaught/ebnf#Grammar.First) and [Grammar.Follow](https://pkg.go.dev/github.com/willfaught/ebnf#Grammar.Follow) compute the first and follow sets for nonterminal identifiers. [Grammar.LL1](https://pkg.go.dev/github.com/willfaught/ebnf#Grammar.LL1) determines whether a valid grammar can be parsed by an LL(1) parser.

ebnf.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
//
88
// Grammars are written like so:
99
//
10-
// Expression = Term {("+" | "-") Term}.
11-
// Term = Factor {("*" | "/") Factor}.
12-
// Factor = Number | "(" Expression ")".
13-
// Number = Digit {Digit}.
14-
// Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9".
10+
// Grammar = {Production}.
11+
// Production = Identifier "=" Expression ".".
12+
// Expression = Term {"|" Term}.
13+
// Term = Factor {Factor}.
14+
// Factor = Group | Identifier | Literal | Option | Repetition.
15+
// Group = "(" Expression ")".
16+
// Identifier = letter {letter}.
17+
// Literal = "\"" {character} "\"".
18+
// Option = "[" Expression "]".
19+
// Repetition = "{" Expression "}".
1520
//
1621
// They can be parsed by [Parse] into a [Grammar].
1722
// [Grammar.Validate] determines whether a grammar is valid.

ebnf_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,17 @@ TA = F TB.
245245
TB = "*" F TB | "".
246246
F = id | "(" EA ")".`
247247

248+
const wsn = `Grammar = {Production}.
249+
Production = Identifier "=" Expression ".".
250+
Expression = Term {"|" Term}.
251+
Term = Factor {Factor}.
252+
Factor = Group | Identifier | Literal | Option | Repetition.
253+
Group = "(" Expression ")".
254+
Identifier = letter {letter}.
255+
Literal = "\"" {character} "\"".
256+
Option = "[" Expression "]".
257+
Repetition = "{" Expression "}".`
258+
248259
func TestParseValid(t *testing.T) {
249260
t.Parallel()
250261
for _, test := range []string{
@@ -293,6 +304,7 @@ func TestParseValidateValid(t *testing.T) {
293304
arith,
294305
modula2,
295306
oberon2,
307+
wsn,
296308
} {
297309
t.Log(test)
298310
g, err := Parse(test)

0 commit comments

Comments
 (0)