Go1 编译器是如何工作的? [英] How does the Go1 compiler work?
问题描述
我已经为一个学校项目涉足 Go 大约一个月,我注意到 src/pkg/go 文件夹中的 go/ast、go/token、go/parser 等包.但是,gc 编译器基于位于 src/cmd/gc 中的 C 文件.
I've been dabbling with Go for about a month for a school project and I noticed the go/ast, go/token, go/parser, etc. packages in the src/pkg/go folder. However, the gc compiler was based on C files located in src/cmd/gc.
我的问题是关于 Go1 中用于构建和运行程序的新 go 命令:这个工具是否依赖于我上面引用的包?即如果我在/go/token/token.go 中添加了一个新令牌,它会被新的 go 编译器识别吗?
My question regards the new go command in Go1 that builds and runs programs: does this tool depend on the packages I referenced above? i.e. if I added a new token to /go/token/token.go, would it be recognized by the new go compiler?
推荐答案
Go 编译器是用纯 C 语言编写的,不使用 go/
下的包.在 Go 源代码树中,它的词法分析器位于 src/cmd/gc/lex.c 中,它的 Bison 语法是 src/cmd/gc/go.y.
The Go compiler is written in pure C and does not use the packages under go/
. In the Go source tree, its lexer lives in src/cmd/gc/lex.c and its Bison grammar is src/cmd/gc/go.y.
go/
包用于 godoc、gofmt 和各种 go 工具子命令等工具中.也许有一天它们也可以用于在 Go 中编写 Go 编译器,但目前还没有人在这条道路上走得很远.
The go/
packages are used in tools like godoc, gofmt, and various go tool subcommands. Maybe someday they can be used to write a Go compiler in Go as well, but no one's gotten very far on that path yet.
这篇关于Go1 编译器是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!