What's the difference among Expression,Statements and Declaration from the view of compiler?

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

What's the difference among Expression,Statements and Declaration from the view of compiler?

问题

我正在阅读 这里 的 Go 语言源代码 ast.go,其中有三种接口类型:Expression(表达式)、Statement(语句)和Declaration(声明)。但仅凭源代码,我无法弄清它们之间的区别。我能够理解的是,表达式会产生一个可以赋值、比较或用作参数的对象,而语句则是一些流程控制,比如 if-else 或 for 循环。

但我发现了一些定义,比如:

// An IncDecStmt node represents an increment or decrement statement.
IncDecStmt struct {
        X      Expr
        TokPos token.Pos   // position of Tok
        Tok    token.Token // INC or DEC
}

这不应该是一个表达式吗?我感到困惑,如何区分表达式和语句,是否有任何规则可遵循?

英文:

I am going through the Go source code of ast.go at here,and there are 3 types of interfaces that are Expression,Statement and Declaration. But only with the source code I couldn't figure out the difference between them.What I could figure out is that expression results in a object that could be assigned or compared or used as parameter,while statements are some flow control like if-else or for loop.
But I found some definitions like

    // An IncDecStmt node represents an increment or decrement statement.
    IncDecStmt struct {
            X      Expr
            TokPos token.Pos   // position of Tok
            Tok    token.Token // INC or DEC
    }

shouldn't it be a expression?I feel confused how to distinguish expressions and statements,are there any rules?

答案1

得分: 7

这些是计算机科学中常见的术语。表达式产生一个值,而语句通常不产生值(实际上,这一点取决于编程语言,事实上,有些语言将表达式视为语句的一个子类)。

根据维基百科的解释:

> 在大多数语言中,语句与表达式的区别在于语句不返回结果,仅用于其副作用,而表达式总是返回结果,通常根本没有副作用。

在Go语言中,表达式可以用作语句

你的困惑似乎来自于增量和减量运算符。在Go语言中,与大多数基于C的语言不同,增量和减量语句不产生值,它们是语句而不是表达式。也就是说,你不能写成:

b := a++
英文:

Those are common terms in CS. An expression produces a value while a statement often doesn't (this last point depends of the language, in fact, some languages consider expressions as a subcategory of statements).

From Wikipedia :

> In most languages, statements contrast with expressions in that
> statements do not return results and are executed solely for their
> side effects, while expressions always return a result and often do
> not have side effects at all.

In Go an expression may be used as statement.

Your confusion seems to come from the increment and decrement operators. In Go, contrary to most C-based languages, the increment and decrement statements don't produce a value, they're statements and not expressions. That is, you may not write

b := a++

答案2

得分: 7

Golang规范中使用了以下术语:

  • 表达式:通过将运算符和函数应用于操作数来指定值的计算。
  • 语句:控制执行流程。
  • 声明(和作用域):将非空标识符绑定到常量、类型、变量、函数、标签或包。

IncDecStmt被定义为:

IncDecStmt = Expression ( "++" | "--" )。

"++" 和 "--" 语句通过未类型化常量1对其操作数进行递增或递减。

它使用了一个表达式,但仍然是一个语句(不产生新值)。

注意:未类型化常量是指在声明常量时没有明确指定其类型:

i := 0 // int8? uint8? int16? ...

未类型化常量有一个默认类型,在需要类型化值的上下文中,常量会被隐式转换为该类型。例如,在没有显式类型的短变量声明中。

未类型化常量的默认类型分别为boolruneintfloat64complex128string,具体取决于它是布尔、符文、整数、浮点数、复数还是字符串常量。

英文:

The Golang spec uses those terms:

  • Expressions: specifies the computation of a value by applying operators and functions to operands.
  • Statements: control execution
  • Declarations (and scope): binds a non-blank identifier to a constant, type, variable, function, label, or package

The IncDecStmt is specified as

IncDecStmt = Expression ( "++" | "--" ) .

> The "++" and "--" statements increment or decrement their operands by the untyped constant 1.

It uses an expression, but remains a statement (don't produce a new value).

Note: an untyped constant is when you declare a constant without explicitly mentioning its type:

i := 0 # int8? uint8? int16? ...

> An untyped constant has a default type which is the type to which the constant is implicitly converted in contexts where a typed value is required, for instance, in a short variable declaration such as where there is no explicit type.

> The default type of an untyped constant is bool, rune, int, float64, complex128 or string respectively, depending on whether it is a boolean, rune, integer, floating-point, complex, or string constant.

huangapple
  • 本文由 发表于 2014年11月13日 21:53:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/26910493.html
匿名

发表评论

匿名网友

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

确定