Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 239
ggaaooppeenngg Avatar asked Nov 13 '14 13:11

ggaaooppeenngg


People also ask

What is difference between expressions and statements?

In programming language terminology, an “expression” is a combination of values and functions that are combined and interpreted by the compiler to create a new value, as opposed to a “statement” which is just a standalone unit of execution and doesn't return anything.

What is the difference between statement and expression in C language?

An expression is something that returns a value, whereas a statement does not. The Big Deal between the two is that you can chain expressions together, whereas statements cannot be chained. Sure statements can be chained.

What is the difference between expression and statement in Java?

Variable declarations and assignments, such as those in the previous section, are statements, as are the basic language structures like conditionals and loops. Expressions describe values; an expression is evaluated to produce a result, to be used as part of another expression or in a statement.

What is the difference between expression and statement in Python?

Statements represent an action or command e.g print statements, assignment statements. Expression is a combination of variables, operations and values that yields a result value. Save this answer.


1 Answers

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.

like image 152
VonC Avatar answered Sep 22 '22 14:09

VonC