lrparser

package module
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: BSD-3-Clause Imports: 6 Imported by: 2

README

lrparser

This is a library for creating LR parsers. It's similar to attribute grammars in that you can equip every rule with a lambda function that represents the semantic counterpart of the syntactic rule (for example, it can generate the value of an expression, as illustrated in the example below).

Example:

gr := lrparser.NewGrammar(lrparser.MustBuildRules([]*lrparser.SynSem{
	{Syn: `Init -> Expr`, Sem: func(args []any) any { return args[0] }},
	{Syn: `Expr -> "expr" AddExpr`, Sem: func(args []any) any { return args[1] }},
	{Syn: `AddExpr -> AddExpr "+" MulExpr`, Sem: func(args []any) any { return args[0].(int) + args[2].(int) }},
	{Syn: `AddExpr -> AddExpr "-" MulExpr`, Sem: func(args []any) any { return args[0].(int) - args[2].(int) }},
	{Syn: `AddExpr -> MulExpr`, Sem: func(args []any) any { return args[0] }},
	{Syn: `MulExpr -> MulExpr "*" ConstExpr`, Sem: func(args []any) any { return args[0].(int) * args[2].(int) }},
	{Syn: `MulExpr -> MulExpr "/" ConstExpr`, Sem: func(args []any) any { return args[0].(int) / args[2].(int) }},
	{Syn: `MulExpr -> ConstExpr`, Sem: func(args []any) any { return args[0] }},
	{Syn: `ConstExpr -> integer`, Sem: func(args []any) any { return args[0] }},
}))

See cmd/example

Documentation

Overview

Package lrparser is an LR-parser.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CoalesceSymbols

func CoalesceSymbols(tokens []*textkit.Token, clusters []string) []*textkit.Token

CoalesceSymbols joins symbols together.

Types

type Grammar

type Grammar struct {
	// The rules of the grammar.
	Rules []*Rule
	// contains filtered or unexported fields
}

Grammar is a formal grammar.

func NewGrammar

func NewGrammar(rules ...[]*Rule) *Grammar

NewGrammar returns a new grammar.

func (*Grammar) BuildItems

func (gr *Grammar) BuildItems()

BuildItems builds the items of the automaton.

func (*Grammar) Parse

func (gr *Grammar) Parse(tokens []*textkit.Token) (any, error)

Parse parses a list of tokens.

type Item

type Item struct {
	LHS    string
	RHS    []string
	DotPos int
}

Item is an item of the parser.

func (*Item) String

func (it *Item) String() string

type Located added in v0.1.4

type Located interface {
	Location() *textkit.Location
	SetLocation(*textkit.Location)
}

Located specifies methods for AST node location.

type Operator

type Operator struct {
	Associativity OperatorAssociativity
	Priority      int
	Symbols       []string
}

Operator ...

func (Operator) Name

func (op Operator) Name() string

Name returns the operator's name.

type OperatorAssociativity

type OperatorAssociativity int

OperatorAssociativity ...

const (
	LeftAssociative OperatorAssociativity = iota
	RightAssociative
	NonAssociative
)

constants for OperatorAssociativity

type ParseError added in v0.1.10

type ParseError struct {
	Message string
	Loc     *textkit.Location
}

ParseError is a parse error.

func (*ParseError) Error added in v0.1.10

func (err *ParseError) Error() string

type Rule

type Rule struct {
	LHS  string
	RHS  []string
	Conv func([]any) any
}

Rule is a context-free rule with a builder function.

func BuildListRules

func BuildListRules(root, leaf string, canBeEmpty bool, leftBracket, sep, rightBracket string, builder func([]any) any) []*Rule

BuildListRules builds list rules.

func BuildOperatorRules

func BuildOperatorRules(root, leaf string, ops []Operator, builder func(string, any, any) any) []*Rule

BuildOperatorRules builds operator rules.

func BuildOptSeq

func BuildOptSeq(root string, head, tail []string, builder func([]any, []any) any) []*Rule

BuildOptSeq builds an optional sequence.

func BuildRule added in v0.1.5

func BuildRule(def string, f func([]any) any) (*Rule, error)

BuildRule builds a grammar rule from a string.

func BuildRules added in v0.1.6

func BuildRules(list []*SynSem) ([]*Rule, error)

BuildRules creates rules from a slice of `SynSem`s.

func MustBuildRule added in v0.1.5

func MustBuildRule(def string, f func([]any) any) *Rule

MustBuildRule builds a grammar rule from a string. It panics on error.

func MustBuildRules added in v0.1.6

func MustBuildRules(list []*SynSem) []*Rule

MustBuildRules creates rules from a slice of `SynSem`s. It panics on error.

func (*Rule) String

func (r *Rule) String() string

String returns a string representation of the rule.

type State

type State struct {
	Items []*Item
}

State is a state of the parser.

func (*State) String

func (st *State) String() string

type SynSem added in v0.1.6

type SynSem struct {
	Syn string
	Sem func([]any) any
}

SynSem is a syntactic rule with its semantic counterpart (a lambda function).

Directories

Path Synopsis
cmd
example command

Jump to

Keyboard shortcuts

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