Values of Expressions

Understand the difference between syntax and semantics of expressions as well as how expression evaluation works.

Syntax vs. semantics

When studying languages in general and programming languages in particular, it is worth learning to differentiate between syntax and semantics. The syntax of a language specifies which character combinations are valid and which aren’t. This lesson focuses on semantics, which is concerned with the meaning of those character combinations.

Consider, for instance, the expression 12 + 34. What we enter is nothing more than a string—a character combination— consisting of characters 1, 2, 3, 4, empty space, +, and so on. The OCaml parser recognizes this input, and creates an abstract syntax tree to represent the expression. Until this point, it’s been all about syntax.

Now, an interesting question is, what about 46, the result of adding 12 and 34? This is the semantics of the string, 12 + 34.

The following diagram visualizes this distinction between syntax and semantics.

Get hands-on with 1200+ tech skills courses.