The Go Programming Language

Package ebnf

import "ebnf"

Package ebnf is a library for EBNF grammars. The input is text ([]byte) satisfying the following grammar (represented itself in EBNF):

Production  = name "=" [ Expression ] "." .
Expression  = Alternative { "|" Alternative } .
Alternative = Term { Term } .
Term        = name | token [ "…" token ] | Group | Option | Repetition .
Group       = "(" Expression ")" .
Option      = "[" Expression "]" .
Repetition  = "{" Expression "}" .

A name is a Go identifier, a token is a Go string, and comments and white space follow the same rules as for the Go language. Production names starting with an uppercase Unicode letter denote non-terminal productions (i.e., productions which allow white-space and comments between tokens); all other production names denote lexical productions.

Package files

ebnf.go parser.go

func Verify

func Verify(fset *token.FileSet, grammar Grammar, start string) os.Error

Verify checks that:

- all productions used are defined
- all productions defined are used when beginning at start
- lexical productions refer only to other lexical productions

Position information is interpreted relative to the file set fset.

type Alternative

An Alternative node represents a non-empty list of alternative expressions.

type Alternative []Expression // x | y | z

func (Alternative) Pos

func (x Alternative) Pos() token.Pos

type Bad

A Bad node stands for pieces of source code that lead to a parse error.

type Bad struct {
    TokPos token.Pos
    Error  string // parser error message
}

func (*Bad) Pos

func (x *Bad) Pos() token.Pos

type Expression

An Expression node represents a production expression.

type Expression interface {
    // Pos is the position of the first character of the syntactic construct
    Pos() token.Pos
}

type Grammar

A Grammar is a set of EBNF productions. The map is indexed by production name.

type Grammar map[string]*Production

func Parse

func Parse(fset *token.FileSet, filename string, src []byte) (Grammar, os.Error)

Parse parses a set of EBNF productions from source src. It returns a set of productions. Errors are reported for incorrect syntax and if a production is declared more than once. Position information is recorded relative to the file set fset.

type Group

A Group node represents a grouped expression.

type Group struct {
    Lparen token.Pos
    Body   Expression // (body)
}

func (*Group) Pos

func (x *Group) Pos() token.Pos

type Name

A Name node represents a production name.

type Name struct {
    StringPos token.Pos
    String    string
}

func (*Name) Pos

func (x *Name) Pos() token.Pos

type Option

An Option node represents an optional expression.

type Option struct {
    Lbrack token.Pos
    Body   Expression // [body]
}

func (*Option) Pos

func (x *Option) Pos() token.Pos

type Production

A Production node represents an EBNF production.

type Production struct {
    Name *Name
    Expr Expression
}

func (*Production) Pos

func (x *Production) Pos() token.Pos

type Range

A List node represents a range of characters.

type Range struct {
    Begin, End *Token // begin ... end
}

func (*Range) Pos

func (x *Range) Pos() token.Pos

type Repetition

A Repetition node represents a repeated expression.

type Repetition struct {
    Lbrace token.Pos
    Body   Expression // {body}
}

func (*Repetition) Pos

func (x *Repetition) Pos() token.Pos

type Sequence

A Sequence node represents a non-empty list of sequential expressions.

type Sequence []Expression // x y z

func (Sequence) Pos

func (x Sequence) Pos() token.Pos

type Token

A Token node represents a literal.

type Token struct {
    StringPos token.Pos
    String    string
}

func (*Token) Pos

func (x *Token) Pos() token.Pos

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.