grammar

package
v0.0.0-...-596da54 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatProduction

func FormatProduction(symbols []Symbol, dotIndex int) string

Types

type Grammar

type Grammar struct {
	// contains filtered or unexported fields
}

func New

func New(productions []Production) *Grammar

New constructs a grammar from the given productions. The first production is special, namely, the head symbol (aka the left most symbol) of the first production is considered to be the special 'program' symbol that is the topmost non-terminal; the last symbol of the first production is considered to be the special 'eof' symbol that indicates the end of input (aka the last token). If in doubt, use the production 'program -> S eof' as the first production, and as second production define 'S' and then all the subsequent productions. For example:

program -> S eof
S -> ...
A -> ...
...

func NewSimple

func NewSimple(productionLines []ProductionLine) (*Grammar, error)

NewSimple is a convenience function that parses each production from a textual representation, and then constructs a grammar using 'New'. It's important to notice that the first production is special. See 'New' for more details. For example:

program -> S eof
S -> ...
A -> ...
...

func (*Grammar) EOFSymbol

func (g *Grammar) EOFSymbol() Symbol

Returns the special "eof" symbol. The "eof" symbol is given to 'New'.

func (*Grammar) GetFirstSet

func (g *Grammar) GetFirstSet(symbol Symbol) mapset.Set[Symbol]

func (*Grammar) GetProductionIndices

func (g *Grammar) GetProductionIndices(symbol Symbol) []int

func (*Grammar) GetProductions

func (g *Grammar) GetProductions(nonTerminal Symbol) []Production

GetProductions returns the productions for the given non-Terminal symbol.

func (*Grammar) IsNonTerminal

func (g *Grammar) IsNonTerminal(symbol Symbol) bool

func (*Grammar) IsTerminal

func (g *Grammar) IsTerminal(symbol Symbol) bool

func (*Grammar) NonTerminals

func (g *Grammar) NonTerminals() []Symbol

func (*Grammar) Productions

func (g *Grammar) Productions() []Production

func (*Grammar) ProgramSymbol

func (g *Grammar) ProgramSymbol() Symbol

Returns the special "program" symbol, which is the non-Terminal symbol of the first production.

func (*Grammar) String

func (g *Grammar) String() string

func (*Grammar) Terminals

func (g *Grammar) Terminals() []Symbol

type Production

type Production struct {
	Symbols []Symbol
	Action  func([]any) any
}

func (Production) String

func (p Production) String() string

type ProductionLine

type ProductionLine struct {
	Line   string
	Action func([]any) any
}

type Symbol

type Symbol struct {
	Name string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL