lrparser

package module
v0.1.9-b Latest Latest
Warning

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

Go to latest
Published: May 2, 2025 License: BSD-3-Clause Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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 or original 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) (interface{}, 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

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 Rule

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

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([]interface{}) interface{}) []*Rule

BuildListRules builds list rules.

func BuildOperatorRules

func BuildOperatorRules(root, leaf string, ops []Operator, builder func(string, interface{}, interface{}) interface{}) []*Rule

BuildOperatorRules builds operator rules.

func BuildOptSeq

func BuildOptSeq(root string, head, tail []string, builder func([]interface{}, []interface{}) interface{}) []*Rule

BuildOptSeq builds an optional sequence.

func BuildRule

func BuildRule(def string, f func([]interface{}) interface{}) (*Rule, error)

BuildRule builds a grammar rule from a string.

func BuildRules

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

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

func MustBuildRule

func MustBuildRule(def string, f func([]interface{}) interface{}) *Rule

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

func MustBuildRules

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

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
⌂ 换网址