On this page:
2.2.1 Syntax, Semantics and Pragmatics
2.2.2 Syntax and Semantics of Julia
2.2.3 The Evaluator

2.2 The Julia Language

In this section we will learn about Julia programming language, which we will use throughout this text. But first, we are going to examine some aspects that are common to other languages.

2.2.1 Syntax, Semantics and Pragmatics

Every language has syntax, semantics, and pragmatics.

In simple terms, syntax is a set of rules that dictate the kind of sentence that can be written in a language. Without it, any concatenation of words could be a sentence. For example, given the words “John”, “cake”, “ate”, and “the”, the syntax rules of the English language tell us that - “John ate the cake” is a correct sentence, and that - “the ate cake John” is not. It is also important to note that, according to the English syntax, “The cake ate John” is also syntactically correct. This means that syntax is not enough to completely specify the rules of a language. In fact, syntax dictates how a sentence is constructed but says nothing in regards to its meaning. That is the job of semantics, which attributes meaning to a sentence, thus telling us that “The cake ate John” makes no sense. Finally, pragmatics sets the way sentences are commonly expressed. In a language, pragmatic changes depending on the context: the way two close friends talk with each other is different from the way two strangers talk.

These three aspects of a language are also present when we discuss programming languages. Unlike the natural languages we use to communicate between us, programming languages are characterized as being formal, obeying a set of simple and restrictive rules that can be mechanically processed.

In this document we will describe Julia’s syntax and semantics and, although there are mathematical formalisms to describe rigorously those two aspects, they require a mathematical sophistication that, given the nature of this work, is inappropriate. Hence, we will only use informal descriptions. Afterwards, as we introduce language elements, we will discuss the language pragmatics.

2.2.2 Syntax and Semantics of Julia

Compared to other programming languages, Julia’s syntax is simple and is based on the concept of expression.

An expression in Julia can be formed using primitive elements, such as numbers, or by the combination of those elements using an operation, such as the sum of two numbers. This simple definition allows us to build expressions of arbitrary complexity. However, it is important to remember that syntax restricts what can be written: the fact that we can combine expressions to create more complex ones does not mean we can write any combination of expressions. These combinations are restricted by syntactic rules, which we will describe throughout this text.

Much like syntax, Julia’s semantics is also simple when compared to other programming languages. As we will see, semantics is determined by the operators that are used in our expressions. For instance, the sum operator is used to add two numbers. An expression that combines this operator with, for example, the numbers \(3\) and \(4\) will have as meaning the sum between \(3\) and \(4\), i.e., \(7\). In a programming language, the semantics of an expression is given by the computer when it evaluates the expression.

2.2.3 The Evaluator

Every expression in Julia has a value. This concept is so important that Julia provides an evaluator, i.e., a program designed to evaluate expressions.

In Julia, the evaluator is shown as soon as we start working with the language. Once Julia is running, the user is presented with the text julia> (called prompt), meaning that Julia is waiting for the user to input an expression. Julia interacts with the user by executing a cycle that reads an expression, determines its value, and writes it. This cycle is traditionally called read-eval-print loop (abbreviated to REPL).

During the read phase, Julia reads an expression. In the evaluation phase, that expression is analyzed in order to produce a value. This analysis uses rules that dictate, for each case, the expression value. Finally, in the print phase, this value is printed so that the user can see it. These different phases are visible in the following example:

julia> 3+4

7

julia>

The existence of the read-eval-print loop makes Julia an interactive language that allows programs to be quickly developed by writing, testing, and correcting small fragments at a time.