Is there a word that includes both statements and definitions/declarations

Wikipedia gives a few examples of statements that we all know:

  • Return statement: return value;
  • Assignment statement: var value = expression;
  • Call statement: expression;, e.g. action();
  • If statement short: if (expression) expression;
  • If statement long: if (expression) { n statements }

Is there a general term that includes both statements and certain elements such as a class definition, method definition, namespace importing and namespace declaration?

Bonus question: are comments statements?


Not really. Not a generally used term at least. Some grammars use the term ‘directive’ for the productions which occur at the “top level”. Other just call everything statements – e.g in Python class and function definitions are considered statements.

Comment are not statements, they are…comments.

One possibility to find such names is to have a look at language syntaxes. Because every interpreter/compiler has to parse some source code, these well-known elements (declaration, statement, etc.) must be defined in some grammar.

Let’s take a look at the Java syntax (I take Java because it’s the language I’m the more comfortable with):

Let’s begin with Statement

Identifier : Statement
StatementExpression ;
if ParExpression Statement [else Statement] assert Expression [: Expression] ;
switch ParExpression { SwitchBlockStatementGroups }
while ParExpression Statement
do Statement while ParExpression ;
for ( ForControl ) Statement
break [Identifier] ;
continue [Identifier] ;
return [Expression] ;
throw Expression ;
synchronized ParExpression Block
try Block (Catches | [Catches] Finally)
try ResourceSpecification Block [Catches] [Finally]

Let’s have a look at class declaration now:


Now a method declaration:

FormalParameters {[]} [throws QualifiedIdentifierList] (Block | 😉

And finally the import (why not after all):

import [static] Identifier { . Identifier } [. *] ;

Now if we want to find the most common term for a statement, a class declaration, a method declaration and an import one “simply” have to go up the syntax tree until we find the first common ancestor.

Doing so, we end up (not so surprisingly) at the top of the tree on the rule

[[Annotations] package QualifiedIdentifier ;] {ImportDeclaration} {TypeDeclaration}

So, syntactically speaking the answer to your question is Compilation Unit. However, this ancestor is so far from the four base elements that it would be crazy to call a statement a compilation unit. Morality: don’t try to unify many terms under a same term when it rightly exists different terms ! (otherwise any language’s grammar would consist of only one big rule instead of many)

It’s like trying to find a word that means either apple or banana but not orange, himbeer, etc.

Bonus answer: comment are not statements because they are not present in the language grammar.


Bonus question: are comments statements?

Only when they are. For example, with docstrings, assuming the language treats docstrings as statements and it defines docstrings as comments.

The thing is with programming languages, the same terms get used in different ways in different languages. For example, C# and Java both make the distinction between expressions and statements, whereas in F# everything is an expression. Likewise with comments: different languages treat comments in different ways:

  1. In many BASIC’s, comments exist as REM statements, so it could be argued that they are statements.
  2. In many languages, comments are used to hold documentation, which is thrown away by the compiler. But in languages like Python, docstrings are used to hold that documentation, so comments about the code are accessible at runtime and can be used as metadata, affecting the code behaviour.
  3. In Lisp, docstrings are, like everything else, S-Expressions. Lisp doesn’t have statements, but S-Expressions are the (sort of) equivalent.

Trying to find common terms for anything in programming tends to be a thankless task…

“Code element”? It is not very meaningful since code elements may be nested. Compound code elements would consist of a bunch of code elements. It does express you are still talking about a unit, some coherent part of the code. But that is all it says.

“Language element” would be a little more restrictive. Code elements may include documentation headers, pre-processor directives and other meta data while language elements do not.

The only thing i see is compilation unit

A compilation unit is C source code that is compiled and treated as one logical unit. The compilation unit is usually one or more entire files, but can also be a selected portion of a file if, for example, the #ifdef preprocessor directive is used to select specific code sections.


Bonus : comments aren’t code so they aren’t statements.
But maybe with some JS framework (angularJS, bootstrap) using comments inside the HTML to works it can be considered as such

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *