Documentation ¶
Overview ¶
Package ebnf represents and parses a variant of Extended Backus-Naur Form called Wirth Syntax Notation. Terminal identifiers must begin with a lowercase letter. Non-terminal identifiers must begin with an uppercase letter. The first production defines the start non-terminal identifier. Terminal identifiers are assumed to be defined elsewhere. Epsilon is represented by an empty literal.
Grammars are written like so:
Expression = Term { ( "+" | "-" ) Term } . Term = Factor { ( "*" | "/" ) Factor } . Factor = Number | "(" Expression ")" . Number = Digit { Digit } . Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" .
They can be parsed by Parse into a Grammar. Grammar.Validate determines whether a grammar is valid. Grammar.First and Grammar.Follow compute the first and follow sets for non-terminal identifiers. Grammar.Conflict determines whether a valid grammar can be parsed by an LL(1) parser.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Expression ¶ added in v0.2.0
type Expression struct {
Terms []*Term
}
Expression is the right side of a production. There must be at least one term. All terms must not be nil.
func (Expression) String ¶ added in v0.2.0
func (e Expression) String() string
String returns the term strings joined by " | ".
type Factor ¶
type Factor struct { Group *Expression Identifier *Identifier Literal *Literal Option *Expression Repetition *Expression }
Factor is a term sequence. One, and only one, of the fields must not be nil.
type FirstFirstConflictError ¶ added in v0.2.0
FirstFirstConflictError is a first/first LL(1) grammar parse conflict.
func (FirstFirstConflictError) Error ¶ added in v0.2.0
func (f FirstFirstConflictError) Error() string
Error indicates a first/first conflict exists for an LL(1) parser
type FirstFollowConflictError ¶ added in v0.2.0
type FirstFollowConflictError struct { Nonterminal string Terminal any // an Identifier or Literal }
FirstFollowConflictError is a first/follow LL(1) grammar parse conflict.
func (FirstFollowConflictError) Error ¶ added in v0.2.0
func (f FirstFollowConflictError) Error() string
type Grammar ¶
type Grammar struct {
Productions []*Production
}
Grammar is an abstract syntax tree for a grammar. There must be at least one production.
func (Grammar) Conflict ¶ added in v0.2.0
Conflict returns whether a valid grammar has a first/first or first/follow conflict for an LL(1) parser. Either a FirstFirstConflictError, a FirstFollowConflictError, or nil are returned.
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 non-terminals 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 non-terminal Identifier text values. The map values are sets of Identifier and Literal values.
type Identifier ¶ added in v0.2.0
type Identifier struct {
Text string
}
Identifier is a terminal or non-terminal identifier. The text must not be empty.
func (Identifier) String ¶ added in v0.2.0
func (i Identifier) String() string
String returns the text.
type Literal ¶ added in v0.2.0
type Literal struct {
Text string
}
Literal is the content of a quoted string. 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 separated by " = ", followed by a period.