calvin
calvin

Reputation: 978

Go uses Go to parse itself?

I am starting a class project that regards adding some functionality to Go.

However, I am thoroughly confused on the structure of Go. I was under the impression that Go used flex and bison but I can't find anything familiar in the Go source code.

On the other hand, the directory go/src/pkg/go has folders with familiar names (ast, token, parser, etc.) but all they contain are .go files. I'm confused!

My request is, of anyone familiar with Go, can you give me an overview of how Go is lexed, parsed, etc. and where to find the files to edit the grammar and whatnot?

Upvotes: 12

Views: 906

Answers (3)

user811773
user811773

Reputation:

The directory structure:

src/cmd/5*   ARM
src/cmd/6*   amd64 (x86-64)
src/cmd/8*   i386 (x86-32)

src/cmd/cc   C compiler  (common part)
src/cmd/gc   Go compiler (common part)
src/cmd/ld   Linker      (common part)
src/cmd/6c   C compiler  (amd64-specific part)
src/cmd/6g   Go compiler (amd64-specific part)
src/cmd/6l   Linker      (amd64-specific part)

Lexer is written in pure C (no flex). Grammar is written in Bison:

src/cmd/gc/lex.c
src/cmd/gc/go.y

Many directories under src/cmd contain a doc.go file with short description of the directory's contents.

If you are planning to modify the grammar, it should be noted that the Bison grammar sometimes does not distinguish between expressions and types.

Upvotes: 8

zzzz
zzzz

Reputation: 91203

lex.c go.y

Upvotes: 4

mikerobi
mikerobi

Reputation: 20878

The Go compilers are written in c, which is why you need flex and bison. The Go package for parsing is not used. If you wanted to write a self hosting compiler in Go, you could use the Go parsing package.

Upvotes: 3

Related Questions