Go使用Go来解析自身?

huangapple go评论85阅读模式
英文:

Go uses Go to parse itself?

问题

我正在开始一个关于给Go添加一些功能的课程项目。

然而,我对Go的结构感到非常困惑。我原以为Go使用flex和bison,但在Go源代码中找不到任何熟悉的东西。

另一方面,go/src/pkg/go目录下有一些熟悉的文件夹(ast、token、parser等),但它们只包含.go文件。我感到困惑!

我的请求是,如果有人熟悉Go,能否给我一个关于Go如何进行词法分析、语法分析等的概述,并告诉我在哪里找到编辑语法等文件?

英文:

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?

答案1

得分: 8

目录结构:

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

src/cmd/cc   C编译器(公共部分)
src/cmd/gc   Go编译器(公共部分)
src/cmd/ld   链接器(公共部分)
src/cmd/6c   C编译器(amd64特定部分)
src/cmd/6g   Go编译器(amd64特定部分)
src/cmd/6l   链接器(amd64特定部分)

词法分析器使用纯C编写(没有使用flex)。语法分析器使用Bison编写:

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

src/cmd目录下的许多子目录都包含一个doc.go文件,其中包含该目录内容的简短描述。

如果您计划修改语法,需要注意的是Bison语法有时无法区分表达式和类型。

英文:

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.

答案2

得分: 4

lex.c
go.y

英文:

lex.c
go.y

答案3

得分: 3

Go编译器是用C语言编写的,这就是为什么你需要使用flex和bison。Go的解析包没有被使用。如果你想要用Go编写一个自举编译器,你可以使用Go的解析包。

英文:

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.

huangapple
  • 本文由 发表于 2012年3月28日 00:53:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/9894098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定