Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created July 14, 2013 18:32
Show Gist options
  • Save jordanorelli/5995251 to your computer and use it in GitHub Desktop.
Save jordanorelli/5995251 to your computer and use it in GitHub Desktop.
simple DOL-System
package main
import (
"bytes"
"fmt"
)
type ProductionSet map[rune]string
type Alphabet map[rune]bool
type DOLSystem struct {
Alphabet
Axiom string
Productions ProductionSet
}
func (s *DOLSystem) Next() string {
var buf bytes.Buffer
for _, r := range s.Axiom {
p, ok := s.Productions[r]
if !ok {
panic("no production exists for rune " + string(r))
}
buf.WriteString(p)
}
n := buf.String()
s.Axiom = n
return n
}
func NewDOLSystem(V Alphabet, axiom string, P ProductionSet) *DOLSystem {
return &DOLSystem{V, axiom, P}
}
func main() {
s := DOLSystem{
Alphabet{'a': true, 'b': true},
"a",
ProductionSet{'a': "ab", 'b': "a"},
}
for i := 0; i < 5; i++ {
fmt.Println(s.Next())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment