INFOM227 ExampleAnalyser
The aim of this project is to provide an example of a static code analyser and to remind people of the basics of
creating languages.
The analyser has been implemented in Scala for several reasons:
Requirements
The application requires:
Download
You can download the application on
the downloads page.
Execution
You can execute a program using the application by running the following command:
java -jar Small-X.X.X-all.jar run <path-to-your-program>
You can run a zero analysis on a program using the application by running the following command:
java -jar Small-X.X.X-all.jar zero-analysis <path-to-your-program>
Setting up a development environment
Requirements
The application requires:
Installation & Build
You can install the project using Gradle and build the application with the following commands:
Windows
./gradlew.bat build
Linux & MacOS
./gradlew build
The application will be built in the build/libs/Small-X.X.X-all.jar file.
Tests
You can run the tests of the project using Gradle with the following commands:
Windows
./gradlew.bat test
Linux & MacOS
./gradlew test
Syntax
The syntax of a programming language determines what constitutes a valid program in terms of text. In almost all cases
for programming languages, this syntax is defined using
a Context-free grammar. In the course and in this example, the
grammars are defined formally using
the EBNF (extended Backus-Naur form) notation.
Grammar
In this example, we will use the following grammar to define the syntax of our language:
$$
\begin{align}
& \langle program \rangle ::= \ \langle function \rangle * \
& \langle function \rangle ::= \ \texttt{‘function’} \quad \langle identifier \rangle \texttt{‘(’} \langle parameters \rangle \texttt{‘)’} \quad \langle body \rangle \
& \langle parameters \rangle ::= \ [\ \langle identifier \rangle (\texttt{‘,’} \ \langle identifier \rangle) * \ ] \
& \langle body \rangle ::= \ \texttt{‘{’} \langle stmt \rangle * \texttt{‘}’} \
& \langle stmt \rangle ::= \ \langle assignStmt \rangle \ | \ \langle ifStmt \rangle \ | \ \langle whileStmt \rangle \ | \ \langle returnStmt \rangle \
& \langle assignStmt \rangle ::= \ \langle identifier \rangle \ \texttt{‘=’} \ (\langle expr \rangle | \langle funcCall \rangle) \texttt{‘;’} \
& \langle ifStmt \rangle ::= \ \texttt{‘if’} \quad \texttt{‘(’} \langle boolExpr \rangle \texttt{‘)’} \quad \langle body \rangle \quad \texttt{‘else’} \quad \langle body \rangle \
& \langle whileStmt \rangle ::= \ \texttt{‘while’} \quad \texttt{‘(’} \langle boolExpr \rangle \texttt{‘)’} \quad \langle body \rangle \
& \langle returnStmt \rangle ::= \ \texttt{‘return’} \quad \langle expr \rangle \texttt{‘;’} \
& \langle arguments \rangle ::= \ [\ \langle expr \rangle (\texttt{‘,’} \ \langle expr \rangle) * \ ] \
& \langle funcCall \rangle ::= \ \langle identifier \rangle \texttt{‘(’} \langle arguments \rangle \texttt{‘)’} \
& \langle expr \rangle ::= \ \langle arithExpr \rangle \ | \ \langle boolExpr \rangle \
& \langle arithExpr \rangle ::= \ \langle noprnd \rangle \ | \ \langle binArithOp \rangle \
& \langle boolExpr \rangle ::= \ \langle boprnd \rangle \ | \ \langle relOp \rangle \ | \ \langle binLogicOp \rangle \
& \langle binArithOp \rangle ::= \ \langle noprnd \rangle \ \langle arithOp \rangle \ \langle noprnd \rangle \
& \langle binLogicOp \rangle ::= \ \langle noprnd \rangle \ \langle logicOp \rangle \ \langle noprnd \rangle \
& \langle relOp \rangle ::= \ \langle boprnd \rangle \ \langle nop \rangle \ \langle boprnd \rangle \
& \langle noprnd \rangle ::= \ \langle identifier \rangle \ | \ \langle num \rangle \
& \langle boprnd \rangle ::= \ \langle identifier \rangle \ | \ \texttt{‘True’} \ | \ \texttt{‘False’} \
& \langle arithOp \rangle ::= \ \texttt{‘+’}\ | \ \texttt{‘-’}\ | \ \texttt{‘*’}\ | \ \texttt{‘/’} \
& \langle logicOp \rangle ::= \ \texttt{‘<’}\ | \ \texttt{‘>’}\ | \ \texttt{‘==’}\ | \ \texttt{‘!=’}\ | \ \texttt{‘>=’}\ | \ \texttt{‘<=’} \
& \langle nop \rangle ::= \ \texttt{‘==’}\ | \ \texttt{‘!=’}\ | \ \texttt{‘and’}\ | \ \texttt{‘or’} \
& \langle digit \rangle ::= \ \texttt{‘0’}\ | \ \texttt{‘1’}\ | \ \texttt{‘2’}\ | \ \texttt{‘3’}\ | \ \texttt{‘4’}\ | \ \texttt{‘5’}\ | \ \texttt{‘6’}\ | \ \texttt{‘7’}\ | \ \texttt{‘8’}\ | \ \texttt{‘9’} \
& \langle num \rangle ::= \ \langle digit \rangle+ \
& \langle letter \rangle ::= \ \texttt{‘a’}\ | \ \texttt{‘b’}\ | \ \texttt{‘c’}\ | \ \ldots\ | \ \texttt{‘z’}\ | \ \texttt{‘A’}\ | \ \texttt{‘B’}\ | \ \texttt{‘C’}\ | \ \ldots\ | \ \texttt{‘Z’} \
& \langle identifier \rangle ::= \ \langle letter \rangle (\langle letter \rangle\ | \ \langle digit \rangle) *
\end{align}
$$
By using this grammar, it is possible to check whether a text follows a certain format and therefore to verify the
syntax of the programming language we want. However, this grammar cannot be directly converted into code that
automatically checks whether text follows the syntax of our language. This is why we usually use tools such as ANTLR to
do this for us. Even if this involves rewriting the syntax in a format that the tool supports, the use of ANTLR saves a
lot of time by creating code that can recognise a language automatically. You can find the grammar described above in
the format supported by ANTLR if you go there. If
you open it, you’ll see that it looks very similar to our grammar using the format the EBNF notation. The main
difference is that the file is divided into 2 parts, the Lexer part
and the Parser part. The lexer is responsible for converting text into words
and removing unnecessary characters. The order of the rules written here is important because the lexer will use the
first rule that matches the text to create the words. This is why the keywords are above the identifiers. Without
this, ANTLR would not be able to properly find the keywords. Then, there is the parser which is responsible for
converting the sequence of words created by the lexer into
a CST (Concrete Syntax Tree). This CST uses
the Visitor design pattern, which makes it easy to browse for the
information we want.
Concrete Syntax Tree
Here is an example of how some code can be converted into a CST using ANTLR and our grammar:
function main() {
a = 5;
c = a + 2;
return c - 6;
}

As you can see, with just 5 lines of code, this already represents a fairly large CST (If you are interested, this CST
was generated using the ANTLR extension for IntelliJ). Usually,
the branches of expressions are much longer because they are designed to follow
the Order of operations directly at the
syntax level and this is something that can be found in almost all programming
languages (Example with the Python grammar). However, since the
operations in Small cannot be nested, this does not affect us.
Semantics
Once the syntactic analysis has been performed using ANTLR, it is then possible to perform
a Semantic analysis. The semantics of a programming
language define what has a meaning, i.e. which operations make sense and which don’t, what happens when an instruction
is executed, and so on. Indeed, does it make sense to divide a boolean variable by 2 given that the syntax allows it? In
the case of our language, we would rather display an error to the user to indicate that it doesn’t make sense because
there is a mismatch between the type of the variable and the type of the expression. That’s why we need to formally
define the semantics of our language.
Semantic rules
The semantics of our language are defined by
the operational semantics below that use
some rules of inference:
$$
\begin{align}
\text{[True]} & \quad \frac{}{(\mathtt{True},\sigma) \leadsto \mathtt{True}} \
\text{[False]} & \quad \frac{}{(\mathtt{False},\sigma) \leadsto \mathtt{False}} \
\text{[Int]} & \quad \frac{v\in\mathbb{Z}}{(v,\sigma) \leadsto v} \
\text{[Var]} & \quad \frac{x\in\mathtt{< Var >}}{(x,\sigma) \leadsto \sigma(x)} \
\text{[Op]} & \quad \frac{(x_1,\sigma) \leadsto v_1 \quad (x_2,\sigma) \leadsto v_2 \quad v_1 \oplus v_2 = v}{(x_1\oplus x_2,\sigma) \leadsto v} \
\text{[Sequence]} & \quad \frac{(s_1,\Sigma\bullet\sigma) \leadsto (\bot, \Sigma\bullet\sigma’) \quad (s_2,\Sigma\bullet\sigma’) \leadsto (v, \Sigma\bullet\sigma’‘)}{(s_1 \mathtt{;} s_2, \Sigma\bullet\sigma) \leadsto (v, \Sigma\bullet\sigma’‘)} \
\text{[Early return]} & \quad \frac{v \in \mathbb{Z} \cup \lbrace \mathtt{True}, \mathtt{False} \rbrace \quad (s_1,\Sigma\bullet\sigma) \leadsto (v, \Sigma\bullet\sigma’)}{(s_1 \mathtt{;} s_2, \Sigma\bullet\sigma) \leadsto (v, \Sigma\bullet\sigma’)} \
\text{[Simple assignment]} & \quad \frac{(e,\sigma) \leadsto v \quad \sigma’ = \sigma[x\mapsto v]}{(x \ \mathtt{=} \ e, \Sigma\bullet\sigma) \leadsto (\bot, \Sigma\bullet\sigma’)} \
\text{[If-True]} & \quad \frac{(e,\sigma) \leadsto \mathtt{True}\quad (s_1,\Sigma\bullet\sigma) \leadsto (v, \Sigma\bullet\sigma’)}{(\mathtt{if}\ (e)\ s_1\ \mathtt{else}\ s_2, \Sigma\bullet\sigma) \leadsto (v, \Sigma\bullet\sigma’)} \
\text{[If-False]} & \quad \frac{(e,\sigma) \leadsto \mathtt{False}\quad (s_2,\Sigma\bullet\sigma) \leadsto (v, \Sigma\bullet\sigma’)}{(\mathtt{if}\ (e)\ s_1\ \mathtt{else}\ s_2, \Sigma\bullet\sigma) \leadsto (v, \Sigma\bullet\sigma’)} \
\text{[While-True]} & \quad \frac{(e,\sigma) \leadsto \mathtt{True}\quad (s;\mathtt{while}\ (e)\ s, \Sigma\bullet\sigma) \leadsto (v, \Sigma\bullet\sigma’)}{(\mathtt{while}\ (e)\ s, \Sigma\bullet\sigma) \leadsto (v, \Sigma\bullet\sigma’)} \
\text{[While-False]} & \quad \frac{(e,\sigma) \leadsto \mathtt{False}}{(\mathtt{while}\ (e)\ s, \Sigma\bullet\sigma) \leadsto (\bot, \Sigma\bullet\sigma)} \
\text{[Return]} & \quad \frac{(e,\sigma) \leadsto v}{(\mathtt{return}\ e, \Sigma\bullet\sigma) \leadsto (v, \Sigma)} \
\text{[Function call]} & \quad \frac{
\begin{aligned}
\left(
\substack{
\displaystyle (e_1,\sigma) \leadsto a_1 \\
\displaystyle \ldots \\
\displaystyle (e_n,\sigma) \leadsto a_n
}
\right)
\quad
\sigma_n &= \left\lbrace
\substack{
\displaystyle x_1 \mapsto a_1, \\
\displaystyle \ldots \\
\displaystyle x_n \mapsto a_n
}
\right\rbrace
\quad
(B,\Sigma\bullet\sigma\bullet\sigma_n) \leadsto (v,\Sigma\bullet\sigma)
\end{aligned}
}{(y \ \mathtt{=} \ f(e_1, \ldots, e_n), \Sigma\bullet\sigma) \leadsto (\bot, \Sigma\bullet\sigma[y\mapsto v])} \
& \mbox{where $n \geq 0$ and $f$ is defined as}\ \mathtt{function}\ f(x_1,\ldots,x_n) \lbrace B \rbrace \
\end{align}
$$
with:
- The notation $< Var >$ corresponds to the set of variables in the program.
- The symbol $\oplus$ corresponds to the operators
+, -, *, /, <, >, <=, >=, !=, ==, and and or
with their mathematical semantics.
- The symbol $\mathcal{E}$ corresponds to the set of all possible environments.
- The symbol $\sigma$, with $\sigma \in \mathcal{E}$ and $\sigma : < Var > \mapsto \mathbb{Z} \cup \lbrace True, False \rbrace$,
corresponds to the environment of the function currently being executed.
- The symbol $\Sigma$, with $\Sigma = \langle \sigma_0, …, \sigma_n \rangle$, corresponds to the execution stack which
is a sequence of environments.
- The notation $\Sigma \bullet \sigma$ splits the execution stack into the environment of the function currently being
executed $\sigma$ and the rest of the execution stack $\Sigma$.
- The notation $(e, \Sigma \bullet \sigma) \leadsto v$ corresponds to the evaluation of an expression $e$ with respect
to an environment $\sigma$ and the rest of the execution stack $\Sigma$ and which produces $v$ as a value.
- The notation $\sigma[x \mapsto v]$ corresponds to the update of the environment $\sigma$ with the fact that $v$ is
associated to $x$.
- The notation $(I, \Sigma \bullet \sigma) \leadsto (v, \Sigma \bullet \sigma’)$ corresponds to the fact of executing an
instruction $I$ with respect to a state $\Sigma \bullet \sigma$ and yielding a return value $v$ and a
state $\Sigma \bullet \sigma’$.
With these semantic rules, it is now possible to perform our semantic analysis. In untyped languages such as the one
considered here, this is primarily handled at runtime. However, certain properties can still be checked statically, such
as ensuring that no two functions share the same name using the Visitor design pattern and the CST defined earlier. When
that happens, an error is produced in the visitor and then displayed to the programmer. In general, it is also during
this step that we try to simplify our CST into
an AST (Abstract Syntax Tree) that allows us to keep only the
information we need and to create a set of data structures that are easier to use in the rest of our application. The
code that performs the semantic analysis and creates the AST can be
found here. Note that in typed languages, it is
usually a good idea to have a Symbol table to store the variables that
have been defined and their type, but it is not useful in our case.
Abstract Syntax Tree
In our implementation, the AST is defined using Scala classes that allow easy pattern matching of the nodes. Here is
what the AST looks like after being built from the CST mentioned above:

Interpreter
With our AST, it is very easy to create an interpreter for our language that will follow the semantics defined earlier.
All we need to do is
create a class that will represent the environments
and then implement the different rules.
The only difference from the grammar is that we throw exceptions when a semantic error is encountered at runtime.
Zero Analysis
In this section, we will define a Zero Analysis, similar to the one used in the course, by using the Worklist algorithm
implemented here. The file containing the entire
analysis code is available here.
Abstract values & Lattice
First, we need to define the set of abstract values $L$ that we are going to use, and the lattice that is used to order
these values:
$$
L = \lbrace Bottom, Z, NZ, U \rbrace
$$
with:
- $Bottom$ representing the fact that there is no value assigned yet.
- $Z$ representing zero.
- $NZ$ representing any value different from zero
- $U$ representing the fact
that it is not known whether the value is zero or different from zero.

The relation between concrete values and abstract values is defined using the following abstraction and concretisation
functions:
$$
\begin{align}
& \alpha(x) = & Z & \quad if & x = 0 \
& & NZ & \quad if & x \in \mathbb{Z}_0 \
& & U & \quad otherwise
\end{align}
$$
$$
\begin{align}
& \gamma(a) = & \lbrace 0 \rbrace & \quad if & a = Z \
& & \mathbb{Z}_0 & \quad if & a = NZ \
& & \mathbb{Z} \cup \lbrace True, False \rbrace & \quad if & a = U \
& & \emptyset & \quad if & a = Bottom
\end{align}
$$
Control-flow graph
Next, we need to convert our AST into a CFG (Control-flow graph).
The code that achieves this can be found here.
A CFG is composed of the following elements:
- $PRED(p)$ : A function which returns the predecessors of the program point $p$.
- $SUCC(p)$ : A function which returns the successors of the program point $p$.
- $COND(p, p’)$ : A function which returns a boolean expression that must be $True$ to go from the program point $p$ to
the program point $p’$.
Abstract environment & Control-flow function
After that, we can define our abstract environment:
$$
\phi: < Var > \mapsto L
$$
with:
- The notation $< Var >$ corresponds to the set of variables in the program.
Next, we can define our control-flow function by specifying its instances:
$$
\begin{align}
& fg [[ p ]] (\phi) = & f [[ P[p] ]] (\phi) & \quad if & P[p] \equiv x = E & \
& & \phi & \quad if & P[p] \equiv while (E) \lor P[p] \equiv if (E) \lor return \ E & \
\end{align}
$$
with:
$$
\begin{align}
& f [[ x = 0 ]] (\phi) = & \phi[x \mapsto Z] & & & \
& f [[ x = c ]] (\phi) = & \phi[x \mapsto NZ] & \quad if & c \in \mathbb{Z}_0 \cup \lbrace True, False \rbrace & \
& f [[ x = y ]] (\phi) = & \phi[x \mapsto \phi(y)] & \quad if & y \in < Var > & \
& f [[ x = c + d ]] (\phi) = & \phi[x \mapsto Z] & \quad if & c = -d & \
& & \phi[x \mapsto U] & \quad otherwise & & \
& f [[ x = y + z ]] (\phi) = & \phi[x \mapsto Z] & \quad if & \phi(y) = \phi(z) = Z & \
& & \phi[x \mapsto U] & \quad otherwise & & \
& f [[ x = y + c ]] (\phi) = & \phi[x \mapsto Z] & \quad if & \phi(y) = Z \land c = 0 & \
& & \phi[x \mapsto NZ] & \quad if & \phi(y) = Z \land c \neq 0 & \
& & \phi[x \mapsto NZ] & \quad if & \phi(y) = NZ \land c = 0 & \
& & \phi[x \mapsto U] & \quad otherwise & & \
& f [[ x = c + y ]] (\phi) = & f [[ x = y + c ]] (\phi) & & & \
& f [[ x = y \oplus z ]] (\phi) = & \phi[x \mapsto U] & \quad if & \oplus \neq + & \
\end{align}
$$
These control-flow function instances are the same as the ones in the syllabus. It was a choice to make them not that
precise.
Condition update function
Moreover, the Worklist algorithm also requires a function to update the abstract environments according to the boolean
expressions in the $COND(p, p’)$ function.
Here are the function instances that we will use in our analysis:
$$
\begin{align}
& \phi[[ True ]] = & \phi & & \
& \phi[[ False ]] = & \bot & & \
& \phi[[ y < c ]] = & \phi[y \mapsto \phi(y) \sqcap NZ] & \quad if & c \leq 0 \land y \in \phi \land \phi(y) \sqcap NZ \neq \bot & \
& & \phi & \quad if & c > 0 \land y \in \phi & \
& & \bot & \quad otherwise & & \
& \phi[[ c < y ]] = & \phi[[ y > c ]] & & & \
& \phi[[ y > c ]] = & \phi[y \mapsto \phi(y) \sqcap NZ] & \quad if & c \geq 0 \land y \in \phi \land \phi(y) \sqcap NZ \neq \bot & \
& & \phi & \quad if & c < 0 \land y \in \phi & \
& & \bot & \quad otherwise & & \
& \phi[[ c > y ]] = & \phi[[ y < c ]] & & & \
& \phi[[ y <= c ]] = & \phi[y \mapsto \phi(y) \sqcap NZ] & \quad if & c < 0 \land y \in \phi \land \phi(y) \sqcap NZ \neq \bot & \
& & \phi & \quad if & c \geq 0 \land y \in \phi & \
& & \bot & \quad otherwise & & \
& \phi[[ c <= y ]] = & \phi[[ y >= c ]] & & & \
& \phi[[ y >= c ]] = & \phi[y \mapsto \phi(y) \sqcap NZ] & \quad if & c > 0 \land y \in \phi \land \phi(y) \sqcap NZ \neq \bot & \
& & \phi & \quad if & c \leq 0 \land y \in \phi & \
& & \bot & \quad otherwise & & \
& \phi[[ c >= y ]] = & \phi[[ y <= c ]] & & & \
& \phi[[ y == c ]] = & \phi[y \mapsto \phi(y) \sqcap Z] & \quad if & c = 0 \land y \in \phi \land \phi(y) \sqcap Z \neq \bot & \
& & \phi[y \mapsto \phi(y) \sqcap NZ] & \quad if & c \neq 0 \land y \in \phi \land \phi(y) \sqcap NZ \neq \bot & \
& & \bot & \quad otherwise & & \
& \phi[[ c == y ]] = & \phi[[ y == c ]] & & & \
& \phi[[ y\ != c ]] = & \phi[y \mapsto \phi(y) \sqcap NZ] & \quad if & c = 0 \land y \in \phi \land \phi(y) \sqcap NZ \neq \bot & \
& & \phi & \quad if & c \neq 0 \land y \in \phi & \
& & \bot & \quad otherwise & & \
& \phi[[ c\ != y ]] = & \phi[[ y\ != c ]] & & & \
& \phi[[ E ]] = & \phi & \quad if & \text{E is not defined in the other instances} & \
\end{align}
$$
GEN/KILL
It is also possible to represent the analysis using an extended version of GEN/KILL:
$$
\begin{align}
\phi_1 \sqsubseteq \phi_2 & \Leftrightarrow \forall x \in dom(\phi_1) \Rightarrow (x \in \phi_2 \land \phi_1(x) \sqsubseteq \phi_2(x)) \
\phi_1 \sqcup \phi_2 & = \lbrace x \mapsto \phi_1(x) \sqcup \phi_2(x) \mid x \in dom(\phi_1) \cap dom(\phi_2) \rbrace \
& \cup \lbrace x \mapsto \phi_1(x) \mid x \in dom(\phi_1) \setminus dom(\phi_2) \rbrace \
& \cup \lbrace x \mapsto \phi_2(x) \mid x \in dom(\phi_2) \setminus dom(\phi_1) \rbrace \
\top & = \lbrace x \mapsto U \mid x \in < Var > \rbrace \
\bot & = \lbrace \rbrace\
\end{align}
$$
$$
\begin{align}
& GEN(p) = & \lbrace x \mapsto fg[[ p ]] (\phi) (x) \rbrace & \quad if & P[p] \equiv x = E \
& & \emptyset & \quad otherwise & \
& KILL(p) = & \lbrace x \mapsto \phi(x) \rbrace & \quad if & x \in \phi \land P[p] \equiv x = E \
& & \emptyset & \quad otherwise & \
\end{align}
$$
Results interpretation
Finally, we can define processing rules in order to interpret the results of the analysis:
| Line (PP) |
Instruction (I) |
Condition © |
Type (T) |
Message (M) |
| p |
x = y / z |
$\phi_p(z) = Z$ |
Error |
“Division by zero detected !” |
| p |
x = y / z |
$\phi_p(z) = Z$ |
Warning |
“Possible division by zero !” |
These rules are the same as the ones in the course.
评论