0

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?

3
  • 2
    The Wikipedia article that you linked to already answers your questions: Many languages (e.g. C) make a distinction between statements and definitions, with a statement only containing executable code and a definition instantiating an identifier, while an expression evaluates to a value only. What, specifically, is confusing? – Thomas Owens Oct 3 '16 at 10:56
  • @ThomasOwens Ok I should edit the question, I'm looking for a word to unify the two terms. – user2180613 Oct 3 '16 at 10:58
  • 1
    The edit is better - it's more clear what you are looking for. However, there may not be such a term. – Thomas Owens Oct 3 '16 at 11:30
1

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.

0

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): https://docs.oracle.com/javase/specs/jls/se7/html/jls-18.html

Let's begin with Statement

Statement: Block ; 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:

ClassDeclaration: NormalClassDeclaration EnumDeclaration

Now a method declaration:

MethodDeclaratorRest: FormalParameters {[]} [throws QualifiedIdentifierList] (Block | ;)

And finally the import (why not after all):

ImportDeclaration: 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

CompilationUnit: [[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.

1
  • To compiler/grammar specialists: feel free to edit my post if some terms I used are not accurate. – Spotted Oct 3 '16 at 12:24
0

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...

0

"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.

-2

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.

See https://www.cs.auckland.ac.nz/references/unix/digital/AQTLTBTE/DOCU_015.HTM

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.