What is the difference between expressions, statements and declarations from the point of view of the compiler?

I am looking at the source code of Go ast.go on here , and there are 3 types of interfaces that are expressions, statements and declarations. But only with the source code, I could not understand the difference between the two. What I can understand is that this expression leads to an object that can be assigned or matched or used as a parameter, while statements are some kind of flow control, like if-else or for loop. But I found some type definitions

    // 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 be an expression? I feel embarrassed how to distinguish between expressions and statements, are there any rules?

+4
source share
2 answers

Golang spec uses these terms:

  • Expressions : Specifies the calculation of a value by applying operators and functions to operands.
  • Statements : control execution
  • Declarations (and scope): Associates a non-empty identifier with a constant, type, variable, function, label, or package

IncDecStmt indicated as

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

The operators "++" and "-" increase or decrease their operands by the untyped constant 1.

It uses an expression, but remains an instruction (do not create a new value).

+6
source

CS. , ( , ).

Wikipedia:

, .

Go .

, , . Go, C, increment and decment , , .

b := a++
+4

All Articles