Documentation
¶
Overview ¶
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.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Error ¶ added in v0.2.0
type Error struct {
Errors []error
}
Error has all lexer and parser errors.
type Expression ¶ added in v0.2.0
type Expression struct {
Terms []*Term
}
Expression is a production expression. It has a list of one or more terms. All terms must not be nil.
func (Expression) String ¶ added in v0.2.0
func (e Expression) String() string
String returns the term strings separated by " | ".
type Factor ¶
type Factor struct {
Group *Expression
Identifier *Identifier
Literal *Literal
Option *Expression
Repetition *Expression
}
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.
func (Factor) String ¶ added in v0.2.0
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.
type FirstFirstConflictError ¶ added in v0.2.0
FirstFirstConflictError indicates that a grammar is not LL(1) due to a first/first conflict.
func (FirstFirstConflictError) Error ¶ added in v0.2.0
func (f FirstFirstConflictError) Error() string
Error explains the conflict for the nonterminal and terminal involved.
type FirstFollowConflictError ¶ added in v0.2.0
type FirstFollowConflictError struct {
Nonterminal string
Terminal any // an Identifier or Literal
}
FirstFollowConflictError indicates that a grammar is not LL(1) due to a first/follow conflict.
func (FirstFollowConflictError) Error ¶ added in v0.2.0
func (f FirstFollowConflictError) Error() string
Error explains the conflict for the nonterminal and terminal involved.
type Grammar ¶
type Grammar struct {
Productions []*Production
}
Grammar is a grammar. It has a list of one or more productions. All productions must not be nil.
func (Grammar) First ¶ added in v0.2.0
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 (Grammar) FirstNonterminals ¶ added in v0.2.0
FirstNonterminals returns the first terminals of the nonterminals of a valid grammar.
func (Grammar) Follow ¶ added in v0.2.0
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 (Grammar) LL1 ¶ added in v0.5.0
LL1 determines whether a valid grammar is LL(1). It returns first/first and first/follow conflicts.
type Identifier ¶ added in v0.2.0
type Identifier struct {
Text string
}
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.
func (Identifier) String ¶ added in v0.2.0
func (i Identifier) String() string
String returns the text. For example, for "foo", it returns "foo".
type Literal ¶ added in v0.2.0
type Literal struct {
Text string
}
Literal is the text between the quotes of a literal. If the text is empty, it represents epsilon, the empty string.
type Production ¶ added in v0.2.0
type Production struct {
Identifier *Identifier
Expression *Expression
}
Production is a grammar production. The identifier and expression must not be nil.
func (Production) String ¶ added in v0.2.0
func (p Production) String() string
String returns the identifier and expression strings separated by " = " and followed by a period.