11

When describing the syntax of a given programming language the words "expression" and "term" are often used to seemingly describe the same things. Are these words interchangeable in the context of programming languages?

Anton Trunov
  • 3,499
  • 1
  • 19
  • 26
tibbe
  • 245
  • 2
  • 5

3 Answers3

11

The two words expression and term have largely the same sets of possible meaning, but in a specific presentation, they may not be synonyms.

In rewriting theory, a term is something that conforms to a certain syntax, and for which a notion of computation (rewriting) is defined. In programming language theory, term can mean specifically one of the two aspects. And so can expression. Which means which depends on the author, and not all authors use both words in this way.

  • One possible meaning of the word is “something that can be computed”. In this sense, if the computation terminates, you end up with a value.
  • Another possible meaning of the word is “something that conforms to a certain syntax”. There is often a computation mechanism defined over this syntax, but not always.

In the theory of a programming language, there are often multiple syntactic objects: the core language, types, kinds, objects, classes, modules, interfaces, … Something like function x : int => x + 1 may be a core term which contains the type term int, or a core expression which contains the type expression int. It's common but not universal to use one of the two nouns to mean “core” (and “core” is my choice of terminology), e.g. “function x : int => x + 1 is an expression” or “function x : int => x + 1 is a term”. Different authors use different words; communities of a specific language tend to align on terminology, but that isn't systematic.

For example, in the world of ML, “expression” with no qualifier is normally reserved for the core language, and there are additionally “module expressions” in the module language, as well as “type expressions” and “signature expressions”. A “term” can be anything conforming to one of the syntactic forms, although many authors (e.g. the SML definition) don't use “term” at all.

But in Coq, a “term” is the most important syntactic form. Due to the nature of the language, the syntax of types is the same as terms. The Coq manual uses “expression” to designate any family of syntactic form.

If you're reading a text on programming languages, the meaning of the words should be explained or (in less formal texts) clear from context. If you're writing a text, try to use the terminology that's common in your community, and always make sure that the definitions you use are clear.

Gilles 'SO- stop being evil'
  • 44,159
  • 8
  • 120
  • 184
6

These terms do not have any universal meaning in computer science theory. They are popular choices for the names of productions in particular grammars, but they only have meaning within the context of a specific grammar. By convention "expression" is often used for the top-level construct in a grammar (so that in some sense "everything is an expression"), while "term" is used for a much more specific construct, often an operand to an addition or subtraction operator. The word "term" may be used with other meanings in other contexts, for example it has a particular meaning in some flavours of BNF.

Michael Kay
  • 449
  • 2
  • 4
2

In predicate logic, you can have both function symbols and relation symbols. The terms in this context are the valid combinations of variables, constants and function symbols. The valid combinations of terms, relation symbols and equality are called atomic formulas, the valid combinations of atomic formulas, logical operators and logical quantifiers are called formulas.

In programming languages like C or C++, things like typedefs or function definitions and declarations are probably not expressions. Control structures like for, if, and while are probably not expressions either (because they don't need to be terminated by a semicolon, and because they don't have a type). The language specifies exactly what counts as an expression, and I guess that an expression (in C/C++) always has a type.

Thomas Klimpel
  • 5,440
  • 29
  • 69