Go Home Page
The Go Programming Language

Package ebnf

import "ebnf"

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(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

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.Position

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.Position
}

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(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.

type Group

A Group node represents a grouped expression.

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

type Name

A Name node represents a production name.

type Name struct {
    token.Position
    String string
}

type Option

An Option node represents an optional expression.

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

type Production

A Production node represents an EBNF production.

type Production struct {
    Name *Name
    Expr Expression
}

func (*Production) Pos

func (p *Production) Pos() token.Position

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.Position

type Repetition

A Repetition node represents a repeated expression.

type Repetition struct {
    token.Position
    Body Expression // {body}
}

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.Position

type Token

A Token node represents a literal.

type Token struct {
    token.Position
    String string
}