我问这个问题的一些背景。
我正在阅读 Writing An Interpreter In Go,在书中,Tokenstruct is inside of AST Nodes。Node是一种可以通过实现tokenLiteral()和实现的类型String()
type IntegerLiteral struct {
Token token.Token
Value int64
}
type Node interface {
TokenLiteral() string
String() string
}
我了解到在现实生活中,编译器必须提供错误的行和列位置,而词法分析器无法检测到错误,因此必须将此信息传递给解析器。例如go编译器使用下面作为 AST 节点。
type Pos int
// All node types implement the Node interface.
type Node interface {
Pos() token.Pos // position of first character belonging to the node
End() token.Pos // position of first character immediately after the node
}
我的问题的长版
AFAIK,编译前端的工作方式如下stream of chars -> streams of tokens -> AST:在每个级别中,“某些东西”被抽象出来。在我眼里,一个Token不应该是的一部分AST Node
如果令牌是AST Node
你能举例说明 PL 选择哪种方式吗
白衣染霜花
相关分类