Skip to content

Instantly share code, notes, and snippets.

View kfsone's full-sized avatar

Oliver Smith kfsone

View GitHub Profile
@kfsone
kfsone / python-perf.ipynb
Created April 17, 2021 23:52
Optimizations in Python
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
+ mkdir build
+ cd build
+ cmake --version
cmake version 3.20.1
CMake suite maintained and supported by Kitware (kitware.com/cmake).
+ CC=gcc-10
+ CXX=g++-10
+ cmake .. -DCMAKE_VERBOSE_MAKEFILE=ON
-- The CXX compiler identification is GNU 10.2.1
example/bools/parser/productionstable.go
diff -u example/bools/parser/productionstable.go.orig example/bools/parser/productionstable.go
--- example/bools/parser/productionstable.go.orig 2021-03-19 10:59:15.160000000 -0700
+++ example/bools/parser/productionstable.go 2021-03-19 10:59:15.160000000 -0700
@@ -2,7 +2,9 @@
package parser
-import ( "github.com/goccmack/gocc/example/bools/ast" )
+import (
make install
make[1]: Entering directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc'
go install .
make[1]: Leaving directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc'
make -C example regenerate
make[1]: Entering directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc/example'
make -C astx regenerate
make[2]: Entering directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc/example/astx'
gocc ast.bnf
make[2]: Leaving directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc/example/astx'
go vet -methods=false .
go get github.com/kisielk/[email protected]
go: finding github.com v1.2.0
go: finding github.com/kisielk v1.2.0
errcheck -exclude .errcheck-ignore ./...
make install
make[1]: Entering directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc'
go install .
make[1]: Leaving directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc'
make -C example regenerate
make[1]: Entering directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc/example'
make -C astx regenerate
make[2]: Entering directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc/example/astx'
gocc ast.bnf
make[2]: Leaving directory '/mnt/c/Users/oliver/go/src/github.com/kfsone/gocc/example/astx'
@kfsone
kfsone / structuredrules.md
Last active March 16, 2021 18:50
structured rule example

Structured Rules

These are mostly syntactic sugar for deeper code generation and integration with the user's AST.

They are also tied to a top-down concept that helps guide parsing by supporting context.

E.g: How do you validate the type of a function parameter before the end of the body?

StructuredRules grammar is a superset of BNF.

@kfsone
kfsone / parsing.md
Last active March 16, 2021 17:37
Thoughts on a parser generator

The thinking

I'm thinking I might just write a typical ebnf generator, but then I'd be going from reinventing the wheel to reinventing the wheel, tire and hubcap.

EBNF generators I've used are very hands-offy because providing for the most common paths might somehow exclude some use cases.

So I think I want something that is EBNF+, and which is focused on describing c/json/lua/python type DSLs in a top-down way that allows accumulation of guidance through context, allows the user to validate in-situ rather than forcing them to write the code to build the parse tree, code to walk it and validate it and then code to do anything with it.

Start from EBNF and extend sideways, if you will.

func (e *Error) Error() string {
w := new(strings.Builder)
fmt.Fprintf(w, "%d:%d: error: ", e.ErrorToken.Pos.Line, e.ErrorToken.Pos.Column)
if e.Err != nil {
fmt.Fprint(w, e.Err)
} else {
if len(e.ExpectedTokens) == 1 {
// if there's only one expectation, don't describe it in list terms.
fmt.Fprint(w, "expected ", e.ExpectedTokens[0])
func (e *Error) Error() string {
w := new(strings.Builder)
fmt.Fprintf(w, "%d:%d: error: ", e.ErrorToken.Pos.Line, e.ErrorToken.Pos.Column)
if e.Err != nil {
fmt.Fprintf(w, "%w: %q", e.Err, e.ErrorToken.Lit)
} else {
if len(e.ExpectedTokens) == 1 {
// if there's only one expectation, don't describe it in list terms.
fmt.Fprint(w, "expected: ", e.ExpectedTokens[0])