Skip to content

eliben/go-ungrammar

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

go-ungrammar

Ungrammar implementation and API in Go. Blog post for background.

Ungrammar is a DSL for concrete syntax trees (CST). This implementation is based on the original ungrammar crate, also borrowing some test files from it.

Ungrammar syntax

The syntax of Ungrammar files is very simple:

//           -- comment
Name =       -- non-terminal definition
'ident'      -- token (terminal)
A B          -- sequence
A | B        -- alternation
A*           -- repetition (zero or more)
A?           -- optional (zero or one)
(A B)        -- grouping elements for precedence control
label:A      -- label hint for naming

For some concrete examples, look at files in the testdata directory.

Usage

Go Reference

Usage example:

func ExampleParseAndExamine() {
input := `
Foo = Bar Baz
Baz = ( Kay Jay )* | 'id'`
// Create an Ungrammar parser and parse input.
p := ungrammar.NewParser(input)
ungram, err := p.ParseGrammar()
if err != nil {
panic(err)
}
// Display the string representation of the parsed ungrammar.
fmt.Println(ungram.Rules["Foo"].String())
fmt.Println(ungram.Rules["Baz"].String())
// Output:
// Seq(Bar, Baz)
// Alt(Rep(Seq(Kay, Jay)), 'id')
}

For somewhat more sophisticated usage, see the cmd/ungrammar2json command.

About

Ungrammar implementation and API in Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages